Source code for app.ui_validation

"""
UI Validation Module
"""

from pathlib import Path
from typing import Protocol, Callable

import i18n


class ConfigManagerProtocol(Protocol):
    """Protocol for configuration manager to manage settings."""

    output_path: Path
    use_vessel: bool
    use_waterline: bool
    vessel_id: str
    waterline_value: float

    def update_output_path(self, path_str: str) -> None:
        """Update output path from string input."""
        ...

    def toggle_vessel_mode(self) -> bool:
        """Toggle vessel mode and handle mutual exclusivity. Returns True if changed."""
        ...

    def toggle_waterline_mode(self) -> bool:
        """Toggle waterline mode and handle mutual exclusivity. Returns True if changed."""
        ...


[docs] class Validator: def __init__( self, get_files_func: Callable[[], list[dict]], config_manager: ConfigManagerProtocol, ) -> None: self.get_files_func = get_files_func self.config_manager = config_manager
[docs] def validate_inputs(self) -> list[str]: """Validate all inputs and return list of errors.""" errors = [] if not self.get_files_func(): errors.append(i18n.t("app.ui_validation.no_file_selected")) # Use config_manager for all configuration properties output_path = self.config_manager.output_path if not output_path or str(output_path).strip() == "" or output_path == Path(): errors.append(i18n.t("app.ui_validation.no_output_path")) use_vessel = self.config_manager.use_vessel use_waterline = self.config_manager.use_waterline if use_vessel and use_waterline: errors.append(i18n.t("app.ui_validation.mutual_exclusivity")) # Additional validations for vessel and waterline values vessel_id = self.config_manager.vessel_id if use_vessel and not vessel_id.strip(): errors.append(i18n.t("app.ui_validation.no_vessel_id")) waterline_value = self.config_manager.waterline_value if use_waterline and waterline_value < 0: errors.append(i18n.t("app.ui_validation.invalid_waterline")) return errors
[docs] def validate_file_selection(self) -> bool: """Check if at least one file is selected.""" return bool(self.get_files_func())
[docs] def validate_output_path(self) -> bool: """Check if output path is specified.""" output_path = self.config_manager.output_path return bool(output_path and output_path != Path() and str(output_path).strip())
[docs] def validate_mutual_exclusivity(self) -> bool: """Check vessel and waterline options mutual exclusivity.""" use_vessel = self.config_manager.use_vessel use_waterline = self.config_manager.use_waterline return not (use_vessel and use_waterline)
[docs] def validate_vessel_configuration(self) -> bool: """Check vessel configuration validity.""" use_vessel = self.config_manager.use_vessel if use_vessel: vessel_id = self.config_manager.vessel_id return bool(vessel_id.strip()) return True
[docs] def validate_waterline_configuration(self) -> bool: """Check waterline configuration validity.""" use_waterline = self.config_manager.use_waterline if use_waterline: waterline_value = self.config_manager.waterline_value return waterline_value >= 0 return True