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