diff --git a/shellgenius/config.py b/shellgenius/config.py index c05f9ba..6394af2 100644 --- a/shellgenius/config.py +++ b/shellgenius/config.py @@ -10,6 +10,97 @@ CONFIG_DIR = Path(os.environ.get("XDG_CONFIG_HOME", Path.home() / ".config")) / CONFIG_FILE = CONFIG_DIR / "config.yaml" +class Config: + """Configuration class for ShellGenius.""" + + def __init__(self, config_path: str | None = None): + """Initialize configuration. + + Args: + config_path: Path to config file + """ + if config_path is None: + config_path = str(CONFIG_FILE) + + self._config_path = config_path + self._config = self._load_config() + + def _load_config(self) -> dict[str, Any]: + """Load configuration from file. + + Returns: + Configuration dictionary + """ + defaults = { + "ollama": { + "host": "localhost:11434", + "model": "llama3", + }, + "safety": { + "level": "moderate", + }, + "default_shell": "bash", + } + + try: + with open(self._config_path) as f: + config = yaml.safe_load(f) or {} + except FileNotFoundError: + config = {} + + if isinstance(config, dict): + defaults.update(config) + + return defaults + + def get(self, key: str, default: Any = None) -> Any: + """Get configuration value by key. + + Args: + key: Dot-separated key (e.g., "ollama.host") + default: Default value if key not found + + Returns: + Configuration value + """ + keys = key.split(".") + value: Any = self._config + + for k in keys: + if isinstance(value, dict): + value = value.get(k) if k in value else None + else: + return default + + return value if value is not None else default + + def save(self) -> None: + """Save configuration to file.""" + Path(self._config_path).parent.mkdir(parents=True, exist_ok=True) + with open(self._config_path, "w") as f: + yaml.dump(self._config, f) + + @property + def ollama_host(self) -> str: + """Get Ollama host URL.""" + return self.get("ollama.host", "localhost:11434") + + @property + def ollama_model(self) -> str: + """Get Ollama model name.""" + return self.get("ollama.model", "llama3") + + @property + def safety_level(self) -> str: + """Get safety level.""" + return self.get("safety.level", "moderate") + + @property + def default_shell(self) -> str: + """Get default shell type.""" + return self.get("default_shell", "bash") + + def get_config(config_path: str | None = None) -> dict[str, Any]: """Load configuration from file or return defaults.""" if config_path is None: