Initial upload: Git AI Documentation Generator v0.1.0
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
93
src/config.py
Normal file
93
src/config.py
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user