mirror of
https://github.com/Garmelon/PFERD.git
synced 2023-12-21 10:23:01 +01:00
Add and use utility functions for changing paths
This fixes a small bug in the example config, where some files were put in the wrong locations.
This commit is contained in:
parent
a084b05433
commit
8b1a34233a
@ -3,6 +3,8 @@ import pathlib
|
|||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"get_base_dir",
|
"get_base_dir",
|
||||||
|
"move",
|
||||||
|
"rename",
|
||||||
"stream_to_path",
|
"stream_to_path",
|
||||||
"OutOfTriesException",
|
"OutOfTriesException",
|
||||||
"UnknownFileTypeException",
|
"UnknownFileTypeException",
|
||||||
@ -12,6 +14,14 @@ __all__ = [
|
|||||||
def get_base_dir(script_file):
|
def get_base_dir(script_file):
|
||||||
return pathlib.Path(os.path.dirname(os.path.abspath(script_file)))
|
return pathlib.Path(os.path.dirname(os.path.abspath(script_file)))
|
||||||
|
|
||||||
|
def move(path, from_folders, to_folders):
|
||||||
|
l = len(from_folders)
|
||||||
|
if path.parts[:l] == from_folders:
|
||||||
|
return pathlib.PurePath(*to_folders, *path.parts[l:])
|
||||||
|
|
||||||
|
def rename(path, to_name):
|
||||||
|
return pathlib.PurePath(*path.parts[:-1], to_name)
|
||||||
|
|
||||||
async def stream_to_path(resp, to_path, chunk_size=1024**2):
|
async def stream_to_path(resp, to_path, chunk_size=1024**2):
|
||||||
with open(to_path, 'wb') as fd:
|
with open(to_path, 'wb') as fd:
|
||||||
while True:
|
while True:
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#!/bin/env python3
|
||||||
|
|
||||||
import PFERD
|
import PFERD
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
@ -11,123 +13,124 @@ base_dir = PFERD.get_base_dir(__file__)
|
|||||||
|
|
||||||
def gbi_filter(path):
|
def gbi_filter(path):
|
||||||
# Tutorien rausfiltern
|
# Tutorien rausfiltern
|
||||||
if path.match("Tutoriumsfolien/Tutorium 15"): return True
|
if path.parts[:1] == ("Tutoriumsfolien",):
|
||||||
if path.match("Tutoriumsfolien/Tutorium 15/*"): return True
|
if path.parts[1:] == (): return True
|
||||||
if path.match("Tutoriumsfolien/*"): return False
|
if path.parts[1:2] == ("Tutorium 15",): return True
|
||||||
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def gbi_transform(path):
|
def gbi_transform(path):
|
||||||
# Übungsblätter in Blätter/blatt_xx.pdf
|
# Übungsblätter in Blätter/blatt_xx.pdf
|
||||||
if path.match("Übungsblätter/*"):
|
new_path = PFERD.move(path, ("Übungsblätter",), ("Blätter",))
|
||||||
new_folder = pathlib.PurePath("Blätter")
|
if new_path is not None:
|
||||||
|
|
||||||
match = re.match(r"(\d+).aufgaben.pdf", path.name)
|
match = re.match(r"(\d+).aufgaben.pdf", new_path.name)
|
||||||
if match:
|
if match:
|
||||||
number = int(match.group(1))
|
number = int(match.group(1))
|
||||||
name = f"blatt_{number:02}.pdf"
|
return PFERD.rename(new_path, f"blatt_{number:02}.pdf")
|
||||||
return new_folder / name
|
|
||||||
|
|
||||||
match = re.match(r"(\d+).loesungen.pdf", path.name)
|
match = re.match(r"(\d+).loesungen.pdf", new_path.name)
|
||||||
if match:
|
if match:
|
||||||
number = int(match.group(1))
|
number = int(match.group(1))
|
||||||
name = f"loesung_{number:02}.pdf"
|
return PFERD.rename(new_path, f"loesung_{number:02}.pdf")
|
||||||
return new_folder / name
|
|
||||||
|
|
||||||
return pathlib.Path(new_folder, *path.parts[1:])
|
return new_path
|
||||||
|
|
||||||
# Folien in Folien/*
|
# Folien in Folien/*
|
||||||
if path.match("Vorlesung: Folien/*"):
|
new_path = PFERD.move(path, ("Vorlesung: Folien",), ("Folien",))
|
||||||
return pathlib.Path("Folien", *path.parts[1:])
|
if new_path is not None: return new_path
|
||||||
|
|
||||||
# Skripte in Skripte/*
|
# Skripte in Skripte/*
|
||||||
if path.match("Vorlesung: Skript/*"):
|
new_path = PFERD.move(path, ("Vorlesung: Skript",), ("Skripte",))
|
||||||
return pathlib.Path("Skripte", *path.parts[1:])
|
if new_path is not None: return new_path
|
||||||
|
|
||||||
# Übungsfolien in Übung/*
|
# Übungsfolien in Übung/*
|
||||||
if path.match("große Übung: Folien/*"):
|
new_path = PFERD.move(path, ("große Übung: Folien",), ("Übung",))
|
||||||
return pathlib.Path("Übung", *path.parts[1:])
|
if new_path is not None: return new_path
|
||||||
|
|
||||||
# Tutoriumsfolien in Tutorium/*
|
# Tutoriumsfolien in Tutorium/*
|
||||||
if path.match("Tutoriumsfolien/Tutorium 15/GBI_Tut_2 (1).pdf"):
|
new_path = PFERD.move(path, ("Tutoriumsfolien","Tutorium 15"), ("Tutorium",))
|
||||||
return pathlib.Path("Tutorium", "GBI_Tut_2.pdf")
|
if new_path is not None:
|
||||||
if path.match("Tutoriumsfolien/Tutorium 15/*"):
|
if new_path.name == "GBI_Tut_2 (1).pdf":
|
||||||
return pathlib.Path("Tutorium", *path.parts[2:])
|
return PFERD.rename(new_path, "GBI_Tut_2.pdf")
|
||||||
|
|
||||||
|
return new_path
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def hm1_transform(path):
|
def hm1_transform(path):
|
||||||
if path.match("blatt*.pdf"):
|
if path.match("blatt*.pdf"):
|
||||||
new_folder = pathlib.PurePath("Blätter")
|
new_path = PFERD.move(path, (), ("Blätter",))
|
||||||
|
|
||||||
match = re.match(r"blatt(\d+).pdf", path.name)
|
match = re.match(r"blatt(\d+).pdf", new_path.name)
|
||||||
if match:
|
if match:
|
||||||
number = int(match.group(1))
|
number = int(match.group(1))
|
||||||
name = f"blatt_{number:02}.pdf"
|
return PFERD.rename(new_path, f"blatt_{number:02}.pdf")
|
||||||
return new_folder / name
|
|
||||||
|
|
||||||
match = re.match(r"blatt(\d+).loesungen.pdf", path.name)
|
match = re.match(r"blatt(\d+).loesungen.pdf", new_path.name)
|
||||||
if match:
|
if match:
|
||||||
number = int(match.group(1))
|
number = int(match.group(1))
|
||||||
name = f"loesung_{number:02}.pdf"
|
return PFERD.rename(new_path, f"loesung_{number:02}.pdf")
|
||||||
return new_folder / name
|
|
||||||
|
|
||||||
return pathlib.PurePath(new_folder, *path.parts[1:])
|
return new_path
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def la1_filter(path):
|
def la1_filter(path):
|
||||||
# Tutorien rausfitern
|
# Tutorien rausfitern
|
||||||
if path.match("Tutorien/Tutorium 03 - Philipp Faller"): return True
|
if path.parts[:1] == ("Tutorien",):
|
||||||
if path.match("Tutorien/Tutorium 03 - Philipp Faller/*"): return True
|
if path.parts[1:] == (): return True
|
||||||
if path.match("Tutorien/*"): return False
|
if path.parts[1:2] == ("Tutorium 03 - Philipp Faller",): return True
|
||||||
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def la1_transform(path):
|
def la1_transform(path):
|
||||||
# Alle Übungsblätter in Blätter/blatt_xx.pdf
|
# Alle Übungsblätter in Blätter/blatt_xx.pdf
|
||||||
# Alles andere Übungsmaterial in Blätter/*
|
# Alles andere Übungsmaterial in Blätter/*
|
||||||
if path.match("Übungen/*"):
|
new_path = PFERD.move(path, ("Übungen",), ("Blätter",))
|
||||||
new_folder = pathlib.PurePath("Blätter")
|
if new_path is not None:
|
||||||
|
|
||||||
match = re.match(r"Blatt(\d+).pdf", path.name)
|
match = re.match(r"Blatt(\d+).pdf", new_path.name)
|
||||||
if match:
|
if match:
|
||||||
number = int(match.group(1))
|
number = int(match.group(1))
|
||||||
name = f"blatt_{number:02}.pdf"
|
return PFERD.rename(new_path, f"blatt_{number:02}.pdf")
|
||||||
return new_folder / name
|
|
||||||
|
|
||||||
return pathlib.PurePath(new_folder, *path.parts[1:])
|
return new_path
|
||||||
|
|
||||||
# Alles Tutoriengedöns in Tutorium/*
|
# Alles Tutoriengedöns in Tutorium/*
|
||||||
if path.match("Tutorien/Tutorium 03 - Philipp Faller/tut2.pdf"):
|
new_path = PFERD.move(path, ("Tutorien","Tutorium 03 - Philipp Faller"), ("Tutorium",))
|
||||||
return pathlib.PurePath("Tutorium", "Tut2.pdf")
|
if new_path is not None:
|
||||||
if path.match("Tutorien/Tutorium 03 - Philipp Faller/*"):
|
if new_path.name == "tut2.pdf":
|
||||||
return pathlib.PurePath("Tutorium", *path.parts[2:])
|
return PFERD.rename(new_path, "Tut2.pdf")
|
||||||
|
|
||||||
|
return new_path
|
||||||
|
|
||||||
# Übungs-Gedöns in Übung/*
|
# Übungs-Gedöns in Übung/*
|
||||||
if path.match("Informatikervorlesung/Übung_*"):
|
if path.match("Informatikervorlesung/Übung_*"):
|
||||||
return pathlib.PurePath("Übung", *path.parts[1:])
|
return pathlib.PurePath("Übung", *path.parts[1:])
|
||||||
|
|
||||||
# Vorlesungsfolien-Gedöns in Folien/*
|
# Vorlesungsfolien-Gedöns in Folien/*
|
||||||
if path.match("Informatikervorlesung/*"):
|
new_path = PFERD.move(path, ("Informatikervorlesung",), ("Folien",))
|
||||||
return pathlib.PurePath("Folien", *path.parts[1:])
|
if new_path is not None: return new_path
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def prog_filter(path):
|
def prog_filter(path):
|
||||||
# Tutorien rausfiltern
|
# Tutorien rausfiltern
|
||||||
if path.match("Tutorien"): return False
|
if path.parts[:1] == ("Tutorien",): return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def prog_transform(path):
|
def prog_transform(path):
|
||||||
# Übungsblätter in Blätter/*
|
# Übungsblätter in Blätter/*
|
||||||
if path.match("Übungen/*"):
|
new_path = PFERD.move(path, ("Übungen",), ("Blätter",))
|
||||||
return pathlib.PurePath("Blätter", *path.parts[1:])
|
if new_path is not None: return new_path
|
||||||
|
|
||||||
# Folien in Folien/*
|
# Folien in Folien/*
|
||||||
if path.match("Vorlesungsmaterial/*"):
|
new_path = PFERD.move(path, ("Vorlesungsmaterial",), ("Folien",))
|
||||||
return pathlib.PurePath("Folien", *path.parts[1:])
|
if new_path is not None: return new_path
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user