Move code around

This commit is contained in:
Edward Betts 2023-10-21 15:23:34 +01:00
parent 3aeb2fe467
commit 26f9e98b03

View file

@ -323,44 +323,54 @@ def get_birthdays(from_date: date, filepath: str) -> list[Event]:
return events
def get_travel(from_date: date, filepath: str) -> list[Event]:
def get_accommodation(from_date: date, filepath: str) -> list[Event]:
"""Get birthdays from config."""
events = []
with open(filepath) as f:
items = yaml.safe_load(f)
for item in items:
if item["type"] == "accommodation":
title = (
f'{item["location"]} Airbnb'
if item["operator"] == "airbnb"
else item["name"]
)
from_date = item["from"]
to_date = item["to"]
nights = (to_date - from_date).days
night_str = f"{nights} nights" if nights != 1 else "1 night"
checkin = Event(
date=from_date,
name="accommodation",
title=f"check-in {title} ({night_str})",
)
checkout = Event(
date=to_date, name="accommodation", title="check-out " + title
)
events += [checkin, checkout]
continue
if item["type"] == "transport":
event = Event(
date=datetime.fromisoformat(item["depart"]).date(),
name="transport",
title=f'{item["transport_type"]} from {item["from"]} to {item["to"]}',
)
events.append(event)
title = (
f'{item["location"]} Airbnb'
if item["operator"] == "airbnb"
else item["name"]
)
from_date = item["from"]
to_date = item["to"]
nights = (to_date - from_date).days
night_str = f"{nights} nights" if nights != 1 else "1 night"
checkin = Event(
date=from_date,
name="accommodation",
title=f"check-in {title} ({night_str})",
)
checkout = Event(date=to_date, name="accommodation", title="check-out " + title)
events += [checkin, checkout]
return events
def get_travel(from_date: date, method: str, filepath: str) -> list[Event]:
"""Get travel events."""
return [
Event(
date=item["depart"].date(),
name="transport",
title=f'{method} from {item["from"]} to {item["to"]}',
)
for item in yaml.safe_load(open(filepath))
if item["depart"].date() >= from_date
]
def get_all_travel_events(from_date: date) -> list[Event]:
"""Get all flights and rail journeys."""
data_dir = config["data"]["personal-data"]
trains = get_travel(from_date, "train", os.path.join(data_dir, "trains.yaml"))
flights = get_travel(from_date, "flight", os.path.join(data_dir, "flights.yaml"))
return trains + flights
def waste_collection_events() -> list[Event]:
"""Waste colllection events."""
postcode = "BS48 3HG"
@ -410,9 +420,11 @@ def get_data(now: datetime) -> dict[str, str | object]:
for key, value in xmas_last_posting_dates.items():
events.append(Event(name=f"xmas_last_{key}", date=value))
events += get_birthdays(today, config["data"]["entities"])
events += get_travel(today, config["data"]["travel"])
events += get_conferences(today, config["data"]["conferences"])
my_data = config["data"]["personal-data"]
events += get_birthdays(today, os.path.join(my_data, "entities.yaml"))
events += get_accommodation(today, os.path.join(my_data, "accommodation.yaml"))
events += get_all_travel_events(today)
events += get_conferences(today, os.path.join(my_data, "conferences.yaml"))
events += waste_collection_events()
next_up_series = Event(