ok, seems that import_dir is a bit saner

This commit is contained in:
Dima Gerasimov 2020-05-18 21:04:38 +01:00
parent 44aa062756
commit 0f80e9d5e6
3 changed files with 16 additions and 3 deletions

View file

@ -9,6 +9,7 @@ import warnings
# some helper functions # some helper functions
PathIsh = Union[Path, str] PathIsh = Union[Path, str]
# TODO only used in tests? not sure if useful at all.
# TODO port annotations to kython?.. # TODO port annotations to kython?..
def import_file(p: PathIsh, name: Optional[str]=None) -> types.ModuleType: def import_file(p: PathIsh, name: Optional[str]=None) -> types.ModuleType:
p = Path(p) p = Path(p)
@ -33,6 +34,11 @@ def import_from(path: PathIsh, name: str) -> types.ModuleType:
sys.path.remove(path) sys.path.remove(path)
def import_dir(path: PathIsh) -> types.ModuleType:
p = Path(path)
return import_from(p.parent, p.name)
T = TypeVar('T') T = TypeVar('T')
K = TypeVar('K') K = TypeVar('K')
V = TypeVar('V') V = TypeVar('V')

View file

@ -24,8 +24,8 @@ class demo(user_config):
def external_module(self): def external_module(self):
rpath = self.external rpath = self.external
if rpath is not None: if rpath is not None:
from .cfg import set_repo from .core.common import import_dir
set_repo('external', rpath) return import_dir(rpath)
import my.config.repos.external as m # type: ignore import my.config.repos.external as m # type: ignore
return m return m

View file

@ -104,7 +104,14 @@ def prepare(tmp_path: Path):
''') ''')
ext = tmp_path / 'external' ext = tmp_path / 'external'
ext.mkdir() ext.mkdir()
(ext / '__init__.py').write_text('identity = lambda x: x') (ext / '__init__.py').write_text('''
def identity(x):
from .submodule import hello
hello(x)
return x
''')
(ext / 'submodule.py').write_text('hello = lambda x: print("hello " + str(x))')
yield yield
ex = 'my.config.repos.external' ex = 'my.config.repos.external'
if ex in sys.modules: if ex in sys.modules: