diff --git a/src/euring/rules.py b/src/euring/rules.py index 845ba8c..5d87405 100644 --- a/src/euring/rules.py +++ b/src/euring/rules.py @@ -2,29 +2,23 @@ from __future__ import annotations -from .fields import ( - EURING2020_ONLY_KEYS, - EURING_FIELDS, - NON_EURING2000_KEYS, -) +from .fields import EURING2020_ONLY_KEYS, EURING_KEY_NAME, NON_EURING2000_KEYS from .formats import FORMAT_EURING2000, FORMAT_EURING2000PLUS, FORMAT_EURING2020 -_FIELD_NAME_BY_KEY = {field["key"]: field["name"] for field in EURING_FIELDS} - def field_name_for_key(key: str) -> str: """Return the field name for a key, falling back to the key.""" - return _FIELD_NAME_BY_KEY.get(key, key) + return EURING_KEY_NAME.get(key, key) -def accuracy_is_alpha(values_by_key: dict[str, str]) -> bool: +def accuracy_of_coordinates_is_alpha(values_by_key: dict[str, str]) -> bool: """Return True when accuracy_of_coordinates is alphabetic.""" - accuracy = values_by_key.get("accuracy_of_coordinates", "") - return bool(accuracy) and accuracy.isalpha() + accuracy_of_coordinates = values_by_key.get("accuracy_of_coordinates", "") + return bool(accuracy_of_coordinates) and accuracy_of_coordinates.isalpha() def matches_euring2000(values_by_key: dict[str, str]) -> bool: - """Return True when values fit EURING2000.""" + """Return True when values are present only for fields in EURING2000 format.""" for key in NON_EURING2000_KEYS: if values_by_key.get(key): return False @@ -32,13 +26,13 @@ def matches_euring2000(values_by_key: dict[str, str]) -> bool: def requires_euring2000plus(values_by_key: dict[str, str]) -> bool: - """Return True when values require EURING2000+.""" + """Return True when values are present for fields that are not in EURING2000 format.""" return not matches_euring2000(values_by_key) def requires_euring2020(values_by_key: dict[str, str]) -> bool: """Return True when values require EURING2020.""" - if accuracy_is_alpha(values_by_key): + if accuracy_of_coordinates_is_alpha(values_by_key): return True for key in EURING2020_ONLY_KEYS: if values_by_key.get(key): @@ -58,22 +52,22 @@ def _error(key, message): } if format == FORMAT_EURING2020: - geo_value = values_by_key.get("geographical_coordinates", "") or "" - lat_value = values_by_key.get("latitude", "") or "" - lng_value = values_by_key.get("longitude", "") or "" - if lat_value or lng_value: - if geo_value and geo_value != "." * 15: + geographical_coordinates = values_by_key.get("geographical_coordinates", "") or "" + latitude = values_by_key.get("latitude", "") or "" + longitude = values_by_key.get("longitude", "") or "" + if latitude or longitude: + if geographical_coordinates and geographical_coordinates != "." * 15: errors.append( _error( key="geographical_coordinates", message="When Latitude/Longitude are provided, Geographical Co-ordinates must be 15 dots.", ) ) - if lat_value and not lng_value: + if latitude and not longitude: errors.append( _error(key="longitude", message="Longitude is required when Latitude is provided."), ) - if lng_value and not lat_value: + if longitude and not latitude: errors.append( _error( key="latitude", @@ -81,7 +75,7 @@ def _error(key, message): ) ) else: - if accuracy_is_alpha(values_by_key): + if accuracy_of_coordinates_is_alpha(values_by_key): errors.append( _error( key="accuracy_of_coordinates",