Compare commits

..

2 commits

Author SHA1 Message Date
Edward Betts 38792a1721 Enhance YAML validation 🛠️: Add airline checks
Closes: #186
2024-10-22 21:19:11 +01:00
Edward Betts aec1d0e140 Tidy accommodation validation 2024-10-22 18:55:57 +01:00

View file

@ -29,10 +29,12 @@ def check_trips() -> None:
print(len(routes), "routes") print(len(routes), "routes")
def check_flights() -> None: def check_flights(airlines: set[str]) -> None:
"""Check flights.""" """Check flights."""
flights = agenda.travel.parse_yaml("flights", data_dir) bookings = agenda.travel.parse_yaml("flights", data_dir)
print(len(flights), "flights") for booking in bookings:
assert all(flight["airline"] in airlines for flight in booking["flights"])
print(len(bookings), "flights")
def check_trains() -> None: def check_trains() -> None:
@ -57,31 +59,25 @@ def check_events() -> None:
print(len(events), "events") print(len(events), "events")
def check_coordinates(item: agenda.types.StrDict) -> None:
"""Check coordinate are valid."""
if "latitude" not in item and "longitude" not in item:
return
assert "latitude" in item and "longitude" in item
assert all(isinstance(item[key], (int, float)) for key in ("latitude", "longitude"))
def check_accommodation() -> None: def check_accommodation() -> None:
"""Check accommodation.""" """Check accommodation."""
filepath = os.path.join(data_dir, "accommodation.yaml") filepath = os.path.join(data_dir, "accommodation.yaml")
accommodation_list = yaml.safe_load(open(filepath)) accommodation_list = yaml.safe_load(open(filepath))
required_fields = ["type", "name", "country", "location", "trip", "from", "to"]
for stay in accommodation_list: for stay in accommodation_list:
try: try:
assert all( assert all(field in stay for field in required_fields)
field in stay check_coordinates(stay)
for field in (
"type",
"name",
"country",
"location",
"trip",
"from",
"to",
)
)
if "latitude" in stay or "longitude" in stay:
assert "latitude" in stay and "longitude" in stay
assert all(
isinstance(stay[key], (int, float))
for key in ("latitude", "longitude")
)
except AssertionError: except AssertionError:
pprint(stay) pprint(stay)
raise raise
@ -109,25 +105,29 @@ def check_stations() -> None:
assert agenda.get_country(station["country"]) assert agenda.get_country(station["country"])
def check_airlines() -> None: def check_airlines() -> list[agenda.types.StrDict]:
"""Check airlines.""" """Check airlines."""
airlines = agenda.travel.parse_yaml("airlines", data_dir) airlines = agenda.travel.parse_yaml("airlines", data_dir)
print(len(airlines), "airlines") print(len(airlines), "airlines")
for airline in airlines: for airline in airlines:
assert airline.keys() == {"icao", "iata", "name"} assert airline.keys() == {"icao", "iata", "name"}
assert len(airline["icao"]) == 3
assert len(airline["iata"]) == 2
return airlines
def check() -> None: def check() -> None:
"""Validate personal data YAML files.""" """Validate personal data YAML files."""
airlines = check_airlines()
check_trips() check_trips()
check_flights() check_flights({airline["iata"] for airline in airlines})
check_trains() check_trains()
check_conferences() check_conferences()
check_events() check_events()
check_accommodation() check_accommodation()
check_airports() check_airports()
check_stations() check_stations()
check_airlines()
if __name__ == "__main__": if __name__ == "__main__":