From 18621cf9b882723aeabf23801a00d0ff8411a457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sun, 10 Oct 2021 19:46:47 +0200 Subject: [PATCH] add exercise write up class --- scripts/exercises.py | 13 +++++-------- scripts/file_list.py | 28 ++++++++++++++++++++++++---- scripts/window_subprocess.py | 2 +- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/scripts/exercises.py b/scripts/exercises.py index 818d4aa..c683369 100644 --- a/scripts/exercises.py +++ b/scripts/exercises.py @@ -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 diff --git a/scripts/file_list.py b/scripts/file_list.py index 0b93a1e..72334fa 100644 --- a/scripts/file_list.py +++ b/scripts/file_list.py @@ -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): diff --git a/scripts/window_subprocess.py b/scripts/window_subprocess.py index 034891d..a60e785 100644 --- a/scripts/window_subprocess.py +++ b/scripts/window_subprocess.py @@ -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",