university-setup/scripts/courses.py
Maximilian Keßler 13b4852051 introduce script class
A course now does not handle its lectures directly, but has a
'script' property. The script essentially works as the course before,
except that some methods have been moved from the lectures class to the
script class as well.

This ensures further modulation of the course class, to introduce e.g.
an exercise class in the future that a course can also have.

Additionally, with this come further configurations in the info.yaml
file so that the script can have a different relative path from the
course, and the lectures a relative path from the script.
2021-09-16 14:59:00 +02:00

59 lines
1.5 KiB
Python
Executable File

#!/usr/bin/python3
from pathlib import Path
import yaml
from lectures import Lectures
from script import Script
from config import ROOT, CURRENT_COURSE_ROOT, CURRENT_COURSE_SYMLINK, CURRENT_COURSE_WATCH_FILE, COURSE_IGNORE_FILE, \
COURSE_INFO_FILE
class Course:
def __init__(self, path):
self.path = path
self.name = path.stem
self.info = yaml.safe_load((path / COURSE_INFO_FILE).open())
self._script = None
@property
def script(self):
if not self._script:
self._script = Script(self)
return self._script
def __eq__(self, other):
if other is None:
return False
return self.path == other.path
def ignored_courses():
with open(ROOT / COURSE_IGNORE_FILE) as ignore:
lines = ignore.readlines()
paths = []
for line in lines:
paths.append(ROOT / line.strip())
return paths
def read_files():
course_directories = [x for x in ROOT.iterdir() if x.is_dir() and x not in ignored_courses()]
_courses = [Course(path) for path in course_directories]
return sorted(_courses, key=lambda c: c.name)
class Courses(list):
def __init__(self):
list.__init__(self, read_files())
@property
def current(self):
return Course(CURRENT_COURSE_ROOT.resolve())
@current.setter
def current(self, course):
CURRENT_COURSE_SYMLINK.unlink()
CURRENT_COURSE_SYMLINK.symlink_to(course.path)
CURRENT_COURSE_WATCH_FILE.write_text('{}\n'.format(course.info['short']))