61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
"""Configuration management for the prompt manager."""
|
|
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import yaml
|
|
|
|
from .models import Config
|
|
|
|
|
|
class ConfigManager:
|
|
"""Manages user and system configuration."""
|
|
|
|
def __init__(self, config_path: str = None):
|
|
if config_path is None:
|
|
home = Path.home()
|
|
self.config_path = home / ".config" / "llm-prompt-manager" / "config.yaml"
|
|
else:
|
|
self.config_path = Path(config_path)
|
|
|
|
def ensure_config_dir(self) -> None:
|
|
"""Ensure configuration directory exists."""
|
|
self.config_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
def load(self) -> Config:
|
|
"""Load configuration from file."""
|
|
self.ensure_config_dir()
|
|
if self.config_path.exists():
|
|
with open(self.config_path) as f:
|
|
data = yaml.safe_load(f) or {}
|
|
return Config.from_dict(data)
|
|
return Config()
|
|
|
|
def save(self, config: Config) -> None:
|
|
"""Save configuration to file."""
|
|
self.ensure_config_dir()
|
|
with open(self.config_path, "w") as f:
|
|
yaml.dump(config.to_dict(), f, default_flow_style=False)
|
|
|
|
def get(self, key: str) -> Any:
|
|
"""Get a configuration value by key."""
|
|
config = self.load()
|
|
return getattr(config, key, None)
|
|
|
|
def set(self, key: str, value: Any) -> None:
|
|
"""Set a configuration value."""
|
|
config = self.load()
|
|
setattr(config, key, value)
|
|
self.save(config)
|
|
|
|
def reset(self) -> None:
|
|
"""Reset configuration to defaults."""
|
|
config = Config()
|
|
self.save(config)
|
|
|
|
|
|
def get_config() -> Config:
|
|
"""Get the current configuration."""
|
|
manager = ConfigManager()
|
|
return manager.load()
|