From 636060db57c2cb7453af6bd60aea43c9de32299e Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Mon, 4 May 2020 22:52:43 +0100 Subject: [PATCH] Simplify config discovery: get rid of the hacky stub and reimport proper config automatically --- .gitignore | 3 +++ lint | 11 ++--------- my/cfg.py | 3 +++ my/common.py | 2 -- my/config/__init__.py | 7 +++++++ my/{mycfg_stub => config}/repos/.gitkeep | 0 my/init.py | 24 ++++++++++++++---------- my/mycfg_stub/__init__.py | 4 ---- setup.py | 1 + tests/config.py | 5 +++++ 10 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 my/config/__init__.py rename my/{mycfg_stub => config}/repos/.gitkeep (100%) delete mode 100644 my/mycfg_stub/__init__.py diff --git a/.gitignore b/.gitignore index 7f13069..888867a 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,6 @@ dmypy.json .pyre/ # End of https://www.gitignore.io/api/python,emacs + +cov/ +*.png diff --git a/lint b/lint index 95c7e50..5d91b6e 100755 --- a/lint +++ b/lint @@ -35,6 +35,7 @@ def package_name(p: Path) -> str: def core_modules() -> Iterable[str]: return [ 'my.common', + 'my.config', 'my.core', 'my.cfg', 'my.error', @@ -61,20 +62,12 @@ def pylint(): def mypy(thing: str): - my_config_stub = DIR / 'mycfg_template'; assert my_config_stub.is_dir(), my_config_stub - env = {**os.environ} - MP = 'MYPYPATH' - if CI: # patch up, because CI doesn't have config... meh, but does the trick for now - mypypath = env.get(MP, None) - mypypath = str(my_config_stub) + ('' if mypypath is None else ':' + str(mypypath)) - env[MP] = mypypath - is_package = Path(thing).suffix != '.py' return run([ 'mypy', '--color-output', # TODO eh? doesn't work.. *(['-p'] if is_package else []), thing, - ], stdout=PIPE, stderr=PIPE, env=env) + ], stdout=PIPE, stderr=PIPE) def mypy_all() -> Iterable[Exception]: diff --git a/my/cfg.py b/my/cfg.py index beb8243..ddc102f 100644 --- a/my/cfg.py +++ b/my/cfg.py @@ -25,3 +25,6 @@ def set_repo(name: str, repo): module = import_from(repo, name) assign_module('my.config.repos', name, module) + + +# TODO set_repo is still useful, but perhaps move this thing away to core? diff --git a/my/common.py b/my/common.py index 2ce3d38..063f555 100644 --- a/my/common.py +++ b/my/common.py @@ -5,8 +5,6 @@ import types from typing import Union, Callable, Dict, Iterable, TypeVar, Sequence, List, Optional, Any, cast, Tuple import warnings -from . import init - # some helper functions PathIsh = Union[Path, str] diff --git a/my/config/__init__.py b/my/config/__init__.py new file mode 100644 index 0000000..da9d781 --- /dev/null +++ b/my/config/__init__.py @@ -0,0 +1,7 @@ +# TODO ok, this thing should trigger .cfg import presumably?? +from .. import init + +# TODO maybe, reuse mycfg_template here? + +class stub: + key = 'value' diff --git a/my/mycfg_stub/repos/.gitkeep b/my/config/repos/.gitkeep similarity index 100% rename from my/mycfg_stub/repos/.gitkeep rename to my/config/repos/.gitkeep diff --git a/my/init.py b/my/init.py index 9fb54ac..8c54cf2 100644 --- a/my/init.py +++ b/my/init.py @@ -38,22 +38,26 @@ def setup_config(): cfg_dir = Path('~/.config').expanduser() mycfg_dir = cfg_dir / 'my' - # TODO maybe try importing first and if it's present, don't do anything? - 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 - assign_module('my', 'config', mycfg) - else: - mp = str(mycfg_dir) - if mp not in sys.path: - sys.path.insert(0, mp) + warnings.warn(f"my.config package isn't found! (expected at {mycfg_dir}). This is likely to result in issues.") + return + mpath = str(mycfg_dir) + if mpath not in sys.path: + sys.path.insert(0, mpath) + + # remove the stub and insert reimport hte 'real' config + if 'my.config' in sys.modules: + # TODO FIXME make sure this method isn't called twice... + del sys.modules['my.config'] try: import my.config except ImportError as ex: - warnings.warn(f"Importing my.config failed! (error: {ex}). This might result in issues.") + # just in case... who knows what crazy setup users have in mind. + warnings.warn(f"Importing my.config failed! (error: {ex}). This is likely to result in issues.") setup_config() del setup_config + +# TODO move to my.core? diff --git a/my/mycfg_stub/__init__.py b/my/mycfg_stub/__init__.py deleted file mode 100644 index a1438fc..0000000 --- a/my/mycfg_stub/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -# TODO maybe, reuse mycfg_template here? - -class stub: - key = 'value' diff --git a/setup.py b/setup.py index 991ea2a..b8f947d 100644 --- a/setup.py +++ b/setup.py @@ -35,6 +35,7 @@ def main(): # TODO hmm maybe not necessary anymore? # empty dir, necessary for proper dynamic imports 'mycfg_stub/repos/.gitkeep', + # TODO might need fixing (empty dir) ], }, diff --git a/tests/config.py b/tests/config.py index 8f6f5ad..31e6f59 100644 --- a/tests/config.py +++ b/tests/config.py @@ -9,6 +9,11 @@ def setup_notes_path(notes: Path) -> None: config.orgmode = SimpleNamespace( # type: ignore[misc,assignment] roots=[notes], ) + # TODO FIXME ugh. this belongs to tz provider or global config or someting + import pytz + config.weight = SimpleNamespace( # type: ignore[misc,assignment] + default_timezone = pytz.timezone('Europe/London') + ) def test_dynamic_configuration(notes: Path) -> None: