Add support for YAML events with duration

This commit is contained in:
Edward Betts 2023-11-10 01:02:22 +01:00
parent 6a82e3d8f4
commit 495f0b9744

View file

@ -10,6 +10,7 @@ from datetime import date, datetime, timedelta
import dateutil.rrule
import dateutil.tz
import holidays # type: ignore
import isodate
import lxml
import pytz
import yaml
@ -144,10 +145,22 @@ def get_yaml_event_date_field(item: dict[str, str]) -> str:
)
def get_yaml_event_end_date_field(item: dict[str, str]) -> str:
"""Event date field name."""
return (
"end_date"
if item["name"] == "travel_insurance"
else ("start_date" if "start_date" in item else "date")
)
def read_events_yaml(data_dir: str, start: date, end: date) -> list[Event]:
"""Read eventes from YAML file."""
events: list[Event] = []
for item in yaml.safe_load(open(os.path.join(data_dir, "events.yaml"))):
duration = (
isodate.parse_duration(item["duration"]) if "duration" in item else None
)
dates = (
dates_from_rrule(item["rrule"], start, end)
if "rrule" in item
@ -158,7 +171,13 @@ def read_events_yaml(data_dir: str, start: date, end: date) -> list[Event]:
name=item["name"],
date=dt,
end_date=(
item.get("end_date") if item["name"] != "travel_insurance" else None
dt + duration
if duration
else (
item.get("end_date")
if item["name"] != "travel_insurance"
else None
)
),
title=item.get("title"),
url=item.get("url"),