"""Style configuration management.""" from __future__ import annotations import json from pathlib import Path from src.models.style import TranslationStyle class StyleConfigManager: """Manages translation style configuration persistence.""" def __init__(self, config_path: Path | str | None = None) -> None: """Initialize the style config manager. Args: config_path: Path to the style configuration file. If None, uses default location. """ if config_path is None: # Default to data/config/style.json relative to project root self._config_path = Path("data/config/style.json") else: self._config_path = Path(config_path) self._style: TranslationStyle | None = None @property def config_path(self) -> Path: """Get the configuration file path.""" return self._config_path def load(self) -> TranslationStyle: """Load style configuration from file. Returns: TranslationStyle configuration. Returns default if file doesn't exist. """ if self._style is not None: return self._style if not self._config_path.exists(): self._style = TranslationStyle() return self._style with self._config_path.open("r", encoding="utf-8") as f: data = json.load(f) self._style = TranslationStyle.from_dict(data) return self._style def save(self, style: TranslationStyle | None = None) -> None: """Save style configuration to file. Args: style: Style to save. If None, saves the currently loaded style. """ if style is not None: self._style = style if self._style is None: self._style = TranslationStyle() # Ensure directory exists self._config_path.parent.mkdir(parents=True, exist_ok=True) with self._config_path.open("w", encoding="utf-8") as f: json.dump(self._style.to_dict(), f, ensure_ascii=False, indent=2) def get_style(self) -> TranslationStyle: """Get the current style configuration. Loads from file if not already loaded. Returns: Current TranslationStyle configuration. """ if self._style is None: return self.load() return self._style def set_style(self, style: TranslationStyle) -> None: """Set the current style configuration (in memory only). Call save() to persist to file. Args: style: The new style configuration. """ self._style = style def reset_to_default(self) -> TranslationStyle: """Reset to default style configuration. Returns: The default TranslationStyle. """ self._style = TranslationStyle() return self._style def reload(self) -> TranslationStyle: """Force reload from file, discarding in-memory changes. Returns: Freshly loaded TranslationStyle. """ self._style = None return self.load()