Refactor SettingsDialog for improved readability and maintainability

- Cleaned up whitespace and formatting throughout the settings_dialog.py file.
- Enhanced type hints for better clarity and type checking.
- Consolidated URL mapping handling in get_config_data method.
- Improved error handling and logging for configuration operations.
- Added comments for better understanding of the code structure and functionality.
This commit is contained in:
claudi 2026-02-25 13:26:46 +01:00
parent 03991fdea5
commit 986793632e
4 changed files with 468 additions and 480 deletions

View file

@ -12,7 +12,7 @@ logger = logging.getLogger(__name__)
class ConfigValidator:
"""Validates configuration values against schema.
Provides detailed error messages for invalid configurations.
"""
@ -33,10 +33,10 @@ class ConfigValidator:
@staticmethod
def validate(config_dict: Dict[str, Any]) -> List[str]:
"""Validate configuration dictionary.
Args:
config_dict: Configuration dictionary to validate
Returns:
List of validation error messages (empty if valid)
"""
@ -53,7 +53,9 @@ class ConfigValidator:
# Check type
expected_type = rules.get("type")
if expected_type and not isinstance(value, expected_type):
errors.append(f"{field}: expected {expected_type.__name__}, got {type(value).__name__}")
errors.append(
f"{field}: expected {expected_type.__name__}, got {type(value).__name__}"
)
continue
# Check allowed values
@ -84,10 +86,10 @@ class ConfigValidator:
@staticmethod
def validate_or_raise(config_dict: Dict[str, Any]) -> None:
"""Validate configuration and raise error if invalid.
Args:
config_dict: Configuration dictionary to validate
Raises:
ConfigurationError: If configuration is invalid
"""
@ -98,26 +100,26 @@ class ConfigValidator:
class ConfigProfile:
"""Manages named configuration profiles.
Profiles are stored in ~/.webdrop-bridge/profiles/ directory as JSON files.
"""
PROFILES_DIR = Path.home() / ".webdrop-bridge" / "profiles"
def __init__(self):
def __init__(self) -> None:
"""Initialize profile manager."""
self.PROFILES_DIR.mkdir(parents=True, exist_ok=True)
def save_profile(self, profile_name: str, config: Config) -> Path:
"""Save configuration as a named profile.
Args:
profile_name: Name of the profile (e.g., "work", "personal")
config: Config object to save
Returns:
Path to the saved profile file
Raises:
ConfigurationError: If profile name is invalid
"""
@ -148,13 +150,13 @@ class ConfigProfile:
def load_profile(self, profile_name: str) -> Dict[str, Any]:
"""Load configuration from a named profile.
Args:
profile_name: Name of the profile to load
Returns:
Configuration dictionary
Raises:
ConfigurationError: If profile not found or invalid
"""
@ -173,7 +175,7 @@ class ConfigProfile:
def list_profiles(self) -> List[str]:
"""List all available profiles.
Returns:
List of profile names (without .json extension)
"""
@ -184,10 +186,10 @@ class ConfigProfile:
def delete_profile(self, profile_name: str) -> None:
"""Delete a profile.
Args:
profile_name: Name of the profile to delete
Raises:
ConfigurationError: If profile not found
"""
@ -209,11 +211,11 @@ class ConfigExporter:
@staticmethod
def export_to_json(config: Config, output_path: Path) -> None:
"""Export configuration to JSON file.
Args:
config: Config object to export
output_path: Path to write JSON file
Raises:
ConfigurationError: If export fails
"""
@ -240,13 +242,13 @@ class ConfigExporter:
@staticmethod
def import_from_json(input_path: Path) -> Dict[str, Any]:
"""Import configuration from JSON file.
Args:
input_path: Path to JSON file to import
Returns:
Configuration dictionary
Raises:
ConfigurationError: If import fails or validation fails
"""