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"])
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)