2019-09-15 20:42:11 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import locale
|
|
|
|
import re
|
|
|
|
import subprocess
|
2021-09-16 13:28:51 +02:00
|
|
|
from datetime import datetime
|
2019-09-15 20:42:11 +02:00
|
|
|
|
2021-09-16 20:07:18 +02:00
|
|
|
from config import DATE_FORMAT, LOCALE, DEFAULT_MASTER_FILE_NAME, DEFAULT_NEW_LECTURE_HEADER
|
|
|
|
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:
|
2019-09-15 20:42:11 +02:00
|
|
|
def __init__(self, file_path, course):
|
|
|
|
with file_path.open() as f:
|
|
|
|
for line in f:
|
2021-09-16 13:28:51 +02:00
|
|
|
lecture_match = re.search(r'lecture{(.*?)}{(.*?)}{(.*)}', 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
|
|
|
|
|
|
|
# number = int(lecture_match.group(1))
|
|
|
|
|
|
|
|
date_str = lecture_match.group(2)
|
|
|
|
date = datetime.strptime(date_str, DATE_FORMAT)
|
|
|
|
week = get_week(date)
|
|
|
|
|
|
|
|
title = lecture_match.group(3)
|
|
|
|
|
|
|
|
self.file_path = file_path
|
|
|
|
self.date = date
|
|
|
|
self.week = week
|
|
|
|
self.number = filename2number(file_path.stem)
|
|
|
|
self.title = title
|
|
|
|
self.course = course
|
|
|
|
|
|
|
|
def edit(self):
|
|
|
|
subprocess.Popen([
|
|
|
|
"x-terminal-emulator",
|
|
|
|
"-e", "zsh", "-i", "-c",
|
|
|
|
f"\\vim --servername kulak --remote-silent {str(self.file_path)}"
|
|
|
|
])
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f'<Lecture {self.course.info["short"]} {self.number} "{self.title}">'
|
|
|
|
|
|
|
|
|
|
|
|
class Lectures(list):
|
2021-09-16 14:59:00 +02:00
|
|
|
def __init__(self, script):
|
|
|
|
self.course = script.course
|
|
|
|
self.script = script
|
|
|
|
if 'lectures' in script.info:
|
|
|
|
self.info = script.info['lectures']
|
|
|
|
else:
|
|
|
|
self.info = []
|
|
|
|
if 'path' in self.info:
|
|
|
|
self.root = script.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 14:59:00 +02:00
|
|
|
self.root = script.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')
|
|
|
|
return sorted((Lecture(f, self.course) for f in files), key=lambda l: l.number)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
new_lecture_path.touch()
|
2021-09-16 20:03:41 +02:00
|
|
|
new_lecture_path.write_text(DEFAULT_NEW_LECTURE_HEADER.format(number=new_lecture_number, date=date))
|
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
|