ok, seems that import_dir is a bit saner
This commit is contained in:
parent
44aa062756
commit
0f80e9d5e6
3 changed files with 16 additions and 3 deletions
|
@ -9,6 +9,7 @@ import warnings
|
|||
# some helper functions
|
||||
PathIsh = Union[Path, str]
|
||||
|
||||
# TODO only used in tests? not sure if useful at all.
|
||||
# TODO port annotations to kython?..
|
||||
def import_file(p: PathIsh, name: Optional[str]=None) -> types.ModuleType:
|
||||
p = Path(p)
|
||||
|
@ -33,6 +34,11 @@ def import_from(path: PathIsh, name: str) -> types.ModuleType:
|
|||
sys.path.remove(path)
|
||||
|
||||
|
||||
def import_dir(path: PathIsh) -> types.ModuleType:
|
||||
p = Path(path)
|
||||
return import_from(p.parent, p.name)
|
||||
|
||||
|
||||
T = TypeVar('T')
|
||||
K = TypeVar('K')
|
||||
V = TypeVar('V')
|
||||
|
|
|
@ -24,8 +24,8 @@ class demo(user_config):
|
|||
def external_module(self):
|
||||
rpath = self.external
|
||||
if rpath is not None:
|
||||
from .cfg import set_repo
|
||||
set_repo('external', rpath)
|
||||
from .core.common import import_dir
|
||||
return import_dir(rpath)
|
||||
|
||||
import my.config.repos.external as m # type: ignore
|
||||
return m
|
||||
|
|
|
@ -104,7 +104,14 @@ def prepare(tmp_path: Path):
|
|||
''')
|
||||
ext = tmp_path / 'external'
|
||||
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
|
||||
ex = 'my.config.repos.external'
|
||||
if ex in sys.modules:
|
||||
|
|
Loading…
Add table
Reference in a new issue