2021-09-16 14:59:00 +02:00
|
|
|
#!/usr/bin/python3
|
2021-09-18 14:39:05 +02:00
|
|
|
import os
|
2021-09-16 14:59:00 +02:00
|
|
|
import subprocess
|
2021-09-16 22:46:31 +02:00
|
|
|
from pathlib import Path
|
2021-09-17 12:35:12 +02:00
|
|
|
from typing import Dict
|
2021-09-16 14:59:00 +02:00
|
|
|
|
2021-09-17 14:11:46 +02:00
|
|
|
from config import LECTURE_START_MARKER, LECTURE_END_MARKER, DEFAULT_IMPORT_INDENTATION, \
|
|
|
|
DEFAULT_LATEX_COUNTER_AUX_FILE_EXTENSION
|
2021-10-01 19:08:36 +02:00
|
|
|
from window_subprocess import edit
|
2021-09-16 14:59:00 +02:00
|
|
|
from lectures import Lectures, number2filename
|
2021-09-17 09:28:43 +02:00
|
|
|
from parse_counters import parse_counters, dict2setcounters
|
2021-09-16 14:59:00 +02:00
|
|
|
|
2021-09-16 18:02:36 +02:00
|
|
|
|
|
|
|
class Notes:
|
2021-09-16 14:59:00 +02:00
|
|
|
def __init__(self, course):
|
2021-09-17 12:35:12 +02:00
|
|
|
self.course = course # A course
|
|
|
|
self.info: Dict = course.info['notes']
|
|
|
|
self.root: Path = course.path / self.info['path']
|
2021-09-17 10:37:47 +02:00
|
|
|
self.root.mkdir(parents=True, exist_ok=True)
|
2021-09-17 12:35:12 +02:00
|
|
|
self.master_file: Path = self.root / self.info['master_file']
|
|
|
|
self.full_file: Path = self.root / self.info['full_file']
|
2021-09-18 14:17:42 +02:00
|
|
|
self.texinputs: Path = self.root / self.info['texinputs']
|
2021-09-16 14:59:00 +02:00
|
|
|
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!
|
2021-09-16 20:03:41 +02:00
|
|
|
if LECTURE_END_MARKER in line:
|
2021-09-16 14:59:00 +02:00
|
|
|
part = 2
|
|
|
|
|
|
|
|
if part == 0:
|
|
|
|
header += line
|
|
|
|
if part == 2:
|
|
|
|
footer += line
|
|
|
|
|
2021-09-16 20:03:41 +02:00
|
|
|
if LECTURE_START_MARKER in line:
|
2021-09-16 14:59:00 +02:00
|
|
|
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])
|
2021-10-27 19:36:40 +02:00
|
|
|
self._lectures = None # This causes the lectures to be re-computed
|
2021-09-16 19:34:16 +02:00
|
|
|
self.update_lectures_in_full(self.lectures.parse_range_string('all'))
|
2021-09-16 14:59:00 +02:00
|
|
|
return lec
|
|
|
|
|
2021-09-17 09:28:43 +02:00
|
|
|
def input_lecture_command(self, num: int):
|
2021-09-16 18:55:10 +02:00
|
|
|
if self.lectures.root.relative_to(self.root) == Path('.'):
|
|
|
|
input_command = r'\input{'
|
|
|
|
else:
|
|
|
|
input_command = r'\import{' + str(self.lectures.root.relative_to(self.root)) + '/}{'
|
2021-09-17 09:32:41 +02:00
|
|
|
return ' ' * DEFAULT_IMPORT_INDENTATION + input_command + number2filename(num) + '}\n'
|
2021-09-17 09:28:43 +02:00
|
|
|
|
|
|
|
def set_counters(self, lecture_list, lec, setcounters=False):
|
|
|
|
if not setcounters:
|
|
|
|
return ''
|
2021-09-17 09:32:41 +02:00
|
|
|
if lec - 1 not in lecture_list and self.full_file:
|
2022-05-01 18:13:10 +02:00
|
|
|
cnt_file = self.full_file.with_suffix(DEFAULT_LATEX_COUNTER_AUX_FILE_EXTENSION)
|
|
|
|
if not cnt_file.exists():
|
|
|
|
cnt_file = self.full_file.parent / 'build' / cnt_file.name
|
2021-09-17 14:11:46 +02:00
|
|
|
return dict2setcounters(parse_counters(
|
2022-05-01 18:13:10 +02:00
|
|
|
cnt_file,
|
2021-09-17 14:11:46 +02:00
|
|
|
{'lecture': lec}
|
|
|
|
))
|
2021-09-17 09:28:43 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def update_lectures_in_file(self, filename, lecture_list, setcounters=False):
|
|
|
|
header, footer = self.get_header_footer(filename)
|
|
|
|
body = ''.join([self.set_counters(lecture_list, num, setcounters) + self.input_lecture_command(num)
|
|
|
|
for num in lecture_list])
|
2021-09-16 19:34:16 +02:00
|
|
|
filename.write_text(header + body + footer)
|
|
|
|
|
|
|
|
def update_lectures_in_master(self, lecture_list):
|
2021-09-17 09:28:43 +02:00
|
|
|
self.update_lectures_in_file(self.master_file, lecture_list, True)
|
2021-09-16 19:34:16 +02:00
|
|
|
|
|
|
|
def update_lectures_in_full(self, lecture_list):
|
|
|
|
if self.full_file:
|
|
|
|
self.update_lectures_in_file(self.full_file, lecture_list)
|
2021-09-16 14:59:00 +02:00
|
|
|
|
2021-09-17 09:43:54 +02:00
|
|
|
def edit_master(self):
|
2021-09-18 14:39:05 +02:00
|
|
|
edit(self.master_file, rootpath=self.root, env=self.environment())
|
2021-09-17 09:43:54 +02:00
|
|
|
|
|
|
|
def edit_full(self):
|
2021-09-18 14:39:05 +02:00
|
|
|
edit(self.full_file, rootpath=self.root, env=self.environment())
|
2021-09-17 09:43:54 +02:00
|
|
|
|
2021-09-17 12:35:12 +02:00
|
|
|
def open_master(self):
|
|
|
|
result = subprocess.run(
|
|
|
|
['zathura', str(self.master_file.with_suffix('.pdf'))],
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL
|
|
|
|
)
|
2021-09-18 13:43:04 +02:00
|
|
|
return result.returncode
|
2021-09-17 12:35:12 +02:00
|
|
|
|
|
|
|
def open_full(self):
|
|
|
|
result = subprocess.run(
|
|
|
|
['zathura', str(self.full_file.with_suffix('.pdf'))],
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL
|
|
|
|
)
|
2021-09-18 13:43:04 +02:00
|
|
|
return result.returncode
|
2021-09-17 12:35:12 +02:00
|
|
|
|
2021-09-18 14:43:18 +02:00
|
|
|
def open_terminal(self):
|
|
|
|
result = subprocess.Popen(
|
|
|
|
['termite'], env=self.environment(), cwd=self.root
|
|
|
|
)
|
|
|
|
|
2021-09-16 14:59:00 +02:00
|
|
|
def compile_master(self):
|
|
|
|
result = subprocess.run(
|
2021-09-18 14:39:05 +02:00
|
|
|
['latexmk', '-f', '-interaction=nonstopmode', '-dvi-', '-pdf', str(self.master_file)],
|
2021-09-16 14:59:00 +02:00
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL,
|
2021-09-18 14:39:05 +02:00
|
|
|
cwd=str(self.root),
|
|
|
|
env=self.environment()
|
2021-09-16 14:59:00 +02:00
|
|
|
)
|
|
|
|
return result.returncode
|
|
|
|
|
2021-09-17 09:43:54 +02:00
|
|
|
def compile_full(self):
|
|
|
|
if not self.full_file:
|
|
|
|
return 0
|
|
|
|
result = subprocess.run(
|
2021-09-18 14:39:05 +02:00
|
|
|
['latexmk', '-f', '-interaction=nonstopmode', '-dvi-', '-pdf', str(self.full_file)],
|
2021-09-17 09:43:54 +02:00
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL,
|
2021-09-18 14:39:05 +02:00
|
|
|
cwd=str(self.root),
|
|
|
|
env=self.environment()
|
2021-09-17 09:43:54 +02:00
|
|
|
)
|
|
|
|
return result.returncode
|
|
|
|
|
2021-09-18 14:39:05 +02:00
|
|
|
def environment(self):
|
|
|
|
env = os.environ
|
|
|
|
env["TEXINPUTS"] = str(self.texinputs) + '//:'
|
|
|
|
return env
|
|
|
|
|
2021-09-16 14:59:00 +02:00
|
|
|
@property
|
|
|
|
def lectures(self):
|
|
|
|
if not self._lectures:
|
|
|
|
self._lectures = Lectures(self)
|
|
|
|
return self._lectures
|