""" Configuration Manager for the application. Handles loading and saving of application settings to a JSON file. """ import json from pathlib import Path from typing import Dict, Any, Optional, List from . import logger from ..core.export_profiles import ExportProfile, ExportField log = logger.get_logger(__name__) class ConfigManager: """Manages loading and saving configuration from/to a JSON file.""" def __init__(self, config_path: Path): """ Initializes the ConfigManager. Args: config_path: The path to the configuration file. """ self.config_path: Path = config_path self.config: Dict[str, Any] = self._get_default_config() # In-memory representation as objects self.export_profiles: List[ExportProfile] = [] @staticmethod def _get_default_config() -> Dict[str, Any]: """Provides the default configuration structure.""" default_profile = ExportProfile( name="Default", fields=[ ExportField(column_name="batch_id", data_path="batch_id"), ExportField(column_name="timetag", data_path="header.header_data.signal_descr.ttag"), ] ) return { "last_opened_file": "", "last_output_file": "", # <-- NUOVA CHIAVE "active_export_profile_name": "Default", "export_profiles": [default_profile.to_dict()] } def load_config(self) -> None: """Loads the configuration from the JSON file.""" log.info(f"Attempting to load configuration from: {self.config_path}") if self.config_path.is_file(): try: with open(self.config_path, 'r', encoding='utf-8') as f: loaded_data = json.load(f) self.config['last_opened_file'] = loaded_data.get('last_opened_file', '') self.config['last_output_file'] = loaded_data.get('last_output_file', '') # <-- NUOVA RIGA self.config['active_export_profile_name'] = loaded_data.get('active_export_profile_name', 'Default') profiles_data = loaded_data.get("export_profiles", []) self.export_profiles = [ExportProfile.from_dict(p_data) for p_data in profiles_data] if not self.export_profiles: log.warning("No export profiles found in config, loading defaults.") self._load_default_profiles() log.info(f"Configuration loaded successfully. Found {len(self.export_profiles)} export profiles.") except (json.JSONDecodeError, IOError, KeyError) as e: log.error(f"Failed to load or parse config file: {e}. Using defaults.") self._load_defaults() else: log.warning("Configuration file not found. Using default settings.") self._load_defaults() def _load_defaults(self): """Loads all default settings into the manager.""" self.config = self._get_default_config() self._load_default_profiles() def _load_default_profiles(self): """Loads just the default profiles from the default config.""" default_conf = self._get_default_config() profiles_data = default_conf.get("export_profiles", []) self.export_profiles = [ExportProfile.from_dict(p_data) for p_data in profiles_data] def save_config(self) -> None: """Saves the current configuration to the JSON file.""" log.info(f"Saving configuration to: {self.config_path}") data_to_save = { "last_opened_file": self.get("last_opened_file", ""), "last_output_file": self.get("last_output_file", ""), # <-- NUOVA RIGA "active_export_profile_name": self.get("active_export_profile_name", "Default"), "export_profiles": [profile.to_dict() for profile in self.export_profiles] } try: self.config_path.parent.mkdir(parents=True, exist_ok=True) with open(self.config_path, 'w', encoding='utf-8') as f: json.dump(data_to_save, f, indent=4) log.info("Configuration saved successfully.") except IOError as e: log.error(f"Failed to save configuration file: {e}") def get(self, key: str, default: Optional[Any] = None) -> Any: """Gets a value from the configuration.""" return self.config.get(key, default) def set(self, key: str, value: Any) -> None: """Sets a value in the configuration.""" self.config[key] = value def get_export_profiles(self) -> List[ExportProfile]: """Returns the list of loaded export profiles.""" return self.export_profiles def save_export_profiles(self, profiles: List[ExportProfile]): """Updates the list of export profiles and saves the config.""" self.export_profiles = profiles self.save_config()