diff --git a/my/core/compat.py b/my/core/compat.py new file mode 100644 index 0000000..2a173b6 --- /dev/null +++ b/my/core/compat.py @@ -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') + diff --git a/my/endomondo.py b/my/endomondo.py index df470b6..7b38ea3 100644 --- a/my/endomondo.py +++ b/my/endomondo.py @@ -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 diff --git a/my/github/ghexport.py b/my/github/ghexport.py index 5f54d03..bef5440 100644 --- a/my/github/ghexport.py +++ b/my/github/ghexport.py @@ -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 diff --git a/my/hypothesis.py b/my/hypothesis.py index cabaa25..ddf4239 100644 --- a/my/hypothesis.py +++ b/my/hypothesis.py @@ -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) ############################ diff --git a/my/instapaper.py b/my/instapaper.py index 9730eeb..b312281 100644 --- a/my/instapaper.py +++ b/my/instapaper.py @@ -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 diff --git a/my/pocket.py b/my/pocket.py index f20bb88..ada6fc7 100644 --- a/my/pocket.py +++ b/my/pocket.py @@ -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 diff --git a/my/reddit.py b/my/reddit.py index c6cb76d..5e1f83b 100755 --- a/my/reddit.py +++ b/my/reddit.py @@ -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 diff --git a/my/rescuetime.py b/my/rescuetime.py index b2acad9..06c72eb 100644 --- a/my/rescuetime.py +++ b/my/rescuetime.py @@ -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