~kris/dots

srice

ref: e9b48d06a8541f3eda5c4db90382ab3c77183afb srice/.local/bin/academic/courses.py -rw-r--r-- 2.5 KiB
e9b48d06 — Kris Yotam xprofile: systemd-aware pipewire start + blueman-applet; sb-internet: tolerate missing /proc/net/wireless 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
"""
===============================================================
 Script: courses.py
 Author: Kris Yotam (aka. khr1st)
 Date:   2025-09-29
 License: MIT License
 Description:
   Defines Course and Courses abstractions.
   - Course: represents one LaTeX course (metadata + lectures).
   - Courses: collection of Course objects with support for
     setting the "current" course via symlink and watch file.

 Inspiration:
   Adapted and extended from Gilles Castel's lecture note workflow.
===============================================================
"""

from pathlib import Path
import yaml

from lectures import Lectures
from config import (
    ROOT,
    CURRENT_COURSE_ROOT,
    CURRENT_COURSE_SYMLINK,
    CURRENT_COURSE_WATCH_FILE,
)


# -----------------------------
# Course object
# -----------------------------

class Course:
    def __init__(self, path: Path):
        self.path = path
        self.name = path.stem

        # Load metadata from info.yaml
        with (path / "info.yaml").open() as f:
            self.info = yaml.safe_load(f)

        self._lectures = None

    @property
    def lectures(self) -> Lectures:
        """
        Lazily initialize lectures for this course.
        """
        if not self._lectures:
            self._lectures = Lectures(self)
        return self._lectures

    def __eq__(self, other) -> bool:
        """
        Courses are equal if their paths match.
        """
        if other is None:
            return False
        return self.path == other.path


# -----------------------------
# Courses collection
# -----------------------------

class Courses(list):
    def __init__(self):
        super().__init__(self.read_files())

    def read_files(self):
        """
        Scan ROOT for course directories and return Course objects.
        """
        course_dirs = [x for x in ROOT.iterdir() if x.is_dir()]
        courses = [Course(path) for path in course_dirs]
        return sorted(courses, key=lambda c: c.name)

    @property
    def current(self) -> Course:
        """
        Return the currently active course.
        """
        return Course(CURRENT_COURSE_ROOT.resolve())

    @current.setter
    def current(self, course: Course):
        """
        Set the current course by updating symlink and watch file.
        """
        if CURRENT_COURSE_SYMLINK.exists() or CURRENT_COURSE_SYMLINK.is_symlink():
            CURRENT_COURSE_SYMLINK.unlink()

        CURRENT_COURSE_SYMLINK.symlink_to(course.path)
        CURRENT_COURSE_WATCH_FILE.write_text(f"{course.info['short']}\n")