Simplify config discovery: get rid of the hacky stub and reimport proper config automatically
This commit is contained in:
parent
fd224d8c38
commit
636060db57
10 changed files with 35 additions and 25 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -158,3 +158,6 @@ dmypy.json
|
||||||
.pyre/
|
.pyre/
|
||||||
|
|
||||||
# End of https://www.gitignore.io/api/python,emacs
|
# End of https://www.gitignore.io/api/python,emacs
|
||||||
|
|
||||||
|
cov/
|
||||||
|
*.png
|
||||||
|
|
11
lint
11
lint
|
@ -35,6 +35,7 @@ def package_name(p: Path) -> str:
|
||||||
def core_modules() -> Iterable[str]:
|
def core_modules() -> Iterable[str]:
|
||||||
return [
|
return [
|
||||||
'my.common',
|
'my.common',
|
||||||
|
'my.config',
|
||||||
'my.core',
|
'my.core',
|
||||||
'my.cfg',
|
'my.cfg',
|
||||||
'my.error',
|
'my.error',
|
||||||
|
@ -61,20 +62,12 @@ def pylint():
|
||||||
|
|
||||||
|
|
||||||
def mypy(thing: str):
|
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'
|
is_package = Path(thing).suffix != '.py'
|
||||||
return run([
|
return run([
|
||||||
'mypy',
|
'mypy',
|
||||||
'--color-output', # TODO eh? doesn't work..
|
'--color-output', # TODO eh? doesn't work..
|
||||||
*(['-p'] if is_package else []), thing,
|
*(['-p'] if is_package else []), thing,
|
||||||
], stdout=PIPE, stderr=PIPE, env=env)
|
], stdout=PIPE, stderr=PIPE)
|
||||||
|
|
||||||
|
|
||||||
def mypy_all() -> Iterable[Exception]:
|
def mypy_all() -> Iterable[Exception]:
|
||||||
|
|
|
@ -25,3 +25,6 @@ def set_repo(name: str, repo):
|
||||||
|
|
||||||
module = import_from(repo, name)
|
module = import_from(repo, name)
|
||||||
assign_module('my.config.repos', name, module)
|
assign_module('my.config.repos', name, module)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO set_repo is still useful, but perhaps move this thing away to core?
|
||||||
|
|
|
@ -5,8 +5,6 @@ import types
|
||||||
from typing import Union, Callable, Dict, Iterable, TypeVar, Sequence, List, Optional, Any, cast, Tuple
|
from typing import Union, Callable, Dict, Iterable, TypeVar, Sequence, List, Optional, Any, cast, Tuple
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from . import init
|
|
||||||
|
|
||||||
# some helper functions
|
# some helper functions
|
||||||
PathIsh = Union[Path, str]
|
PathIsh = Union[Path, str]
|
||||||
|
|
||||||
|
|
7
my/config/__init__.py
Normal file
7
my/config/__init__.py
Normal 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'
|
24
my/init.py
24
my/init.py
|
@ -38,22 +38,26 @@ def setup_config():
|
||||||
cfg_dir = Path('~/.config').expanduser()
|
cfg_dir = Path('~/.config').expanduser()
|
||||||
mycfg_dir = cfg_dir / 'my'
|
mycfg_dir = cfg_dir / 'my'
|
||||||
|
|
||||||
# TODO maybe try importing first and if it's present, don't do anything?
|
|
||||||
|
|
||||||
if not mycfg_dir.exists():
|
if not mycfg_dir.exists():
|
||||||
warnings.warn(f"my.config package isn't found! (expected at {mycfg_dir}). This might result in issues.")
|
warnings.warn(f"my.config package isn't found! (expected at {mycfg_dir}). This is likely to result in issues.")
|
||||||
from . import mycfg_stub as mycfg
|
return
|
||||||
assign_module('my', 'config', mycfg)
|
|
||||||
else:
|
|
||||||
mp = str(mycfg_dir)
|
|
||||||
if mp not in sys.path:
|
|
||||||
sys.path.insert(0, mp)
|
|
||||||
|
|
||||||
|
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:
|
try:
|
||||||
import my.config
|
import my.config
|
||||||
except ImportError as ex:
|
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()
|
setup_config()
|
||||||
del setup_config
|
del setup_config
|
||||||
|
|
||||||
|
# TODO move to my.core?
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
# TODO maybe, reuse mycfg_template here?
|
|
||||||
|
|
||||||
class stub:
|
|
||||||
key = 'value'
|
|
1
setup.py
1
setup.py
|
@ -35,6 +35,7 @@ def main():
|
||||||
# TODO hmm maybe not necessary anymore?
|
# TODO hmm maybe not necessary anymore?
|
||||||
# empty dir, necessary for proper dynamic imports
|
# empty dir, necessary for proper dynamic imports
|
||||||
'mycfg_stub/repos/.gitkeep',
|
'mycfg_stub/repos/.gitkeep',
|
||||||
|
# TODO might need fixing (empty dir)
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,11 @@ def setup_notes_path(notes: Path) -> None:
|
||||||
config.orgmode = SimpleNamespace( # type: ignore[misc,assignment]
|
config.orgmode = SimpleNamespace( # type: ignore[misc,assignment]
|
||||||
roots=[notes],
|
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:
|
def test_dynamic_configuration(notes: Path) -> None:
|
||||||
|
|
Loading…
Add table
Reference in a new issue