Files
SemesterapparatsManager/src/shared/config.py

67 lines
1.8 KiB
Python

"""Application configuration and settings."""
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
import yaml
from src.shared.logging import log
@dataclass
class Settings:
"""Settings for the application."""
save_path: str
database_name: str
database_path: str
bib_id: str = ""
default_apps: bool = True
custom_applications: list[dict[str, Any]] = field(default_factory=list)
def save_settings(self, config_path: str | Path = "config.yaml") -> None:
"""Save the settings to the config file.
Args:
config_path: Path to the configuration file
"""
try:
with open(config_path, "w") as f:
yaml.dump(self.__dict__, f)
log.info(f"Settings saved to {config_path}")
except Exception as e:
log.error(f"Failed to save settings: {e}")
raise
@classmethod
def load_settings(cls, config_path: str | Path = "config.yaml") -> dict[str, Any]:
"""Load the settings from the config file.
Args:
config_path: Path to the configuration file
Returns:
Dictionary containing the loaded settings
"""
try:
with open(config_path, "r") as f:
data = yaml.safe_load(f)
log.info(f"Settings loaded from {config_path}")
return data
except Exception as e:
log.error(f"Failed to load settings: {e}")
raise
def load_config(config_path: str | Path = "config.yaml") -> dict[str, Any]:
"""Convenience function to load configuration.
Args:
config_path: Path to the configuration file
Returns:
Dictionary containing the loaded settings
"""
return Settings.load_settings(config_path)