From 252e845505a8c1225032873ec571c8c0910ef388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Tue, 26 Jul 2022 20:45:14 +0200 Subject: [PATCH] restructure configuration handling --- scripts/.gitignore | 1 + .../{default_config.py => config/config.py} | 1 - scripts/{ => config}/fallback.yaml | 0 scripts/config_loader.py | 30 ----------- .../{ => src}/compile_all_full_versions.py | 0 scripts/src/config_loader.py | 53 +++++++++++++++++++ scripts/{ => src}/countdown.py | 2 +- scripts/{ => src}/courses.py | 8 +-- scripts/{ => src}/exercises.py | 0 scripts/{ => src}/file_list.py | 0 scripts/{ => src}/init-all-courses.py | 0 scripts/{ => src}/labels.py | 0 scripts/{ => src}/lectures.py | 0 scripts/{ => src}/links.py | 0 scripts/{ => src}/new-lecture.py | 0 scripts/{ => src}/new-writeup.py | 0 scripts/{ => src}/notes.py | 0 scripts/{ => src}/open.py | 0 scripts/{ => src}/parse_counters.py | 0 scripts/{ => src}/rofi-courses.py | 0 scripts/{ => src}/rofi-exercises.py | 0 scripts/{ => src}/rofi-lectures-view.py | 0 scripts/{ => src}/rofi-lectures.py | 0 scripts/{ => src}/rofi.py | 0 scripts/{ => src}/utils.py | 0 scripts/{ => src}/window_subprocess.py | 0 26 files changed, 56 insertions(+), 39 deletions(-) create mode 100644 scripts/.gitignore rename scripts/{default_config.py => config/config.py} (95%) rename scripts/{ => config}/fallback.yaml (100%) delete mode 100644 scripts/config_loader.py rename scripts/{ => src}/compile_all_full_versions.py (100%) create mode 100644 scripts/src/config_loader.py rename scripts/{ => src}/countdown.py (98%) rename scripts/{ => src}/courses.py (84%) rename scripts/{ => src}/exercises.py (100%) rename scripts/{ => src}/file_list.py (100%) rename scripts/{ => src}/init-all-courses.py (100%) rename scripts/{ => src}/labels.py (100%) rename scripts/{ => src}/lectures.py (100%) rename scripts/{ => src}/links.py (100%) rename scripts/{ => src}/new-lecture.py (100%) rename scripts/{ => src}/new-writeup.py (100%) rename scripts/{ => src}/notes.py (100%) rename scripts/{ => src}/open.py (100%) rename scripts/{ => src}/parse_counters.py (100%) rename scripts/{ => src}/rofi-courses.py (100%) rename scripts/{ => src}/rofi-exercises.py (100%) rename scripts/{ => src}/rofi-lectures-view.py (100%) rename scripts/{ => src}/rofi-lectures.py (100%) rename scripts/{ => src}/rofi.py (100%) rename scripts/{ => src}/utils.py (100%) rename scripts/{ => src}/window_subprocess.py (100%) diff --git a/scripts/.gitignore b/scripts/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/scripts/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/scripts/default_config.py b/scripts/config/config.py similarity index 95% rename from scripts/default_config.py rename to scripts/config/config.py index eea1f19..03eaed4 100644 --- a/scripts/default_config.py +++ b/scripts/config/config.py @@ -23,7 +23,6 @@ DEFAULT_NEW_LECTURE_HEADER = r'\lecture[]{{{date}}}{{{title}}}' DEFAULT_NEW_LECTURE_TITLE = 'Untitled' DEFAULT_LECTURE_SEARCH_REGEX = r'lecture.*({\d*})?{(.*?)}{(.*)}' DEFAULT_IMPORT_INDENTATION = 4 -FALLBACK_COURSE_INFO_FILE = Path(__file__).parent.resolve() / 'fallback.yaml' TIMEZONE = pytz.timezone('CET') SCHEDULER_DELAY = 60 DEFAULT_LATEX_COUNTER_AUX_FILE_EXTENSION = '.cnt' diff --git a/scripts/fallback.yaml b/scripts/config/fallback.yaml similarity index 100% rename from scripts/fallback.yaml rename to scripts/config/fallback.yaml diff --git a/scripts/config_loader.py b/scripts/config_loader.py deleted file mode 100644 index 4129f2e..0000000 --- a/scripts/config_loader.py +++ /dev/null @@ -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 diff --git a/scripts/compile_all_full_versions.py b/scripts/src/compile_all_full_versions.py similarity index 100% rename from scripts/compile_all_full_versions.py rename to scripts/src/compile_all_full_versions.py diff --git a/scripts/src/config_loader.py b/scripts/src/config_loader.py new file mode 100644 index 0000000..3b202e5 --- /dev/null +++ b/scripts/src/config_loader.py @@ -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 diff --git a/scripts/countdown.py b/scripts/src/countdown.py similarity index 98% rename from scripts/countdown.py rename to scripts/src/countdown.py index 0a9f96b..771226e 100755 --- a/scripts/countdown.py +++ b/scripts/src/countdown.py @@ -20,7 +20,7 @@ from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request from courses import Courses -from config import USERCALENDARID, TIMEZONE, SCHEDULER_DELAY +from config_loader import USERCALENDARID, TIMEZONE, SCHEDULER_DELAY courses = Courses() diff --git a/scripts/courses.py b/scripts/src/courses.py similarity index 84% rename from scripts/courses.py rename to scripts/src/courses.py index 0e7f5b2..8380cee 100644 --- a/scripts/courses.py +++ b/scripts/src/courses.py @@ -23,13 +23,7 @@ class Course: f"file in the directory or add the directory to the course ignore file named" f" '{COURSE_IGNORE_FILE}' in your root directory ({ROOT})") 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()) - 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 = {} + fallback_file = yaml.safe_load(FALLBACK_COURSE_INFO_FILE.open()) self.info = merge_dictionaries(self.info, fallback_file) self._notes = None self._links = None diff --git a/scripts/exercises.py b/scripts/src/exercises.py similarity index 100% rename from scripts/exercises.py rename to scripts/src/exercises.py diff --git a/scripts/file_list.py b/scripts/src/file_list.py similarity index 100% rename from scripts/file_list.py rename to scripts/src/file_list.py diff --git a/scripts/init-all-courses.py b/scripts/src/init-all-courses.py similarity index 100% rename from scripts/init-all-courses.py rename to scripts/src/init-all-courses.py diff --git a/scripts/labels.py b/scripts/src/labels.py similarity index 100% rename from scripts/labels.py rename to scripts/src/labels.py diff --git a/scripts/lectures.py b/scripts/src/lectures.py similarity index 100% rename from scripts/lectures.py rename to scripts/src/lectures.py diff --git a/scripts/links.py b/scripts/src/links.py similarity index 100% rename from scripts/links.py rename to scripts/src/links.py diff --git a/scripts/new-lecture.py b/scripts/src/new-lecture.py similarity index 100% rename from scripts/new-lecture.py rename to scripts/src/new-lecture.py diff --git a/scripts/new-writeup.py b/scripts/src/new-writeup.py similarity index 100% rename from scripts/new-writeup.py rename to scripts/src/new-writeup.py diff --git a/scripts/notes.py b/scripts/src/notes.py similarity index 100% rename from scripts/notes.py rename to scripts/src/notes.py diff --git a/scripts/open.py b/scripts/src/open.py similarity index 100% rename from scripts/open.py rename to scripts/src/open.py diff --git a/scripts/parse_counters.py b/scripts/src/parse_counters.py similarity index 100% rename from scripts/parse_counters.py rename to scripts/src/parse_counters.py diff --git a/scripts/rofi-courses.py b/scripts/src/rofi-courses.py similarity index 100% rename from scripts/rofi-courses.py rename to scripts/src/rofi-courses.py diff --git a/scripts/rofi-exercises.py b/scripts/src/rofi-exercises.py similarity index 100% rename from scripts/rofi-exercises.py rename to scripts/src/rofi-exercises.py diff --git a/scripts/rofi-lectures-view.py b/scripts/src/rofi-lectures-view.py similarity index 100% rename from scripts/rofi-lectures-view.py rename to scripts/src/rofi-lectures-view.py diff --git a/scripts/rofi-lectures.py b/scripts/src/rofi-lectures.py similarity index 100% rename from scripts/rofi-lectures.py rename to scripts/src/rofi-lectures.py diff --git a/scripts/rofi.py b/scripts/src/rofi.py similarity index 100% rename from scripts/rofi.py rename to scripts/src/rofi.py diff --git a/scripts/utils.py b/scripts/src/utils.py similarity index 100% rename from scripts/utils.py rename to scripts/src/utils.py diff --git a/scripts/window_subprocess.py b/scripts/src/window_subprocess.py similarity index 100% rename from scripts/window_subprocess.py rename to scripts/src/window_subprocess.py