diff --git a/agenda/trip.py b/agenda/trip.py index 946c3ac..bf110b4 100644 --- a/agenda/trip.py +++ b/agenda/trip.py @@ -211,6 +211,26 @@ def add_coordinates_for_unbooked_flights( ) +def stations_from_travel(t: StrDict) -> list[StrDict]: + """Stations from train journey.""" + station_list = [t["from_station"], t["to_station"]] + for leg in t["legs"]: + station_list.append(leg["from_station"]) + station_list.append(leg["to_station"]) + + return station_list + + +def process_station_list(station_list: list[StrDict]) -> StrDict: + """Proess sation list.""" + stations = {} + for s in station_list: + if s["name"] in stations: + continue + stations[s["name"]] = s + return stations + + def get_locations(trip: Trip) -> dict[str, StrDict]: """Collect locations of all travel locations in trip.""" locations: dict[str, StrDict] = { @@ -223,10 +243,7 @@ def get_locations(trip: Trip) -> dict[str, StrDict]: for t in trip.travel: match t["type"]: case "train": - station_list += [t["from_station"], t["to_station"]] - for leg in t["legs"]: - station_list.append(leg["from_station"]) - station_list.append(leg["to_station"]) + station_list += stations_from_travel(t) case "flight": for field in "from_airport", "to_airport": if field in t: @@ -236,11 +253,7 @@ def get_locations(trip: Trip) -> dict[str, StrDict]: terminal = t[field] locations["ferry_terminal"][terminal["name"]] = terminal - for s in station_list: - if s["name"] in locations["station"]: - continue - locations["station"][s["name"]] = s - + locations["station"] = process_station_list(station_list) return locations @@ -376,7 +389,7 @@ def get_trip_routes(trip: Trip, data_dir: str) -> list[StrDict]: for item in trip.conferences if "latitude" in item and "longitude" in item - and item["country"] not in ("gb", "be") # not flying to Belgium + and item["country"] not in {"gb", "be", "fr"} # not flying to Belgium or France ] diff --git a/agenda/types.py b/agenda/types.py index 0bfef4d..0b383b1 100644 --- a/agenda/types.py +++ b/agenda/types.py @@ -73,20 +73,21 @@ class Trip: return self.name titles: list[str] = [conf["name"] for conf in self.conferences] + [ event["title"] for event in self.events - ] - if not titles: - for travel in self.travel: - if travel["depart"] and utils.as_date(travel["depart"]) != self.start: - place = travel["from"] - if place not in titles: - titles.append(place) - if travel["depart"] and utils.as_date(travel["depart"]) != self.end: - place = travel["to"] - if place not in titles: - titles.append(place) + ] or self.titles_from_travel() return format_list_with_ampersand(titles) or "[unnamed trip]" + def titles_from_travel(self) -> list[str]: + """Titles from travel.""" + titles = [] + for travel in self.travel: + if not (depart := (travel["depart"] and utils.as_date(travel["depart"]))): + continue + for when, from_or_to in ((self.start, "from"), (self.end, "to")): + if depart != when and travel["from_or_to"] not in titles: + titles.append(travel["from_or_to"]) + return titles + @property def end(self) -> datetime.date | None: """End date for trip.""" diff --git a/validate_yaml.py b/validate_yaml.py index f656ea3..09ce218 100755 --- a/validate_yaml.py +++ b/validate_yaml.py @@ -56,7 +56,9 @@ def check_flights(airlines: set[str]) -> None: assert "trip" in booking assert all(flight["airline"] in airlines for flight in booking["flights"]) flight_count += len(booking["flights"]) - co2_flight_count += len([flight for flight in booking["flights"] if "co2_kg" in flight]) + co2_flight_count += len( + [flight for flight in booking["flights"] if "co2_kg" in flight] + ) check_currency(booking) if prev_first_depart: @@ -65,8 +67,10 @@ def check_flights(airlines: set[str]) -> None: ), "Bookings are not in chronological order by first flight's departure." prev_first_depart = booking["flights"][0]["depart"] - print(f"{len(bookings)} flight bookings, {flight_count} flights, " - f"{co2_flight_count} with CO2 numbers") + print( + f"{len(bookings)} flight bookings, {flight_count} flights, " + f"{co2_flight_count} with CO2 numbers" + ) def check_trains() -> None: