Read events from YAML file

Closes: #59
This commit is contained in:
Edward Betts 2023-11-08 15:40:07 +01:00
parent bf5f7f2276
commit a5eab7473d
2 changed files with 11 additions and 16 deletions

View file

@ -11,6 +11,7 @@ import dateutil.tz
import holidays import holidays
import lxml import lxml
import pytz import pytz
import yaml
from dateutil.relativedelta import FR, relativedelta from dateutil.relativedelta import FR, relativedelta
from . import ( from . import (
@ -147,6 +148,7 @@ async def bristol_waste_collection_events(
def combine_holidays(events: list[Event]) -> list[Event]: def combine_holidays(events: list[Event]) -> list[Event]:
"""Combine UK and US holidays with the same date and title."""
combined: dict[tuple[date, str], Event] = {} combined: dict[tuple[date, str], Event] = {}
for e in events: for e in events:
@ -161,15 +163,16 @@ def combine_holidays(events: list[Event]) -> list[Event]:
return list(combined.values()) return list(combined.values())
def xmas_last_posting(config: configparser.ConfigParser) -> list[Event]: def read_events_yaml(data_dir: str) -> list[Event]:
"""Last posting days for Christmas.""" """Read eventes from YAML file."""
return [ return [
Event( Event(
name=f"xmas_last_{key}", name=item["name"],
date=date.fromisoformat(config["xmas_last_posting_dates"][key]), date=item["end_date" if item["name"] == "travel_insurance" else "date"],
url="https://www.postoffice.co.uk/last-posting-dates", title=item.get("title"),
url=item.get("url"),
) )
for key in ("first", "second") for item in yaml.safe_load(open(os.path.join(data_dir, "events.yaml")))
] ]
@ -259,17 +262,10 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]:
events += travel.all_events(config["data"]["personal-data"]) events += travel.all_events(config["data"]["personal-data"])
events += conference.get_list(os.path.join(my_data, "conferences.yaml")) events += conference.get_list(os.path.join(my_data, "conferences.yaml"))
events += backwell_bins + bristol_bins events += backwell_bins + bristol_bins
events += xmas_last_posting(config) events += read_events_yaml(my_data)
events += subscription.get_events(os.path.join(my_data, "subscriptions.yaml")) events += subscription.get_events(os.path.join(my_data, "subscriptions.yaml"))
next_up_series = Event(
date=date(2026, 6, 1),
title="70 Up",
name="next_up_series",
)
events.append(next_up_series)
events.sort(key=operator.attrgetter("as_datetime")) events.sort(key=operator.attrgetter("as_datetime"))
reply["events"] = events reply["events"] = events

View file

@ -66,8 +66,7 @@
"next_us_presidential_election": "US pres. election", "next_us_presidential_election": "US pres. election",
"xmas_last_second": "Christmas last posting 2nd class", "xmas_last_second": "Christmas last posting 2nd class",
"xmas_last_first": "Christmas last posting 1st class", "xmas_last_first": "Christmas last posting 1st class",
"xmas_day": "Christmas day", "up_series": "Up documentary",
"next_up_series": "Next Up documentary",
"waste_schedule": "Waste schedule", "waste_schedule": "Waste schedule",
"gwr_advance_tickets": "GWR advance tickets", "gwr_advance_tickets": "GWR advance tickets",
"critical_mass": "Critical Mass", "critical_mass": "Critical Mass",