university-setup/scripts/config_loader.py

31 lines
1.1 KiB
Python

import shutil
from pathlib import Path
import importlib.util
import os
import sys
# We read a configuration file for university setup that is located
# in $XDG_CONFIG_HOME/university-setup/config.cfg
# We follow standard configparser procedure
def get_config_dir() -> str:
if 'XDG_CONFIG_HOME' in os.environ.keys():
xdg_config_home = Path(os.environ['XDG_CONFIG_HOME']).resolve()
else:
xdg_config_home = Path('~/.config').expanduser().resolve()
config_file = xdg_config_home / 'university-setup' / 'config.py'
if not config_file.exists():
config_file.parent.mkdir(exist_ok=True, parents=True)
shutil.copy(Path(__file__).with_name('default_config.py'), config_file)
print(f'Initialized default config file at {str(config_file)}.')
return str(config_file.parent.absolute())
sys.path.append(get_config_dir())
# Note that IDEs will probably complain about this, since they cannot find the module right now
# This is because the module is in fact user-supplied
from config import *
# future: potentially check that config in fact defines all symbols that we need