restructure configuration handling

This commit is contained in:
Maximilian Keßler 2022-07-26 20:45:14 +02:00
parent 28c841e7f4
commit 252e845505
26 changed files with 56 additions and 39 deletions

1
scripts/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
__pycache__

View file

@ -23,7 +23,6 @@ DEFAULT_NEW_LECTURE_HEADER = r'\lecture[]{{{date}}}{{{title}}}'
DEFAULT_NEW_LECTURE_TITLE = 'Untitled' DEFAULT_NEW_LECTURE_TITLE = 'Untitled'
DEFAULT_LECTURE_SEARCH_REGEX = r'lecture.*({\d*})?{(.*?)}{(.*)}' DEFAULT_LECTURE_SEARCH_REGEX = r'lecture.*({\d*})?{(.*?)}{(.*)}'
DEFAULT_IMPORT_INDENTATION = 4 DEFAULT_IMPORT_INDENTATION = 4
FALLBACK_COURSE_INFO_FILE = Path(__file__).parent.resolve() / 'fallback.yaml'
TIMEZONE = pytz.timezone('CET') TIMEZONE = pytz.timezone('CET')
SCHEDULER_DELAY = 60 SCHEDULER_DELAY = 60
DEFAULT_LATEX_COUNTER_AUX_FILE_EXTENSION = '.cnt' DEFAULT_LATEX_COUNTER_AUX_FILE_EXTENSION = '.cnt'

View file

@ -1,30 +0,0 @@
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

View file

@ -0,0 +1,53 @@
import shutil
from pathlib import Path
import os
import sys
# We read a configuration file for university setup that is located
# in $XDG_CONFIG_HOME/university-setup/config.cfg
def get_default_file(name: str) -> Path:
# System installation
f1 = Path('/etc/opt/mkessler/university-setup') / name
# no installation, try relative path
f2 = Path(__file__).parent.parent / 'config' / name
if f1.exists():
return f1
if f2.exists():
return f2
raise FileNotFoundError(f'Default file {name} not found, bad installation.')
# Ensure that config.py and fallback.yaml are present
# in the config directory and returns this directory
def get_config_dir() -> Path:
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'
fallback_file = xdg_config_home / 'university-setup' / 'fallback.yaml'
config_file.parent.mkdir(exist_ok=True, parents=True)
# Copy defaults if not present already
if not config_file.exists():
shutil.copy(get_default_file('config.py'), config_file)
print(f'Initialized default config file at {str(config_file)}.')
if not fallback_file.exists():
shutil.copy(get_default_file('fallback.yaml'), fallback_file)
print(f'Initialized default fallback file at {str(fallback_file)}.')
return config_file.parent.absolute()
FALLBACK_COURSE_INFO_FILE = get_config_dir() / 'university-setup' / 'fallback.yaml'
sys.path.append(str(get_config_dir()))
# Note that IDEs will probably complain about this, since they cannot find the module right now
# they might also flag this as an unused import, but the imported config values
# are in turn imported by all the oder scripts
from config import *
# future: potentially check that config in fact defines all symbols that we need

View file

@ -20,7 +20,7 @@ from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request from google.auth.transport.requests import Request
from courses import Courses from courses import Courses
from config import USERCALENDARID, TIMEZONE, SCHEDULER_DELAY from config_loader import USERCALENDARID, TIMEZONE, SCHEDULER_DELAY
courses = Courses() courses = Courses()

View file

@ -23,13 +23,7 @@ class Course:
f"file in the directory or add the directory to the course ignore file named" f"file in the directory or add the directory to the course ignore file named"
f" '{COURSE_IGNORE_FILE}' in your root directory ({ROOT})") f" '{COURSE_IGNORE_FILE}' in your root directory ({ROOT})")
self.info = {'title': str(path.stem) + ' (unnamed course)'} self.info = {'title': str(path.stem) + ' (unnamed course)'}
if FALLBACK_COURSE_INFO_FILE.is_file(): fallback_file = yaml.safe_load(FALLBACK_COURSE_INFO_FILE.open())
fallback_file = yaml.safe_load(FALLBACK_COURSE_INFO_FILE.open())
else:
warnings.warn(f"No fallback course info file found. Program might crash if your provided info files do not"
f"have the correct file format or are missing specified values. Provide the fallback course"
f"file at {FALLBACK_COURSE_INFO_FILE}.")
fallback_file = {}
self.info = merge_dictionaries(self.info, fallback_file) self.info = merge_dictionaries(self.info, fallback_file)
self._notes = None self._notes = None
self._links = None self._links = None