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

3
.gitignore vendored
View file

@ -158,3 +158,6 @@ dmypy.json
.pyre/
# End of https://www.gitignore.io/api/python,emacs
cov/
*.png

11
lint
View file

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

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'

View file

@ -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)
],
},

View file

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