diff --git a/my/core/compat.py b/my/core/compat.py index d7937f9..48e194b 100644 --- a/my/core/compat.py +++ b/my/core/compat.py @@ -1,56 +1,12 @@ ''' -Some backwards compatibility stuff/deprecation helpers +Contains backwards compatibility helpers for different python versions. +If something is relevant to HPI itself, please put it in .hpi_compat instead ''' +import os import sys -from types import ModuleType from typing import TYPE_CHECKING -from . import warnings -from .common import LazyLogger - -logger = LazyLogger('my.core.compat') - - -def pre_pip_dal_handler( - name: str, - e: ModuleNotFoundError, - cfg, - requires=[], -) -> ModuleType: - ''' - 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) - warnings.high(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(), stacklevel=2) - except ModuleNotFoundError: - 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') - - -import os windows = os.name == 'nt' @@ -60,7 +16,7 @@ def sqlite_backup(*, source: sqlite3.Connection, dest: sqlite3.Connection, **kwa source.backup(dest, **kwargs) -# can remove after python3.9 +# can remove after python3.9 (although need to keep the method itself for bwd compat) def removeprefix(text: str, prefix: str) -> str: if text.startswith(prefix): return text[len(prefix):] diff --git a/my/core/legacy.py b/my/core/hpi_compat.py similarity index 61% rename from my/core/legacy.py rename to my/core/hpi_compat.py index 3ad121d..9ef103a 100644 --- a/my/core/legacy.py +++ b/my/core/hpi_compat.py @@ -1,16 +1,20 @@ -# I think 'compat' should be for python-specific compat stuff, whereas this for HPI specific backwards compatibility +""" +Contains various backwards compatibility/deprecation helpers relevant to HPI itself. +(as opposed to .compat module which implements compatibility between python versions) +""" import os import inspect import re +from types import ModuleType from typing import List -from my.core import warnings as W +from my.core import warnings def handle_legacy_import( - parent_module_name: str, - legacy_submodule_name: str, - parent_module_path: List[str], + parent_module_name: str, + legacy_submodule_name: str, + parent_module_path: List[str], ) -> bool: ### # this is to trick mypy into treating this as a proper namespace package @@ -19,6 +23,7 @@ def handle_legacy_import( # - https://github.com/karlicoss/hpi_namespace_experiment # - discussion here https://memex.zulipchat.com/#narrow/stream/279601-hpi/topic/extending.20HPI/near/269946944 from pkgutil import extend_path + parent_module_path[:] = extend_path(parent_module_path, parent_module_name) # 'this' source tree ends up first in the pythonpath when we extend_path() # so we need to move 'this' source tree towards the end to make sure we prioritize overlays @@ -52,9 +57,54 @@ def handle_legacy_import( is_legacy_import = not (imported_as_parent or importing_submodule) if is_legacy_import and not autocompleting_module_cli: - W.high(f'''\ + warnings.high( + f'''\ importing {parent_module_name} is DEPRECATED! \ Instead, import from {parent_module_name}.{legacy_submodule_name} or {parent_module_name}.all \ See https://github.com/karlicoss/HPI/blob/master/doc/MODULE_DESIGN.org#allpy for more info. -''') +''' + ) return is_legacy_import + + +def pre_pip_dal_handler( + name: str, + e: ModuleNotFoundError, + cfg, + requires=[], +) -> ModuleType: + ''' + 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) + warnings.high( + 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(), + stacklevel=2, + ) + except ModuleNotFoundError: + 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/fbmessenger/__init__.py b/my/fbmessenger/__init__.py index 3919c44..40fb235 100644 --- a/my/fbmessenger/__init__.py +++ b/my/fbmessenger/__init__.py @@ -19,7 +19,7 @@ REQUIRES = [ ] -from my.core.legacy import handle_legacy_import +from my.core.hpi_compat import handle_legacy_import is_legacy_import = handle_legacy_import( parent_module_name=__name__, legacy_submodule_name='export', diff --git a/my/github/ghexport.py b/my/github/ghexport.py index 9eebbf0..d446c35 100644 --- a/my/github/ghexport.py +++ b/my/github/ghexport.py @@ -35,7 +35,8 @@ config = make_config(github, migration=migration) try: from ghexport import dal except ModuleNotFoundError as e: - from my.core.compat import pre_pip_dal_handler + from my.core.hpi_compat import pre_pip_dal_handler + dal = pre_pip_dal_handler('ghexport', e, config, requires=REQUIRES) ############################ diff --git a/my/hypothesis.py b/my/hypothesis.py index 370854a..822fe9d 100644 --- a/my/hypothesis.py +++ b/my/hypothesis.py @@ -35,7 +35,8 @@ config = make_config(hypothesis) try: from hypexport import dal except ModuleNotFoundError as e: - from .core.compat import pre_pip_dal_handler + from my.core.hpi_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 1ab62c2..df1f70b 100644 --- a/my/instapaper.py +++ b/my/instapaper.py @@ -28,7 +28,8 @@ config = make_config(instapaper) try: from instapexport import dal except ModuleNotFoundError as e: - from .core.compat import pre_pip_dal_handler + from my.core.hpi_compat import pre_pip_dal_handler + dal = pre_pip_dal_handler('instapexport', e, config, requires=REQUIRES) ############################ diff --git a/my/pocket.py b/my/pocket.py index 912806d..2a7bdcb 100644 --- a/my/pocket.py +++ b/my/pocket.py @@ -28,7 +28,8 @@ config = make_config(pocket) try: from pockexport import dal except ModuleNotFoundError as e: - from .core.compat import pre_pip_dal_handler + from my.core.hpi_compat import pre_pip_dal_handler + dal = pre_pip_dal_handler('pockexport', e, config, requires=REQUIRES) ############################ diff --git a/my/reddit/__init__.py b/my/reddit/__init__.py index 22813f1..e81aaf9 100644 --- a/my/reddit/__init__.py +++ b/my/reddit/__init__.py @@ -19,7 +19,7 @@ REQUIRES = [ ] -from my.core.legacy import handle_legacy_import +from my.core.hpi_compat import handle_legacy_import is_legacy_import = handle_legacy_import( parent_module_name=__name__, legacy_submodule_name='rexport', diff --git a/my/reddit/rexport.py b/my/reddit/rexport.py index f166ecd..a7be39b 100644 --- a/my/reddit/rexport.py +++ b/my/reddit/rexport.py @@ -67,7 +67,7 @@ config = make_config(reddit, migration=migration) try: from rexport import dal except ModuleNotFoundError as e: - from my.core.compat import pre_pip_dal_handler + from my.core.hpi_compat import pre_pip_dal_handler dal = pre_pip_dal_handler('rexport', e, config, requires=REQUIRES) # TODO ugh. this would import too early