feat: pfad-metadata-parser mit semester/fach/typ

This commit is contained in:
2026-05-04 22:06:59 +02:00
parent e5032c7e59
commit 8d15f02187
2 changed files with 90 additions and 0 deletions

44
app/ingest/metadata.py Normal file
View File

@@ -0,0 +1,44 @@
import re
from dataclasses import dataclass
from pathlib import PurePosixPath
SEMESTER_RE = re.compile(r"^\d+\.Semester$")
@dataclass(frozen=True)
class PathMetadata:
semester: str
fach: str
typ: str | None
def parse_path(file_path: str, ingest_root: str) -> PathMetadata | None:
"""Parse a Nextcloud file path into structured metadata.
Returns None when the path is outside the ingest root or does not match
the expected `<root>/<N>.Semester/<Fach>/[<typ>/...]/<file>` pattern.
"""
norm_path = file_path.lstrip("/")
norm_root = ingest_root.strip("/")
if not norm_path.startswith(norm_root + "/"):
return None
relative = norm_path[len(norm_root) + 1:]
parts = PurePosixPath(relative).parts
# Need at least: semester / fach / file.ext → 3 parts
if len(parts) < 3:
return None
semester, fach = parts[0], parts[1]
if not SEMESTER_RE.match(semester):
return None
# parts[-1] is the filename. Anything between fach and filename is "deeper".
# The first deeper segment becomes `typ`. None if file lives directly in fach.
typ = parts[2] if len(parts) > 3 else None
return PathMetadata(semester=semester, fach=fach, typ=typ)