From a607f29259bc47f273c3be9fa68465dd8f0d4095 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 5 Apr 2024 11:21:41 +0200 Subject: [PATCH] Validate YAML to catch bad train rotues Closes: #143 --- agenda/trip.py | 9 +++++---- validate_yaml.py | 4 ++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/agenda/trip.py b/agenda/trip.py index db148e9..2a4f7ee 100644 --- a/agenda/trip.py +++ b/agenda/trip.py @@ -165,9 +165,8 @@ def latlon_tuple(stop: StrDict) -> tuple[float, float]: return (stop["latitude"], stop["longitude"]) -def read_geojson(filename: str) -> str: +def read_geojson(data_dir: str, filename: str) -> str: """Read GeoJSON from file.""" - data_dir = flask.current_app.config["PERSONAL_DATA"] return open(os.path.join(data_dir, "train_routes", filename + ".geojson")).read() @@ -238,8 +237,10 @@ def get_trip_routes(trip: Trip) -> list[StrDict]: def get_coordinates_and_routes( - trip_list: list[Trip], + trip_list: list[Trip], data_dir: str | None = None ) -> tuple[list[StrDict], list[StrDict]]: + if data_dir is None: + data_dir = flask.current_app.config["PERSONAL_DATA"] coordinates = [] seen_coordinates: set[tuple[str, str]] = set() routes = [] @@ -260,6 +261,6 @@ def get_coordinates_and_routes( for route in routes: if "geojson_filename" in route: - route["geojson"] = read_geojson(route.pop("geojson_filename")) + route["geojson"] = read_geojson(data_dir, route.pop("geojson_filename")) return (coordinates, routes) diff --git a/validate_yaml.py b/validate_yaml.py index e506441..75fb42e 100755 --- a/validate_yaml.py +++ b/validate_yaml.py @@ -14,6 +14,10 @@ data_dir = config.PERSONAL_DATA trip_list = agenda.trip.build_trip_list(data_dir) print(len(trip_list), "trips") +coords, routes = agenda.trip.get_coordinates_and_routes(trip_list, data_dir) +print(len(coords), "coords") +print(len(routes), "routes") + flights = agenda.travel.parse_yaml("flights", data_dir) print(len(flights), "flights")