From 65efb9c8546b4b646e98ed97c567a2d546265dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 1 Oct 2021 19:08:56 +0200 Subject: [PATCH] add FileHandle class for lists of pdf files (e.g. exercise sheets) --- scripts/file_list.py | 36 +++++++++++++++++++++++ scripts/{edit.py => window_subprocess.py} | 9 ++++++ 2 files changed, 45 insertions(+) create mode 100644 scripts/file_list.py rename scripts/{edit.py => window_subprocess.py} (63%) diff --git a/scripts/file_list.py b/scripts/file_list.py new file mode 100644 index 0000000..0b93a1e --- /dev/null +++ b/scripts/file_list.py @@ -0,0 +1,36 @@ +#!/usr/bin/python3 +import re +import subprocess +import warnings +from pathlib import Path +from window_subprocess import open_pdf + + +class FileHandle: + def __init__(self, file_path: Path): + self.path = file_path + match = re.match(r'[\D]*(\d+)[\D]*\.pdf', file_path.name) + if match is None: + warnings.warn(f'Invalid format in file {str(file_path)}: Could not parse number.') + self.number = -1 + else: + self.number = int(match.group(1)) + + def open(self): + open_pdf(self.path) + + +class Files(list): + def __init__(self, root_path: Path, pattern: str = "*"): + self.root: Path = root_path + list.__init__(self, self.read_files(pattern)) + + def read_files(self, pattern): + files = self.root.glob(pattern) + return sorted((FileHandle(f) for f in files), key=lambda f: f.number) + + def unite_files(self, name): + result = subprocess.run( + ['pdfunite'] + list(map(lambda f: str(f.path), self)) + [str(self.root / name)] + ) + print(result.stdout) diff --git a/scripts/edit.py b/scripts/window_subprocess.py similarity index 63% rename from scripts/edit.py rename to scripts/window_subprocess.py index 87fd440..034891d 100644 --- a/scripts/edit.py +++ b/scripts/window_subprocess.py @@ -13,3 +13,12 @@ def edit(filepath: Path, rootpath: Path = None, env=os.environ): "-e", f"vim --servername tex-vorlesung --remote-silent {str(filepath)}" ], env=env, cwd=str(rootpath)) + + +def open_pdf(filepath: Path): + result = subprocess.run( + ['zathura', str(filepath)], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL + ) + return result.returncode