Skip to content

Commit 5a5ac2c

Browse files
committed
refactor(component validation): the TOW validation into a helper to bring nr of return statements down.
1 parent 3f30889 commit 5a5ac2c

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

ardupilot_methodic_configurator/data_model_vehicle_components_validation.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,26 @@ def _update_possible_choices_for_path( # pylint: disable=too-many-branches
423423
tuple(gnss_available_protocols) if gnss_available_protocols else ("None",)
424424
)
425425

426+
def _validate_tow_limits(self, value: str, path: ComponentPath) -> tuple[str, Optional[float]]:
427+
"""Validate takeoff weight min/max cross-constraints."""
428+
if path[2] == "TOW max Kg":
429+
try:
430+
tow_max = float(value)
431+
except ValueError:
432+
return _("Takeoff Weight max must be a float"), None
433+
lim = self.get_component_value(("Frame", "Specifications", "TOW min Kg"))
434+
if lim:
435+
return self.validate_against_another_value(tow_max, lim, "TOW min Kg", above=True, delta=0.01)
436+
if path[2] == "TOW min Kg":
437+
try:
438+
tow_min = float(value)
439+
except ValueError:
440+
return _("Takeoff Weight min must be a float"), None
441+
lim = self.get_component_value(("Frame", "Specifications", "TOW max Kg"))
442+
if lim:
443+
return self.validate_against_another_value(tow_min, lim, "TOW max Kg", above=False, delta=0.01)
444+
return "", None
445+
426446
def validate_entry_limits(self, value: str, path: ComponentPath) -> tuple[str, Optional[float]]:
427447
"""
428448
Validate entry values against limits.
@@ -445,23 +465,7 @@ def validate_entry_limits(self, value: str, path: ComponentPath) -> tuple[str, O
445465

446466
# Validate takeoff weight limits
447467
if path[0] == "Frame" and path[1] == "Specifications" and "TOW" in path[2]:
448-
if path[2] == "TOW max Kg":
449-
try:
450-
tow_max = float(value)
451-
except ValueError:
452-
return _("Takeoff Weight max must be a float"), None
453-
lim = self.get_component_value(("Frame", "Specifications", "TOW min Kg"))
454-
if lim:
455-
return self.validate_against_another_value(tow_max, lim, "TOW min Kg", above=True, delta=0.01)
456-
457-
if path[2] == "TOW min Kg":
458-
try:
459-
tow_min = float(value)
460-
except ValueError:
461-
return _("Takeoff Weight min must be a float"), None
462-
lim = self.get_component_value(("Frame", "Specifications", "TOW max Kg"))
463-
if lim:
464-
return self.validate_against_another_value(tow_min, lim, "TOW max Kg", above=False, delta=0.01)
468+
return self._validate_tow_limits(value, path)
465469

466470
if path in BATTERY_CELL_VOLTAGE_PATHS:
467471
return self.validate_cell_voltage(value, path)

0 commit comments

Comments
 (0)