add FileHandle class for lists of pdf files (e.g. exercise sheets)

This commit is contained in:
Maximilian Keßler 2021-10-01 19:08:56 +02:00
parent f2e0ffc05d
commit 65efb9c854
2 changed files with 45 additions and 0 deletions

36
scripts/file_list.py Normal file
View file

@ -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)

View file

@ -13,3 +13,12 @@ def edit(filepath: Path, rootpath: Path = None, env=os.environ):
"-e", "-e",
f"vim --servername tex-vorlesung --remote-silent {str(filepath)}" f"vim --servername tex-vorlesung --remote-silent {str(filepath)}"
], env=env, cwd=str(rootpath)) ], 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