university-setup/scripts/lectures.py

131 lines
3.9 KiB
Python
Raw Normal View History

2019-09-15 20:42:11 +02:00
#!/usr/bin/python3
import locale
import os
2019-09-15 20:42:11 +02:00
import re
import warnings
2021-09-16 13:28:51 +02:00
from datetime import datetime
2019-09-15 20:42:11 +02:00
from config import DATE_FORMAT, LOCALE, DEFAULT_NEW_LECTURE_HEADER, DEFAULT_LECTURE_SEARCH_REGEX, \
DEFAULT_NEW_LECTURE_TITLE
from edit import edit
2021-09-17 10:49:48 +02:00
from utils import get_week
2019-09-15 20:42:11 +02:00
# TODO
locale.setlocale(locale.LC_TIME, LOCALE)
2019-09-15 20:42:11 +02:00
def number2filename(n):
return 'lec_{0:02d}.tex'.format(n)
2021-09-16 13:28:51 +02:00
2019-09-15 20:42:11 +02:00
def filename2number(s):
return int(str(s).replace('.tex', '').replace('lec_', ''))
2021-09-16 13:28:51 +02:00
class Lecture:
2021-09-18 14:17:21 +02:00
def __init__(self, file_path, notes):
2019-09-15 20:42:11 +02:00
with file_path.open() as f:
for line in f:
lecture_match = re.search(DEFAULT_LECTURE_SEARCH_REGEX, line)
2019-09-15 20:42:11 +02:00
if lecture_match:
2021-09-16 13:28:51 +02:00
break
2019-09-15 20:42:11 +02:00
if lecture_match:
date_str = lecture_match.group(2)
try:
date = datetime.strptime(date_str, DATE_FORMAT)
except ValueError:
warnings.warn(f"Invalid date format found in lecture file {file_path}. Specify time in format"
f"'{DATE_FORMAT}' that you set in the config.py file.")
date = datetime.min
week = get_week(date)
2019-09-15 20:42:11 +02:00
title = lecture_match.group(3)
else:
date = datetime.min
week = get_week(date)
2019-09-15 20:42:11 +02:00
title = 'Error while parsing lecture file'
2019-09-15 20:42:11 +02:00
self.file_path = file_path
self.date = date
self.week = week
self.number = filename2number(file_path.stem)
self.title = title
2021-09-18 14:17:21 +02:00
self.notes = notes
2019-09-15 20:42:11 +02:00
def edit(self):
2021-09-18 14:17:21 +02:00
edit(self.file_path, rootpath=self.notes.root, texinputs=self.notes.texinputs)
2019-09-15 20:42:11 +02:00
def __str__(self):
return f'<Lecture {self.course.info["short"]} {self.number} "{self.title}">'
class Lectures(list):
def __init__(self, notes):
self.course = notes.course
self.notes = notes
if 'lectures' in notes.info:
self.info = notes.info['lectures']
else:
self.info = []
if 'path' in self.info:
self.root = notes.root / self.info['path']
2021-09-16 18:33:33 +02:00
self.root.mkdir(parents=True, exist_ok=True)
else:
self.root = notes.root
2019-09-15 20:42:11 +02:00
list.__init__(self, self.read_files())
def read_files(self):
files = self.root.glob('lec_*.tex')
2021-09-18 14:17:21 +02:00
return sorted((Lecture(f, self.notes) for f in files), key=lambda l: l.number)
2019-09-15 20:42:11 +02:00
def parse_lecture_spec(self, string):
if len(self) == 0:
return 0
if string.isdigit():
return int(string)
elif string == 'last':
return self[-1].number
elif string == 'prev':
return self[-1].number - 1
def parse_range_string_section(self, arg):
2019-09-15 20:42:11 +02:00
if 'all' in arg:
return [lecture.number for lecture in self]
2019-09-15 20:42:11 +02:00
if '-' in arg:
start, end = [self.parse_lecture_spec(bit) for bit in arg.split('-')]
return list(range(start, end + 1))
2019-09-15 20:42:11 +02:00
return [self.parse_lecture_spec(arg)]
def parse_range_string(self, arg):
all_numbers = [lecture.number for lecture in self]
sets = [set(self.parse_range_string_section(part)) for part in arg.split(',')]
return list(set.union(*sets) & set(all_numbers))
2019-09-15 20:42:11 +02:00
def new_lecture(self):
if len(self) != 0:
new_lecture_number = self[-1].number + 1
else:
new_lecture_number = 1
new_lecture_path = self.root / number2filename(new_lecture_number)
today = datetime.today()
date = today.strftime(DATE_FORMAT)
vimtex_root_str = f"%! TEX root = {str(os.path.relpath(self.notes.master_file, self.root))}\n"
header_str = DEFAULT_NEW_LECTURE_HEADER.format(
number=new_lecture_number, date=date, title=DEFAULT_NEW_LECTURE_TITLE)
2019-09-15 20:42:11 +02:00
new_lecture_path.touch()
new_lecture_path.write_text(vimtex_root_str + header_str)
2019-09-15 20:42:11 +02:00
self.read_files()
2021-09-16 13:28:51 +02:00
lec = Lecture(new_lecture_path, self.course)
2019-09-15 20:42:11 +02:00
2021-09-16 13:28:51 +02:00
return lec