Parse all data file entries as key-value
This commit is contained in:
parent
37efb3cef1
commit
86cb85b56d
1 changed files with 16 additions and 41 deletions
57
movmaker.py
57
movmaker.py
|
|
@ -522,50 +522,25 @@ def parse_data_file(path: Path | None) -> MovieData:
|
||||||
if not path or not path.exists():
|
if not path or not path.exists():
|
||||||
return MovieData(captions={})
|
return MovieData(captions={})
|
||||||
|
|
||||||
raw_lines = [line.rstrip() for line in path.read_text(encoding="utf-8", errors="replace").splitlines()]
|
|
||||||
data = MovieData(captions={})
|
data = MovieData(captions={})
|
||||||
header: list[str] = []
|
for raw in path.read_text(encoding="utf-8", errors="replace").splitlines():
|
||||||
description: list[str] = []
|
|
||||||
in_description = False
|
|
||||||
|
|
||||||
for raw in raw_lines:
|
|
||||||
line = raw.strip()
|
line = raw.strip()
|
||||||
if line.startswith("#"):
|
if not line or line.startswith("#") or ":" not in line:
|
||||||
continue
|
continue
|
||||||
if not line:
|
key, value = line.split(":", 1)
|
||||||
if header or in_description:
|
key = key.strip()
|
||||||
in_description = True
|
value = value.strip()
|
||||||
if description:
|
low = key.lower()
|
||||||
description.append("")
|
if low in {"title", "name"}:
|
||||||
continue
|
data.title = value
|
||||||
if ":" in line:
|
elif low in {"date", "dates", "when"}:
|
||||||
key, value = line.split(":", 1)
|
data.date = value
|
||||||
key = key.strip()
|
elif low in {"place", "location", "where"}:
|
||||||
value = value.strip()
|
data.place = value
|
||||||
if key:
|
elif low in {"description", "desc", "synopsis"}:
|
||||||
data.captions[key] = value
|
data.description = value
|
||||||
continue
|
elif key:
|
||||||
if in_description:
|
data.captions[key] = value
|
||||||
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()
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue