From fe4d19b75048a2a5f0d362d46fc2eb17bacb636d Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 1 Feb 2026 19:31:05 +0000 Subject: [PATCH] Initial upload: Git AI Documentation Generator v0.1.0 --- src/config.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/config.py diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..c00ff2c --- /dev/null +++ b/src/config.py @@ -0,0 +1,93 @@ +"""Configuration module for git-ai-doc-generator.""" + +from dataclasses import dataclass +from pathlib import Path + +import yaml + + +@dataclass +class Config: + """Configuration for git-ai-doc-generator.""" + + ollama_host: str = "http://localhost:11434" + model: str = "llama3.2" + debug: bool = False + + +def find_config_file() -> Path | None: + """Find the config file in current directory or home. + + Returns: + Path to config file or None. + """ + config_names = [".ai-doc-gen.yaml", "ai-doc-gen.yaml", ".git-ai-doc.yaml"] + search_paths = [Path.cwd(), Path.home()] + + for search_path in search_paths: + for config_name in config_names: + config_path = search_path / config_name + if config_path.exists(): + return config_path + return None + + +def load_config(config_path: str | None = None) -> Config: + """Load configuration from file. + + Args: + config_path: Path to config file. If None, searches for default locations. + + Returns: + Config object. + """ + config = Config() + + if config_path: + path = Path(config_path) + if path.exists(): + _load_from_file(path, config) + return config + + default_path = find_config_file() + if default_path: + _load_from_file(default_path, config) + + return config + + +def _load_from_file(path: Path, config: Config) -> None: + """Load configuration from a YAML file. + + Args: + path: Path to the config file. + config: Config object to update. + """ + try: + with open(path) as f: + data = yaml.safe_load(f) + if data: + if "ollama_host" in data: + config.ollama_host = data["ollama_host"] + if "model" in data: + config.model = data["model"] + if "debug" in data: + config.debug = data["debug"] + except (yaml.YAMLError, OSError): + pass + + +def save_config(config: Config, path: Path) -> None: + """Save configuration to a YAML file. + + Args: + config: Config object to save. + path: Path to save the config file. + """ + data = { + "ollama_host": config.ollama_host, + "model": config.model, + "debug": config.debug, + } + with open(path, "w") as f: + yaml.safe_dump(data, f)