general: add compat module and helper for easy backwards compatibiltity for pre-PIP dependencies

my.hypothesis: use hypexport as a proper PIP package + fallback
This commit is contained in:
Dima Gerasimov 2020-09-29 17:40:16 +01:00 committed by karlicoss
parent fbaa8e0b44
commit 109edd9da3
8 changed files with 82 additions and 21 deletions

49
my/core/compat.py Normal file
View file

@ -0,0 +1,49 @@
'''
Some backwards compatibility stuff/deprecation helpers
'''
import warnings
from ..common import LazyLogger
logger = LazyLogger('my.core.compat')
def pre_pip_dal_handler(
name: str,
e: ModuleNotFoundError,
cfg,
requires=[],
) -> None:
'''
https://github.com/karlicoss/HPI/issues/79
'''
if e.name != name:
# the module itself was imported, so the problem is with some dependencies
raise e
try:
dal = _get_dal(cfg, name)
# todo this is fairly high severity, would be nice to highlight in the terminal or something
warnings.warn(f'''
Specifying modules' dependencies in the config or in my/config/repos is deprecated!
Please install {' '.join(requires)} as PIP packages (see the corresponding README instructions).
'''.strip())
except ModuleNotFoundError as ee:
dal = None
if dal is None:
# probably means there was nothing in the old config in the first place
# so we should raise the original exception
raise e
return dal
def _get_dal(cfg, module_name: str):
mpath = getattr(cfg, module_name, None)
if mpath is not None:
from .common import import_dir
return import_dir(mpath, '.dal')
else:
from importlib import import_module
return import_module(f'my.config.repos.{module_name}.dal')

View file

@ -1,6 +1,12 @@
'''
Endomondo exercise data
'''
REQUIRES = [
'git+https://github.com/karlicoss/endoexport',
]
# todo use ast in setup.py or doctor to extract the corresponding pip packages?
from dataclasses import dataclass
from pathlib import Path
from typing import Sequence, Iterable

View file

@ -1,6 +1,9 @@
"""
Github data: events, comments, etc. (API data)
"""
REQUIRES = [
'git+https://github.com/karlicoss/ghexport',
]
from dataclasses import dataclass
from typing import Optional

View file

@ -4,10 +4,15 @@
from dataclasses import dataclass
from typing import Optional
from .core import Paths, PathIsh
from .core import Paths
from my.config import hypothesis as user_config
REQUIRES = [
'git+https://github.com/karlicoss/hypexport',
]
@dataclass
class hypothesis(user_config):
@ -18,30 +23,16 @@ class hypothesis(user_config):
# paths[s]/glob to the exported JSON data
export_path: Paths
# path to a local clone of hypexport
# alternatively, you can put the repository (or a symlink) in $MY_CONFIG/my/config/repos/hypexport
hypexport : Optional[PathIsh] = None
@property
def dal_module(self):
rpath = self.hypexport
if rpath is not None:
from .core.common import import_dir
return import_dir(rpath, '.dal')
else:
import my.config.repos.hypexport.dal as dal
return dal
from .core.cfg import make_config
config = make_config(hypothesis)
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import my.config.repos.hypexport.dal as dal
else:
dal = config.dal_module
try:
from hypexport import dal
except ModuleNotFoundError as e:
from .core.compat import pre_pip_dal_handler
dal = pre_pip_dal_handler('hypexport', e, config, requires=REQUIRES)
############################

View file

@ -1,6 +1,10 @@
"""
[[https://www.instapaper.com][Instapaper]] bookmarks, highlights and annotations
"""
REQUIRES = [
'git+https://github.com/karlicoss/instapexport',
]
from dataclasses import dataclass
from typing import Optional

View file

@ -1,6 +1,9 @@
"""
[[https://getpocket.com][Pocket]] bookmarks and highlights
"""
REQUIRES = [
'git+https://github.com/karlicoss/pockexport',
]
from dataclasses import dataclass
from typing import Optional

View file

@ -1,6 +1,9 @@
"""
Reddit data: saved items/comments/upvotes/etc.
"""
REQUIRES = [
'git+https://github.com/karlicoss/rexport',
]
from typing import Optional
from .core.common import Paths, PathIsh

View file

@ -1,6 +1,9 @@
'''
Rescuetime (phone activity tracking) data.
'''
REQUIRES = [
'git+https://github.com/karlicoss/rescuexport',
]
from pathlib import Path
from datetime import datetime, timedelta
@ -21,7 +24,6 @@ def inputs() -> Sequence[Path]:
return get_files(config.export_path)
# pip git+https://github.com/karlicoss/rescuexport
import rescuexport.dal as dal
DAL = dal.DAL
Entry = dal.Entry