From 4a83ff48645fcfec4ce20066b2f72295e683fd0f Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 19 Apr 2020 12:35:45 +0100 Subject: [PATCH] hacks to work around python3.6 imports; add set_repo --- my/cfg.py | 8 ++++++++ my/init.py | 14 +++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/my/cfg.py b/my/cfg.py index f339b98..beb8243 100644 --- a/my/cfg.py +++ b/my/cfg.py @@ -17,3 +17,11 @@ After that, you can set config attributes: from . import init import my.config as config + + +def set_repo(name: str, repo): + from .init import assign_module + from . common import import_from + + module = import_from(repo, name) + assign_module('my.config.repos', name, module) diff --git a/my/init.py b/my/init.py index e923d42..9fb54ac 100644 --- a/my/init.py +++ b/my/init.py @@ -9,6 +9,18 @@ A hook to insert user's config directory into Python's search path. ''' +# TODO not ideal to keep it here, but this should really be a leaf in the import tree +def assign_module(parent: str, name: str, module): + import sys + import importlib + parent_module = importlib.import_module(parent) + sys.modules[parent + '.' + name] = module + if sys.version_info.minor == 6: + # ugh. not sure why it's necessary in py36... + # TODO that crap should be tested... I guess will get it for free when I run rest of tests in the matrix + setattr(parent_module, name, module) + + # separate function to present namespace pollution def setup_config(): from pathlib import Path @@ -31,7 +43,7 @@ def setup_config(): if not mycfg_dir.exists(): warnings.warn(f"my.config package isn't found! (expected at {mycfg_dir}). This might result in issues.") from . import mycfg_stub as mycfg - sys.modules['my.config'] = mycfg + assign_module('my', 'config', mycfg) else: mp = str(mycfg_dir) if mp not in sys.path: