From 94c4f47018b9d5eefae0bb04e93d812729ec64ba Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sat, 21 Oct 2023 15:23:34 +0100 Subject: [PATCH] Move code around --- agenda/__init__.py | 76 +++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/agenda/__init__.py b/agenda/__init__.py index 7aa18ed..094421c 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -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(