mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 15:48:29 +02:00
Add command to generate course guides, add autocompletion for some commands, create json file with course information & abbreviations
This commit is contained in:
parent
6d7b47fee0
commit
c6958d22f3
3 changed files with 223 additions and 9 deletions
64
data/courses.py
Normal file
64
data/courses.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
import dacite
|
||||
import json
|
||||
|
||||
|
||||
@dataclass
|
||||
class Course:
|
||||
abbreviations: list[str]
|
||||
code: str
|
||||
name: str
|
||||
year: int
|
||||
alt: Optional[str] = None
|
||||
|
||||
|
||||
def load_courses() -> dict[str, Course]:
|
||||
"""Create a list of all courses"""
|
||||
with open("files/courses.json", "r") as file:
|
||||
data = json.load(file)
|
||||
|
||||
courses = {}
|
||||
|
||||
for course_name in data:
|
||||
# Add name into the dict to allow flexibility
|
||||
course_data = data[course_name]
|
||||
course_data["name"] = course_name
|
||||
|
||||
courses[course_name] = dacite.from_dict(data_class=Course, data=course_data)
|
||||
|
||||
return courses
|
||||
|
||||
|
||||
def find_course_from_name(name: str, courses: Optional[dict[str, Course]] = None, case_insensitive: bool = True) -> Optional[Course]:
|
||||
# Allow passing a course dict in to avoid having to create it all the time
|
||||
if courses is None:
|
||||
courses = load_courses()
|
||||
|
||||
if case_insensitive:
|
||||
name = name.lower()
|
||||
|
||||
def _perhaps_lower(inp: str) -> str:
|
||||
"""Cast a string to lowercase if necessary"""
|
||||
if case_insensitive:
|
||||
return inp.lower()
|
||||
|
||||
return inp
|
||||
|
||||
# Iterate over all courses to look for a match
|
||||
for course_name, course in courses.items():
|
||||
# Check name first
|
||||
if _perhaps_lower(course_name) == name:
|
||||
return course
|
||||
|
||||
# Then abbreviations
|
||||
for abbreviation in course.abbreviations:
|
||||
if _perhaps_lower(abbreviation) == name:
|
||||
return course
|
||||
|
||||
# Finally alternative names
|
||||
if course.alt is not None and _perhaps_lower(course.alt) == name:
|
||||
return course
|
||||
|
||||
return None
|
||||
Loading…
Add table
Add a link
Reference in a new issue