Parse all data file entries as key-value

This commit is contained in:
hbrain 2026-05-25 18:51:49 +00:00
parent 37efb3cef1
commit 86cb85b56d

View file

@ -522,50 +522,25 @@ def parse_data_file(path: Path | None) -> MovieData:
if not path or not path.exists():
return MovieData(captions={})
raw_lines = [line.rstrip() for line in path.read_text(encoding="utf-8", errors="replace").splitlines()]
data = MovieData(captions={})
header: list[str] = []
description: list[str] = []
in_description = False
for raw in raw_lines:
for raw in path.read_text(encoding="utf-8", errors="replace").splitlines():
line = raw.strip()
if line.startswith("#"):
if not line or line.startswith("#") or ":" not in line:
continue
if not line:
if header or in_description:
in_description = True
if description:
description.append("")
continue
if ":" in line:
key, value = line.split(":", 1)
key = key.strip()
value = value.strip()
if key:
data.captions[key] = value
continue
if in_description:
description.append(line)
else:
header.append(line)
# New simple mode:
# title
# location
# date
#
# description
#
# Any key:value line anywhere is a per-file caption.
# Location and/or date may be omitted by placing the blank line earlier.
if header:
data.title = header[0]
if len(header) > 1:
data.place = header[1]
if len(header) > 2:
data.date = header[2]
data.description = "\n".join(description).strip()
key, value = line.split(":", 1)
key = key.strip()
value = value.strip()
low = key.lower()
if low in {"title", "name"}:
data.title = value
elif low in {"date", "dates", "when"}:
data.date = value
elif low in {"place", "location", "where"}:
data.place = value
elif low in {"description", "desc", "synopsis"}:
data.description = value
elif key:
data.captions[key] = value
return data