Simplify config discovery: get rid of the hacky stub and reimport proper config automatically

This commit is contained in:
Dima Gerasimov 2020-05-04 22:52:43 +01:00 committed by karlicoss
parent fd224d8c38
commit 636060db57
10 changed files with 35 additions and 25 deletions

View file

@ -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?

View file

@ -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]

7
my/config/__init__.py Normal file
View file

@ -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'

View file

@ -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?

View file

@ -1,4 +0,0 @@
# TODO maybe, reuse mycfg_template here?
class stub:
key = 'value'