From 6d1b01485a7a340b3d055641707000f43a5a5ed7 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 4 Nov 2024 10:23:21 +0000 Subject: [PATCH] Check for unknown currencies during validation --- validate_yaml.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/validate_yaml.py b/validate_yaml.py index 68df0f9..818840d 100755 --- a/validate_yaml.py +++ b/validate_yaml.py @@ -2,6 +2,7 @@ """Load YAML data to ensure validity.""" import os +import sys import typing from datetime import date, timedelta @@ -18,6 +19,18 @@ import agenda.types config = __import__("config.default", fromlist=[""]) data_dir = config.PERSONAL_DATA +currencies = set(config.CURRENCIES + ["GBP"]) + + +def check_currency(item: agenda.types.StrDict) -> None: + """Throw error if currency is not in config.""" + currency = item.get("currency") + if not currency or currency in currencies: + return None + pprint(item) + print(f"currency {currency!r} not in {currencies!r}") + sys.exit(-1) + def check_trips() -> None: """Check trips.""" @@ -34,6 +47,10 @@ def check_flights(airlines: set[str]) -> None: bookings = agenda.travel.parse_yaml("flights", data_dir) for booking in bookings: assert all(flight["airline"] in airlines for flight in booking["flights"]) + + for booking in bookings: + check_currency(booking) + print(len(bookings), "flights") @@ -45,7 +62,18 @@ def check_trains() -> None: def check_conferences() -> None: """Check conferences.""" - conferences = agenda.conference.get_list(os.path.join(data_dir, "conferences.yaml")) + filepath = os.path.join(data_dir, "conferences.yaml") + conferences = [ + agenda.conference.Conference(**conf) + for conf in yaml.safe_load(open(filepath, "r")) + ] + for conf in conferences: + if not conf.currency or conf.currency in currencies: + continue + pprint(conf) + print(f"currency {conf.currency!r} not in {currencies!r}") + sys.exit(-1) + print(len(conferences), "conferences") @@ -82,6 +110,8 @@ def check_accommodation() -> None: pprint(stay) raise + check_currency(stay) + print(len(accommodation_list), "stays")