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 typing import Dict
class ExerciseWriteUp:
class ExerciseWriteUp(FileHandle):
def __init__(self, root_dir: Path, course):
self.root_dir = root_dir
self.course = course
self.number = 1
def edit(self):
pass
FileHandle.__init__(self, next(self.root_dir.rglob('*.tex')), FileType.tex)
class Exercise:
@ -53,6 +50,7 @@ class Exercises(list):
self._solutions = None
self._writeups = None
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)))
@property
@ -68,8 +66,7 @@ class Exercises(list):
@property
def writeups(self):
if not self._writeups:
dirs = (d for d in self.root.iterdir()
if d.root is not self.sheet_root.root and d.root is not self.solutions_root.root)
dirs = list(d for d in self.root.iterdir() if d.is_dir() and d not in self.ignored_folders)
self._writeups = sorted((ExerciseWriteUp(d, self.course) for d in dirs), key=lambda e: e.number)
return self._writeups

View File

@ -3,13 +3,20 @@ import re
import subprocess
import warnings
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:
def __init__(self, file_path: Path):
def __init__(self, file_path: Path, file_type: FileType = FileType.pdf):
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:
warnings.warn(f'Invalid format in file {str(file_path)}: Could not parse number.')
self.number = -1
@ -17,7 +24,20 @@ class FileHandle:
self.number = int(match.group(1))
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):

View File

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