Refactor validate_yaml.py

This commit is contained in:
Edward Betts 2024-07-03 09:54:00 +03:00
parent e814e1b135
commit e66945a825

View file

@ -13,9 +13,11 @@ import agenda.trip
import agenda.types import agenda.types
config = __import__("config.default", fromlist=[""]) config = __import__("config.default", fromlist=[""])
data_dir = config.PERSONAL_DATA data_dir = config.PERSONAL_DATA
def check_trips() -> None:
"""Check trips."""
trip_list = agenda.trip.build_trip_list(data_dir) trip_list = agenda.trip.build_trip_list(data_dir)
print(len(trip_list), "trips") print(len(trip_list), "trips")
@ -23,15 +25,27 @@ coords, routes = agenda.trip.get_coordinates_and_routes(trip_list, data_dir)
print(len(coords), "coords") print(len(coords), "coords")
print(len(routes), "routes") print(len(routes), "routes")
def check_flights() -> None:
"""Check flights."""
flights = agenda.travel.parse_yaml("flights", data_dir) flights = agenda.travel.parse_yaml("flights", data_dir)
print(len(flights), "flights") print(len(flights), "flights")
def check_trains() -> None:
"""Check trains."""
trains = agenda.travel.parse_yaml("trains", data_dir) trains = agenda.travel.parse_yaml("trains", data_dir)
print(len(trains), "trains") print(len(trains), "trains")
def check_conferences() -> None:
"""Check conferences."""
conferences = agenda.conference.get_list(os.path.join(data_dir, "conferences.yaml")) conferences = agenda.conference.get_list(os.path.join(data_dir, "conferences.yaml"))
print(len(conferences), "conferences") print(len(conferences), "conferences")
def check_events() -> None:
"""Check events."""
today = date.today() today = date.today()
last_year = today - timedelta(days=365) last_year = today - timedelta(days=365)
next_year = today + timedelta(days=2 * 365) next_year = today + timedelta(days=2 * 365)
@ -39,6 +53,9 @@ next_year = today + timedelta(days=2 * 365)
events = agenda.events_yaml.read(data_dir, last_year, next_year) events = agenda.events_yaml.read(data_dir, last_year, next_year)
print(len(events), "events") print(len(events), "events")
def check_airports() -> None:
"""Check airports."""
airports = typing.cast( airports = typing.cast(
dict[str, agenda.types.StrDict], agenda.travel.parse_yaml("airports", data_dir) dict[str, agenda.types.StrDict], agenda.travel.parse_yaml("airports", data_dir)
) )
@ -48,8 +65,34 @@ for airport in airports.values():
assert agenda.get_country(airport["country"]) assert agenda.get_country(airport["country"])
def check_stations() -> None:
"""Check stations."""
stations = agenda.travel.parse_yaml("stations", data_dir) stations = agenda.travel.parse_yaml("stations", data_dir)
print(len(stations), "stations") print(len(stations), "stations")
for station in stations: for station in stations:
assert "country" in station assert "country" in station
assert agenda.get_country(station["country"]) assert agenda.get_country(station["country"])
def check_airlines() -> None:
"""Check airlines."""
airlines = agenda.travel.parse_yaml("airlines", data_dir)
print(len(airlines), "airlines")
for airline in airlines:
assert airline.keys() == {"icao", "iata", "name"}
def check() -> None:
"""Validate personal data YAML files."""
check_trips()
check_flights()
check_trains()
check_conferences()
check_events()
check_airports()
check_stations()
check_airlines()
if __name__ == "__main__":
check()