diff --git a/web_view.py b/web_view.py index 8bb52ec..7c612d7 100755 --- a/web_view.py +++ b/web_view.py @@ -3,6 +3,7 @@ """Web page to show upcoming events.""" import inspect +import itertools import operator import os.path import sys @@ -164,16 +165,14 @@ def load_travel(travel_type: str) -> list[StrDict]: return items -def build_trip_list() -> list[Trip]: - """Generate list of trips.""" - trips: dict[date, Trip] = {} - +def load_trains() -> list[StrDict]: + """Load trains.""" data_dir = app.config["PERSONAL_DATA"] + trains = load_travel("train") stations = travel.parse_yaml("stations", data_dir) by_name = {station["name"]: station for station in stations} - trains = load_travel("train") for train in trains: assert train["from"] in by_name assert train["to"] in by_name @@ -186,8 +185,30 @@ def build_trip_list() -> list[Trip]: leg["from_station"] = by_name[train["from"]] leg["to_station"] = by_name[train["to"]] + return trains + + +def load_flights() -> list[StrDict]: + """Load flights.""" + data_dir = app.config["PERSONAL_DATA"] + flights = load_travel("flight") + airports = travel.parse_yaml("airports", data_dir) + for flight in flights: + if flight["from"] in airports: + flight["from_airport"] = airports[flight["from"]] + if flight["to"] in airports: + flight["to_airport"] = airports[flight["to"]] + return flights + + +def build_trip_list() -> list[Trip]: + """Generate list of trips.""" + trips: dict[date, Trip] = {} + + data_dir = app.config["PERSONAL_DATA"] + travel_items = sorted( - load_travel("flight") + trains, key=operator.itemgetter("depart") + load_flights() + load_trains(), key=operator.itemgetter("depart") ) data = { @@ -239,20 +260,28 @@ def collect_station_coordinates(trip: Trip) -> list[tuple[float, float]]: """Extract and deduplicate station coordinates from trip.""" stations = {} station_list = [] + airports = {} for t in trip.travel: - if t["type"] != "train": - continue - 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"]) + if t["type"] == "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"]) + else: + assert t["type"] == "flight" + for field in "from_airport", "to_airport": + if field in t: + airports[t[field]["iata"]] = t[field] for s in station_list: if s["uic"] in stations: continue stations[s["uic"]] = s - return [(s["latitude"], s["longitude"]) for s in stations.values()] + return [ + (s["latitude"], s["longitude"]) + for s in itertools.chain(stations.values(), airports.values()) + ] @app.route("/trip/")