provide default info file and merge found info files with default info.yaml file

This commit is contained in:
Maximilian Keßler 2021-09-17 10:32:28 +02:00
parent 56637a3b16
commit 5a2fa4494e
2 changed files with 16 additions and 6 deletions

View file

@ -14,7 +14,7 @@ ROOT = Path('~/Uni/semester-5').expanduser()
DATE_FORMAT = '%a %d %b %Y' DATE_FORMAT = '%a %d %b %Y'
LOCALE = "de_DE.utf8" LOCALE = "de_DE.utf8"
COURSE_IGNORE_FILE = '.courseignore' COURSE_IGNORE_FILE = '.courseignore'
COURSE_INFO_FILE = 'info.yaml' COURSE_INFO_FILE_NAME = 'info.yaml'
DEFAULT_MASTER_FILE_NAME = 'master.tex' DEFAULT_MASTER_FILE_NAME = 'master.tex'
MAX_LEN = 40 MAX_LEN = 40
LECTURE_START_MARKER = 'start lectures' LECTURE_START_MARKER = 'start lectures'
@ -22,3 +22,4 @@ LECTURE_END_MARKER = 'end lectures'
DEFAULT_NEW_LECTURE_HEADER = r'\lecture[]{{{date}}}{{{title}}}' DEFAULT_NEW_LECTURE_HEADER = r'\lecture[]{{{date}}}{{{title}}}'
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('info.yaml').resolve()

View file

@ -6,20 +6,29 @@ import warnings
from lectures import Lectures from lectures import Lectures
from notes import Notes from notes import Notes
from config import ROOT, CURRENT_COURSE_ROOT, CURRENT_COURSE_SYMLINK, CURRENT_COURSE_WATCH_FILE, COURSE_IGNORE_FILE, \ 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: class Course:
def __init__(self, path): def __init__(self, path):
self.path = path self.path = path
self.name = path.stem self.name = path.stem
if (path / COURSE_INFO_FILE).is_file(): if (path / COURSE_INFO_FILE_NAME).is_file():
self.info = yaml.safe_load((path / COURSE_INFO_FILE).open()) self.info = yaml.safe_load((path / COURSE_INFO_FILE_NAME).open())
else: 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"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': 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 self._notes = None
@property @property