refactor files

This commit is contained in:
Maximilian Keßler 2021-09-16 13:28:51 +02:00
parent 6fe7fc4938
commit b8c2f236a4
2 changed files with 33 additions and 29 deletions

View file

@ -3,8 +3,11 @@ from pathlib import Path
import yaml import yaml
from lectures import Lectures from lectures import Lectures
from config import ROOT, CURRENT_COURSE_ROOT, CURRENT_COURSE_SYMLINK, CURRENT_COURSE_WATCH_FILE, COURSE_IGNORE_FILE, COURSE_INFO_FILE from config import ROOT, CURRENT_COURSE_ROOT, CURRENT_COURSE_SYMLINK, CURRENT_COURSE_WATCH_FILE, COURSE_IGNORE_FILE, \
class Course(): COURSE_INFO_FILE
class Course:
def __init__(self, path): def __init__(self, path):
self.path = path self.path = path
self.name = path.stem self.name = path.stem
@ -19,20 +22,12 @@ class Course():
return self._lectures return self._lectures
def __eq__(self, other): def __eq__(self, other):
if other == None: if other is None:
return False return False
return self.path == other.path return self.path == other.path
class Courses(list):
def __init__(self):
list.__init__(self, self.read_files())
def read_files(self): def ignored_courses():
course_directories = [x for x in ROOT.iterdir() if x.is_dir() and not x in self.ignored_courses()]
_courses = [Course(path) for path in course_directories]
return sorted(_courses, key=lambda c: c.name)
def ignored_courses(self):
with open(ROOT / COURSE_IGNORE_FILE) as ignore: with open(ROOT / COURSE_IGNORE_FILE) as ignore:
lines = ignore.readlines() lines = ignore.readlines()
paths = [] paths = []
@ -40,6 +35,17 @@ class Courses(list):
paths.append(ROOT / line.strip()) paths.append(ROOT / line.strip())
return paths return paths
def read_files():
course_directories = [x for x in ROOT.iterdir() if x.is_dir() and x not in ignored_courses()]
_courses = [Course(path) for path in course_directories]
return sorted(_courses, key=lambda c: c.name)
class Courses(list):
def __init__(self):
list.__init__(self, read_files())
@property @property
def current(self): def current(self):
return Course(CURRENT_COURSE_ROOT.resolve()) return Course(CURRENT_COURSE_ROOT.resolve())

View file

@ -1,14 +1,11 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os
from datetime import datetime
from pathlib import Path
import locale import locale
import re import re
import subprocess import subprocess
from datetime import datetime
from config import get_week, DATE_FORMAT, LOCALE, DEFAULT_MASTER_FILE_NAME
from config import get_week, DATE_FORMAT, CURRENT_COURSE_ROOT, LOCALE, DEFAULT_MASTER_FILE_NAME
# TODO # TODO
locale.setlocale(locale.LC_TIME, LOCALE) locale.setlocale(locale.LC_TIME, LOCALE)
@ -17,16 +14,18 @@ locale.setlocale(locale.LC_TIME, LOCALE)
def number2filename(n): def number2filename(n):
return 'lec_{0:02d}.tex'.format(n) return 'lec_{0:02d}.tex'.format(n)
def filename2number(s): def filename2number(s):
return int(str(s).replace('.tex', '').replace('lec_', '')) return int(str(s).replace('.tex', '').replace('lec_', ''))
class Lecture():
class Lecture:
def __init__(self, file_path, course): def __init__(self, file_path, course):
with file_path.open() as f: with file_path.open() as f:
for line in f: for line in f:
lecture_match = re.search(r'lecture\{(.*?)\}\{(.*?)\}\{(.*)\}', line) lecture_match = re.search(r'lecture{(.*?)}{(.*?)}{(.*)}', line)
if lecture_match: if lecture_match:
break; break
# number = int(lecture_match.group(1)) # number = int(lecture_match.group(1))
@ -108,7 +107,7 @@ class Lectures(list):
if 'start lectures' in line: if 'start lectures' in line:
part = 1 part = 1
return (header, footer) return header, footer
def update_lectures_in_master(self, r): def update_lectures_in_master(self, r):
header, footer = self.get_header_footer(self.master_file) header, footer = self.get_header_footer(self.master_file)
@ -137,10 +136,9 @@ class Lectures(list):
self.read_files() self.read_files()
lec = Lecture(new_lecture_path, self.course)
l = Lecture(new_lecture_path, self.course) return lec
return l
def compile_master(self): def compile_master(self):
result = subprocess.run( result = subprocess.run(