add exercise write up class

This commit is contained in:
Maximilian Keßler 2021-10-10 19:46:47 +02:00
parent e5c40a54a0
commit 18621cf9b8
3 changed files with 30 additions and 13 deletions

View file

@ -1,16 +1,13 @@
from file_list import Files from file_list import Files, FileHandle, FileType
from pathlib import Path from pathlib import Path
from typing import Dict from typing import Dict
class ExerciseWriteUp: class ExerciseWriteUp(FileHandle):
def __init__(self, root_dir: Path, course): def __init__(self, root_dir: Path, course):
self.root_dir = root_dir self.root_dir = root_dir
self.course = course self.course = course
self.number = 1 FileHandle.__init__(self, next(self.root_dir.rglob('*.tex')), FileType.tex)
def edit(self):
pass
class Exercise: class Exercise:
@ -53,6 +50,7 @@ class Exercises(list):
self._solutions = None self._solutions = None
self._writeups = None self._writeups = None
self._sheets = Files(self.sheet_root) self._sheets = Files(self.sheet_root)
self.ignored_folders = [self.sheet_root, self.solutions_root]
list.__init__(self, (Exercise(self.course, num) for num in map(lambda s: s.number, self._sheets))) list.__init__(self, (Exercise(self.course, num) for num in map(lambda s: s.number, self._sheets)))
@property @property
@ -68,8 +66,7 @@ class Exercises(list):
@property @property
def writeups(self): def writeups(self):
if not self._writeups: if not self._writeups:
dirs = (d for d in self.root.iterdir() dirs = list(d for d in self.root.iterdir() if d.is_dir() and d not in self.ignored_folders)
if d.root is not self.sheet_root.root and d.root is not self.solutions_root.root)
self._writeups = sorted((ExerciseWriteUp(d, self.course) for d in dirs), key=lambda e: e.number) self._writeups = sorted((ExerciseWriteUp(d, self.course) for d in dirs), key=lambda e: e.number)
return self._writeups return self._writeups

View file

@ -3,13 +3,20 @@ import re
import subprocess import subprocess
import warnings import warnings
from pathlib import Path from pathlib import Path
from window_subprocess import open_pdf from window_subprocess import open_pdf, edit
from enum import Enum
class FileType(Enum):
pdf = 'pdf'
tex = 'tex'
class FileHandle: class FileHandle:
def __init__(self, file_path: Path): def __init__(self, file_path: Path, file_type: FileType = FileType.pdf):
self.path = file_path self.path = file_path
match = re.match(r'[\D]*(\d+)[\D]*\.pdf', file_path.name) self.file_type = file_type
match = re.match(r'[\D]*(\d+)[\D]*\.' + file_type.value, file_path.name)
if match is None: if match is None:
warnings.warn(f'Invalid format in file {str(file_path)}: Could not parse number.') warnings.warn(f'Invalid format in file {str(file_path)}: Could not parse number.')
self.number = -1 self.number = -1
@ -17,7 +24,20 @@ class FileHandle:
self.number = int(match.group(1)) self.number = int(match.group(1))
def open(self): def open(self):
open_pdf(self.path) if self.file_type == FileType.pdf:
open_pdf(self.path)
return 0
elif self.file_type == FileType.tex:
edit(self.path)
return 0
return 1
def edit(self):
if self.file_type == FileType.tex:
edit(self.path)
return 0
else:
return 1
class Files(list): class Files(list):

View file

@ -7,7 +7,7 @@ import os
def edit(filepath: Path, rootpath: Path = None, env=os.environ): def edit(filepath: Path, rootpath: Path = None, env=os.environ):
if not rootpath: if not rootpath:
rootpath = filepath rootpath = filepath.root
subprocess.Popen([ subprocess.Popen([
"termite", "termite",
"-e", "-e",