core: move split compat/legacy modules into hpi_compat and compat
This commit is contained in:
parent
70bf51a125
commit
4f7c9b4a71
9 changed files with 72 additions and 62 deletions
|
@ -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
|
import sys
|
||||||
from types import ModuleType
|
|
||||||
from typing import TYPE_CHECKING
|
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'
|
windows = os.name == 'nt'
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,7 +16,7 @@ def sqlite_backup(*, source: sqlite3.Connection, dest: sqlite3.Connection, **kwa
|
||||||
source.backup(dest, **kwargs)
|
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:
|
def removeprefix(text: str, prefix: str) -> str:
|
||||||
if text.startswith(prefix):
|
if text.startswith(prefix):
|
||||||
return text[len(prefix):]
|
return text[len(prefix):]
|
||||||
|
|
|
@ -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 os
|
||||||
import inspect
|
import inspect
|
||||||
import re
|
import re
|
||||||
|
from types import ModuleType
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from my.core import warnings as W
|
from my.core import warnings
|
||||||
|
|
||||||
|
|
||||||
def handle_legacy_import(
|
def handle_legacy_import(
|
||||||
parent_module_name: str,
|
parent_module_name: str,
|
||||||
legacy_submodule_name: str,
|
legacy_submodule_name: str,
|
||||||
parent_module_path: List[str],
|
parent_module_path: List[str],
|
||||||
) -> bool:
|
) -> bool:
|
||||||
###
|
###
|
||||||
# this is to trick mypy into treating this as a proper namespace package
|
# 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
|
# - https://github.com/karlicoss/hpi_namespace_experiment
|
||||||
# - discussion here https://memex.zulipchat.com/#narrow/stream/279601-hpi/topic/extending.20HPI/near/269946944
|
# - discussion here https://memex.zulipchat.com/#narrow/stream/279601-hpi/topic/extending.20HPI/near/269946944
|
||||||
from pkgutil import extend_path
|
from pkgutil import extend_path
|
||||||
|
|
||||||
parent_module_path[:] = extend_path(parent_module_path, parent_module_name)
|
parent_module_path[:] = extend_path(parent_module_path, parent_module_name)
|
||||||
# 'this' source tree ends up first in the pythonpath when we extend_path()
|
# '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
|
# 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)
|
is_legacy_import = not (imported_as_parent or importing_submodule)
|
||||||
if is_legacy_import and not autocompleting_module_cli:
|
if is_legacy_import and not autocompleting_module_cli:
|
||||||
W.high(f'''\
|
warnings.high(
|
||||||
|
f'''\
|
||||||
importing {parent_module_name} is DEPRECATED! \
|
importing {parent_module_name} is DEPRECATED! \
|
||||||
Instead, import from {parent_module_name}.{legacy_submodule_name} or {parent_module_name}.all \
|
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.
|
See https://github.com/karlicoss/HPI/blob/master/doc/MODULE_DESIGN.org#allpy for more info.
|
||||||
''')
|
'''
|
||||||
|
)
|
||||||
return is_legacy_import
|
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')
|
|
@ -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(
|
is_legacy_import = handle_legacy_import(
|
||||||
parent_module_name=__name__,
|
parent_module_name=__name__,
|
||||||
legacy_submodule_name='export',
|
legacy_submodule_name='export',
|
||||||
|
|
|
@ -35,7 +35,8 @@ config = make_config(github, migration=migration)
|
||||||
try:
|
try:
|
||||||
from ghexport import dal
|
from ghexport import dal
|
||||||
except ModuleNotFoundError as e:
|
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)
|
dal = pre_pip_dal_handler('ghexport', e, config, requires=REQUIRES)
|
||||||
|
|
||||||
############################
|
############################
|
||||||
|
|
|
@ -35,7 +35,8 @@ config = make_config(hypothesis)
|
||||||
try:
|
try:
|
||||||
from hypexport import dal
|
from hypexport import dal
|
||||||
except ModuleNotFoundError as e:
|
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)
|
dal = pre_pip_dal_handler('hypexport', e, config, requires=REQUIRES)
|
||||||
|
|
||||||
############################
|
############################
|
||||||
|
|
|
@ -28,7 +28,8 @@ config = make_config(instapaper)
|
||||||
try:
|
try:
|
||||||
from instapexport import dal
|
from instapexport import dal
|
||||||
except ModuleNotFoundError as e:
|
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)
|
dal = pre_pip_dal_handler('instapexport', e, config, requires=REQUIRES)
|
||||||
|
|
||||||
############################
|
############################
|
||||||
|
|
|
@ -28,7 +28,8 @@ config = make_config(pocket)
|
||||||
try:
|
try:
|
||||||
from pockexport import dal
|
from pockexport import dal
|
||||||
except ModuleNotFoundError as e:
|
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)
|
dal = pre_pip_dal_handler('pockexport', e, config, requires=REQUIRES)
|
||||||
|
|
||||||
############################
|
############################
|
||||||
|
|
|
@ -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(
|
is_legacy_import = handle_legacy_import(
|
||||||
parent_module_name=__name__,
|
parent_module_name=__name__,
|
||||||
legacy_submodule_name='rexport',
|
legacy_submodule_name='rexport',
|
||||||
|
|
|
@ -67,7 +67,7 @@ config = make_config(reddit, migration=migration)
|
||||||
try:
|
try:
|
||||||
from rexport import dal
|
from rexport import dal
|
||||||
except ModuleNotFoundError as e:
|
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)
|
dal = pre_pip_dal_handler('rexport', e, config, requires=REQUIRES)
|
||||||
# TODO ugh. this would import too early
|
# TODO ugh. this would import too early
|
||||||
|
|
Loading…
Add table
Reference in a new issue