2019-09-15 20:42:11 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import locale
|
2021-09-16 22:46:31 +02:00
|
|
|
import os
|
2019-09-15 20:42:11 +02:00
|
|
|
import re
|
2021-09-17 10:43:14 +02:00
|
|
|
import warnings
|
2021-09-16 13:28:51 +02:00
|
|
|
from datetime import datetime
|
2019-09-15 20:42:11 +02:00
|
|
|
|
2021-09-17 10:45:14 +02:00
|
|
|
from config import DATE_FORMAT, LOCALE, DEFAULT_NEW_LECTURE_HEADER, DEFAULT_LECTURE_SEARCH_REGEX, \
|
|
|
|
DEFAULT_NEW_LECTURE_TITLE
|
2021-09-17 09:43:54 +02:00
|
|
|
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
|
2021-09-16 12:05:44 +02:00
|
|
|
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:
|
2021-09-16 22:46:31 +02:00
|
|
|
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
|
|
|
|
2021-09-17 09:48:16 +02:00
|
|
|
if lecture_match:
|
|
|
|
date_str = lecture_match.group(2)
|
2021-09-17 10:43:14 +02:00
|
|
|
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
|
2021-09-17 09:48:16 +02:00
|
|
|
week = get_week(date)
|
2019-09-15 20:42:11 +02:00
|
|
|
|
2021-09-17 09:48:16 +02:00
|
|
|
title = lecture_match.group(3)
|
|
|
|
else:
|
|
|
|
date = datetime.min
|
|
|
|
week = get_week(date)
|
2019-09-15 20:42:11 +02:00
|
|
|
|
2021-09-17 09:48:16 +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):
|
2021-09-16 20:30:53 +02:00
|
|
|
def __init__(self, notes):
|
|
|
|
self.course = notes.course
|
|
|
|
self.notes = notes
|
|
|
|
if 'lectures' in notes.info:
|
|
|
|
self.info = notes.info['lectures']
|
2021-09-16 14:59:00 +02:00
|
|
|
else:
|
|
|
|
self.info = []
|
|
|
|
if 'path' in self.info:
|
2021-09-16 20:30:53 +02:00
|
|
|
self.root = notes.root / self.info['path']
|
2021-09-16 18:33:33 +02:00
|
|
|
self.root.mkdir(parents=True, exist_ok=True)
|
2021-09-16 13:08:05 +02:00
|
|
|
else:
|
2021-09-16 20:30:53 +02:00
|
|
|
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
|
|
|
|
|
2021-09-16 19:10:04 +02:00
|
|
|
def parse_range_string_section(self, arg):
|
2019-09-15 20:42:11 +02:00
|
|
|
if 'all' in arg:
|
2021-09-16 19:18:30 +02:00
|
|
|
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('-')]
|
2021-09-16 19:18:30 +02:00
|
|
|
return list(range(start, end + 1))
|
2019-09-15 20:42:11 +02:00
|
|
|
|
|
|
|
return [self.parse_lecture_spec(arg)]
|
|
|
|
|
2021-09-16 19:10:04 +02:00
|
|
|
def parse_range_string(self, arg):
|
2021-09-16 19:18:30 +02:00
|
|
|
all_numbers = [lecture.number for lecture in self]
|
2021-09-16 19:10:04 +02:00
|
|
|
sets = [set(self.parse_range_string_section(part)) for part in arg.split(',')]
|
2021-09-16 19:18:30 +02:00
|
|
|
return list(set.union(*sets) & set(all_numbers))
|
2021-09-16 19:10:04 +02:00
|
|
|
|
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)
|
|
|
|
|
2021-09-16 20:30:53 +02:00
|
|
|
vimtex_root_str = f"%! TEX root = {str(os.path.relpath(self.notes.master_file, self.root))}\n"
|
2021-09-17 10:45:14 +02:00
|
|
|
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()
|
2021-09-16 20:30:53 +02:00
|
|
|
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
|