From 4ebb08f68e9489cba0efdaef6ebb4a3b24d0c9c4 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 11 Mar 2024 15:58:56 +0000 Subject: [PATCH] Add command line utility to validate YAML --- agenda/trip.py | 23 +++++++++++------------ validate_yaml.py | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) create mode 100755 validate_yaml.py diff --git a/agenda/trip.py b/agenda/trip.py index bf22c3d..b303a6a 100644 --- a/agenda/trip.py +++ b/agenda/trip.py @@ -9,20 +9,17 @@ from agenda import travel from agenda.types import StrDict, Trip -def load_travel(travel_type: str) -> list[StrDict]: +def load_travel(travel_type: str, data_dir: str) -> list[StrDict]: """Read flight and train journeys.""" - data_dir = flask.current_app.config["PERSONAL_DATA"] items = travel.parse_yaml(travel_type + "s", data_dir) for item in items: item["type"] = travel_type return items -def load_trains() -> list[StrDict]: +def load_trains(data_dir: str) -> list[StrDict]: """Load trains.""" - data_dir = flask.current_app.config["PERSONAL_DATA"] - - trains = load_travel("train") + trains = load_travel("train", data_dir) stations = travel.parse_yaml("stations", data_dir) by_name = {station["name"]: station for station in stations} @@ -48,10 +45,9 @@ def depart_datetime(item: StrDict) -> datetime: return datetime.combine(depart, time.min).replace(tzinfo=ZoneInfo("UTC")) -def load_flights() -> list[StrDict]: +def load_flights(data_dir: str) -> list[StrDict]: """Load flights.""" - data_dir = flask.current_app.config["PERSONAL_DATA"] - flights = load_travel("flight") + flights = load_travel("flight", data_dir) airlines = yaml.safe_load(open(os.path.join(data_dir, "airlines.yaml"))) airports = travel.parse_yaml("airports", data_dir) for flight in flights: @@ -64,13 +60,16 @@ def load_flights() -> list[StrDict]: return flights -def build_trip_list() -> list[Trip]: +def build_trip_list(data_dir: str | None = None) -> list[Trip]: """Generate list of trips.""" trips: dict[date, Trip] = {} - data_dir = flask.current_app.config["PERSONAL_DATA"] + if data_dir is None: + data_dir = flask.current_app.config["PERSONAL_DATA"] - travel_items = sorted(load_flights() + load_trains(), key=depart_datetime) + travel_items = sorted( + load_flights(data_dir) + load_trains(data_dir), key=depart_datetime + ) data = { "travel": travel_items, diff --git a/validate_yaml.py b/validate_yaml.py new file mode 100755 index 0000000..e506441 --- /dev/null +++ b/validate_yaml.py @@ -0,0 +1,24 @@ +#!/usr/bin/python3 +"""Load YAML data to ensure validity.""" + +import os + +import agenda.conference +import agenda.travel +import agenda.trip + +config = __import__("config.default", fromlist=[""]) + +data_dir = config.PERSONAL_DATA + +trip_list = agenda.trip.build_trip_list(data_dir) +print(len(trip_list), "trips") + +flights = agenda.travel.parse_yaml("flights", data_dir) +print(len(flights), "flights") + +trains = agenda.travel.parse_yaml("trains", data_dir) +print(len(trains), "trains") + +conferences = agenda.conference.get_list(os.path.join(data_dir, "conferences.yaml")) +print(len(conferences), "conferences")