Add command line utility to validate YAML

This commit is contained in:
Edward Betts 2024-03-11 15:58:56 +00:00
parent f1338e5970
commit 4ebb08f68e
2 changed files with 35 additions and 12 deletions

View file

@ -9,20 +9,17 @@ from agenda import travel
from agenda.types import StrDict, Trip 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.""" """Read flight and train journeys."""
data_dir = flask.current_app.config["PERSONAL_DATA"]
items = travel.parse_yaml(travel_type + "s", data_dir) items = travel.parse_yaml(travel_type + "s", data_dir)
for item in items: for item in items:
item["type"] = travel_type item["type"] = travel_type
return items return items
def load_trains() -> list[StrDict]: def load_trains(data_dir: str) -> list[StrDict]:
"""Load trains.""" """Load trains."""
data_dir = flask.current_app.config["PERSONAL_DATA"] trains = load_travel("train", data_dir)
trains = load_travel("train")
stations = travel.parse_yaml("stations", data_dir) stations = travel.parse_yaml("stations", data_dir)
by_name = {station["name"]: station for station in stations} 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")) 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.""" """Load flights."""
data_dir = flask.current_app.config["PERSONAL_DATA"] flights = load_travel("flight", data_dir)
flights = load_travel("flight")
airlines = yaml.safe_load(open(os.path.join(data_dir, "airlines.yaml"))) airlines = yaml.safe_load(open(os.path.join(data_dir, "airlines.yaml")))
airports = travel.parse_yaml("airports", data_dir) airports = travel.parse_yaml("airports", data_dir)
for flight in flights: for flight in flights:
@ -64,13 +60,16 @@ def load_flights() -> list[StrDict]:
return flights return flights
def build_trip_list() -> list[Trip]: def build_trip_list(data_dir: str | None = None) -> list[Trip]:
"""Generate list of trips.""" """Generate list of trips."""
trips: dict[date, Trip] = {} 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 = { data = {
"travel": travel_items, "travel": travel_items,

24
validate_yaml.py Executable file
View file

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