From 5a2fa4494e3ccc2bbfb5c5c3601ff108372b10c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 17 Sep 2021 10:32:28 +0200 Subject: [PATCH] provide default info file and merge found info files with default info.yaml file --- scripts/config.py | 3 ++- scripts/courses.py | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/scripts/config.py b/scripts/config.py index 94e3ef3..ac44415 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -14,7 +14,7 @@ ROOT = Path('~/Uni/semester-5').expanduser() DATE_FORMAT = '%a %d %b %Y' LOCALE = "de_DE.utf8" COURSE_IGNORE_FILE = '.courseignore' -COURSE_INFO_FILE = 'info.yaml' +COURSE_INFO_FILE_NAME = 'info.yaml' DEFAULT_MASTER_FILE_NAME = 'master.tex' MAX_LEN = 40 LECTURE_START_MARKER = 'start lectures' @@ -22,3 +22,4 @@ LECTURE_END_MARKER = 'end lectures' DEFAULT_NEW_LECTURE_HEADER = r'\lecture[]{{{date}}}{{{title}}}' DEFAULT_LECTURE_SEARCH_REGEX = r'lecture.*({\d*})?{(.*?)}{(.*)}' DEFAULT_IMPORT_INDENTATION = 4 +FALLBACK_COURSE_INFO_FILE = Path('info.yaml').resolve() \ No newline at end of file diff --git a/scripts/courses.py b/scripts/courses.py index d47431c..1eb10e0 100644 --- a/scripts/courses.py +++ b/scripts/courses.py @@ -6,20 +6,29 @@ import warnings from lectures import Lectures from notes import Notes from config import ROOT, CURRENT_COURSE_ROOT, CURRENT_COURSE_SYMLINK, CURRENT_COURSE_WATCH_FILE, COURSE_IGNORE_FILE, \ - COURSE_INFO_FILE + COURSE_INFO_FILE_NAME, FALLBACK_COURSE_INFO_FILE +from utils import merge_dictionaries class Course: def __init__(self, path): self.path = path self.name = path.stem - if (path / COURSE_INFO_FILE).is_file(): - self.info = yaml.safe_load((path / COURSE_INFO_FILE).open()) + if (path / COURSE_INFO_FILE_NAME).is_file(): + self.info = yaml.safe_load((path / COURSE_INFO_FILE_NAME).open()) else: - warnings.warn(f"No course info file found in directory '{path.stem}'. Place a {COURSE_INFO_FILE} " + warnings.warn(f"No course info file found in directory '{path.stem}'. Place a {COURSE_INFO_FILE_NAME} " 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': path.stem, 'short': path.stem} + 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 = {} + self.info = merge_dictionaries(self.info, fallback_file) self._notes = None @property