WIP on using ~/.config/my/my.config

This commit is contained in:
Dima Gerasimov 2020-04-11 20:32:26 +01:00
parent 97896fca1f
commit be7c4d2098
3 changed files with 40 additions and 12 deletions

View file

@ -1,12 +0,0 @@
# TODO how to make it mypy friendly? maybe defensive import? or mypy config? or interface file?
try:
import mycfg
except ImportError:
import warnings
warnings.warn("mycfg package isn't found! That might result in issues")
from . import mycfg_stub as mycfg # type: ignore[no-redef]
import sys
sys.modules['mycfg'] = mycfg
del sys

39
my/init.py Normal file
View file

@ -0,0 +1,39 @@
'''
A hook to insert user's config directory into Python's search path.
- Ideally that would be in __init__.py (so it's executed without having to import explicityly)
But, with namespace packages, we can't have __init__.py in the parent subpackage
(see http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-init-py-trap)
Please let me know if you are aware of a better way of dealing with this!
'''
# separate function to present namespace pollution
def setup_config():
from pathlib import Path
import sys
import warnings
# TODO use appdir??
cfg_dir = Path('~/.config').expanduser()
mycfg_dir = cfg_dir / 'my'
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
else:
mp = str(mycfg_dir)
if mp not in sys.path:
sys.path.insert(0, mp)
print("UPDATED PATH") # TODO FIXME remove
try:
import my.config
except ImportError as ex:
warnings.warn(f"Importing my.config failed! (error: {ex}). This might result in issues.")
setup_config()
del setup_config

View file

@ -37,6 +37,7 @@ def main():
'testing': [ 'testing': [
'pytest', 'pytest',
'pytz', 'pytz',
'pylint',
], ],
}, },
) )