Validate YAML to catch bad train rotues

Closes: #143
This commit is contained in:
Edward Betts 2024-04-05 11:21:41 +02:00
parent e5325a0392
commit a607f29259
2 changed files with 9 additions and 4 deletions

View file

@ -165,9 +165,8 @@ def latlon_tuple(stop: StrDict) -> tuple[float, float]:
return (stop["latitude"], stop["longitude"]) return (stop["latitude"], stop["longitude"])
def read_geojson(filename: str) -> str: def read_geojson(data_dir: str, filename: str) -> str:
"""Read GeoJSON from file.""" """Read GeoJSON from file."""
data_dir = flask.current_app.config["PERSONAL_DATA"]
return open(os.path.join(data_dir, "train_routes", filename + ".geojson")).read() 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( def get_coordinates_and_routes(
trip_list: list[Trip], trip_list: list[Trip], data_dir: str | None = None
) -> tuple[list[StrDict], list[StrDict]]: ) -> tuple[list[StrDict], list[StrDict]]:
if data_dir is None:
data_dir = flask.current_app.config["PERSONAL_DATA"]
coordinates = [] coordinates = []
seen_coordinates: set[tuple[str, str]] = set() seen_coordinates: set[tuple[str, str]] = set()
routes = [] routes = []
@ -260,6 +261,6 @@ def get_coordinates_and_routes(
for route in routes: for route in routes:
if "geojson_filename" in route: 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) return (coordinates, routes)

View file

@ -14,6 +14,10 @@ data_dir = config.PERSONAL_DATA
trip_list = agenda.trip.build_trip_list(data_dir) trip_list = agenda.trip.build_trip_list(data_dir)
print(len(trip_list), "trips") 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) flights = agenda.travel.parse_yaml("flights", data_dir)
print(len(flights), "flights") print(len(flights), "flights")