university-setup/scripts/script.py
Maximilian Keßler 13b4852051 introduce script class
A course now does not handle its lectures directly, but has a
'script' property. The script essentially works as the course before,
except that some methods have been moved from the lectures class to the
script class as well.

This ensures further modulation of the course class, to introduce e.g.
an exercise class in the future that a course can also have.

Additionally, with this come further configurations in the info.yaml
file so that the script can have a different relative path from the
course, and the lectures a relative path from the script.
2021-09-16 14:59:00 +02:00

74 lines
2.2 KiB
Python

#!/usr/bin/python3
from pathlib import Path
import subprocess
from lectures import Lectures, number2filename
from config import *
class Script:
def __init__(self, course):
self.course = course
if 'script' in course.info:
self.info = course.info['script']
else:
self.info = []
if 'path' in self.info:
self.root = course.path / self.info['path']
else:
self.root = course.path
if 'master_file' in self.info:
self.master_file = self.root / self.info['master_file']
else:
self.master_file = self.root / DEFAULT_MASTER_FILE_NAME
self._lectures = None
@staticmethod
def get_header_footer(filepath):
part = 0
header = ''
footer = ''
with filepath.open() as f:
for line in f:
# order of if-statements is important here!
if 'end lectures' in line:
part = 2
if part == 0:
header += line
if part == 2:
footer += line
if 'start lectures' in line:
part = 1
return header, footer
def new_lecture(self):
lec = self.lectures.new_lecture()
if lec.number == 1:
self.update_lectures_in_master([1])
else:
self.update_lectures_in_master([lec.number - 1, lec.number])
return lec
def update_lectures_in_master(self, r):
header, footer = self.get_header_footer(self.master_file)
body = ''.join(
' ' * 4 + r'\input{' + number2filename(number) + '}\n' for number in r)
self.master_file.write_text(header + body + footer)
def compile_master(self):
result = subprocess.run(
['latexmk', '-f', '-interaction=nonstopmode', str(self.master_file)],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
cwd=str(self.root)
)
return result.returncode
@property
def lectures(self):
if not self._lectures:
self._lectures = Lectures(self)
return self._lectures