diff --git a/scripts/config.py b/scripts/config.py index 0c5dd50..af449d4 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -25,4 +25,4 @@ 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' \ No newline at end of file +DEFAULT_LATEX_COUNTER_AUX_FILE_EXTENSION = '.cnt' diff --git a/scripts/courses.py b/scripts/courses.py index 5c9ff63..f607cd3 100644 --- a/scripts/courses.py +++ b/scripts/courses.py @@ -9,6 +9,7 @@ from config import ROOT, CURRENT_COURSE_ROOT, CURRENT_COURSE_SYMLINK, CURRENT_CO from notes import Notes from links import Links from utils import merge_dictionaries +from exercises import Exercises class Course: @@ -32,6 +33,7 @@ class Course: self.info = merge_dictionaries(self.info, fallback_file) self._notes = None self._links = None + self._exercises = None @property def links(self) -> Links: @@ -45,6 +47,12 @@ class Course: self._notes = Notes(self) return self._notes + @property + def exercises(self) -> Exercises: + if not self._exercises: + self._exercises = Exercises(self) + return self._exercises + def __eq__(self, other): if other is None: return False diff --git a/scripts/exercises.py b/scripts/exercises.py new file mode 100644 index 0000000..818d4aa --- /dev/null +++ b/scripts/exercises.py @@ -0,0 +1,78 @@ +from file_list import Files +from pathlib import Path +from typing import Dict + + +class ExerciseWriteUp: + def __init__(self, root_dir: Path, course): + self.root_dir = root_dir + self.course = course + self.number = 1 + + def edit(self): + pass + + +class Exercise: + def __init__(self, course, number: int): + self.course = course + self.number = number + self._writeup = None + self._problem = None + self._solution = None + + @property + def writeup(self): + if not self._writeup: + self._writeup = next((w for w in self.course.writeups if w.number == self.number), None) + return self._writeup + + @property + def problem(self): + if not self._problem: + self._problem = next((p for p in self.course.problems if p.number == self.number), None) + return self._problem + + @property + def solution(self): + if not self._solution: + self._solution = next((s for s in self.course.solutions if s.number == self.number), None) + return self._solution + + +class Exercises(list): + def __init__(self, course): + self.course = course + self.info: Dict = course.info['exercises'] + self.root: Path = course.path / self.info['path'] + self.sheet_root = self.root / self.info['sheets'].strip() + self.solutions_root = self.root / self.info['solutions'].strip() + self.root.mkdir(parents=True, exist_ok=True) + self.sheet_root.mkdir(parents=True, exist_ok=True) + self.solutions_root.mkdir(parents=True, exist_ok=True) + self._solutions = None + self._writeups = None + self._sheets = Files(self.sheet_root) + list.__init__(self, (Exercise(self.course, num) for num in map(lambda s: s.number, self._sheets))) + + @property + def sheets(self): + return self._sheets + + @property + def solutions(self): + if not self._solutions: + self._solutions = Files(self.solutions_root) + return self._solutions + + @property + def writeups(self): + if not self._writeups: + dirs = (d for d in self.root.iterdir() + if d.root is not self.sheet_root.root and d.root is not self.solutions_root.root) + self._writeups = sorted((ExerciseWriteUp(d, self.course) for d in dirs), key=lambda e: e.number) + return self._writeups + + def read_write_up_dirs(self): + dirs = self.root.iterdir() + return sorted((ExerciseWriteUp(d, self.course) for d in dirs if d is not self.sheets), key=lambda e: e.number)