This commit is contained in:
109
app/src/git_commit_generator/config.py
Normal file
109
app/src/git_commit_generator/config.py
Normal file
@@ -0,0 +1,109 @@
|
||||
"""Configuration management for git-commit-message-generator."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
class Config:
|
||||
"""Configuration management class."""
|
||||
|
||||
def __init__(self, config_path: Optional[str] = None):
|
||||
"""Initialize configuration.
|
||||
|
||||
Args:
|
||||
config_path: Path to config file. Defaults to None.
|
||||
"""
|
||||
self.config_path = config_path or os.environ.get(
|
||||
"GIT_COMMIT_GENERATOR_CONFIG"
|
||||
)
|
||||
self._config = self._load_config()
|
||||
|
||||
def _load_config(self) -> dict:
|
||||
"""Load configuration from file.
|
||||
|
||||
Returns:
|
||||
Dictionary containing configuration.
|
||||
"""
|
||||
config = {
|
||||
"ollama_host": os.environ.get("OLLAMA_HOST", "http://localhost:11434"),
|
||||
"ollama_model": os.environ.get("OLLAMA_MODEL", "llama3"),
|
||||
"prompt_dir": os.environ.get(
|
||||
"PROMPT_DIR", str(Path(__file__).parent.parent / "prompts")
|
||||
),
|
||||
}
|
||||
|
||||
if self.config_path:
|
||||
config_file = Path(self.config_path)
|
||||
if config_file.exists():
|
||||
with open(config_file, "r") as f:
|
||||
file_config = yaml.safe_load(f) or {}
|
||||
config.update(file_config)
|
||||
|
||||
return config
|
||||
|
||||
@property
|
||||
def ollama_host(self) -> str:
|
||||
"""Get Ollama host URL.
|
||||
|
||||
Returns:
|
||||
Ollama host URL.
|
||||
"""
|
||||
return self._config["ollama_host"]
|
||||
|
||||
@property
|
||||
def ollama_model(self) -> str:
|
||||
"""Get default Ollama model.
|
||||
|
||||
Returns:
|
||||
Ollama model name.
|
||||
"""
|
||||
return self._config["ollama_model"]
|
||||
|
||||
@property
|
||||
def prompt_dir(self) -> str:
|
||||
"""Get prompt directory path.
|
||||
|
||||
Returns:
|
||||
Path to prompt templates directory.
|
||||
"""
|
||||
return self._config["prompt_dir"]
|
||||
|
||||
def get_prompt_path(self, prompt_name: str) -> Path:
|
||||
"""Get full path to a prompt file.
|
||||
|
||||
Args:
|
||||
prompt_name: Name of the prompt file.
|
||||
|
||||
Returns:
|
||||
Full path to the prompt file.
|
||||
"""
|
||||
return Path(self.prompt_dir) / prompt_name
|
||||
|
||||
def read_prompt(self, prompt_name: str) -> str:
|
||||
"""Read a prompt template file.
|
||||
|
||||
Args:
|
||||
prompt_name: Name of the prompt file.
|
||||
|
||||
Returns:
|
||||
Content of the prompt file.
|
||||
"""
|
||||
prompt_path = self.get_prompt_path(prompt_name)
|
||||
if prompt_path.exists():
|
||||
with open(prompt_path, "r") as f:
|
||||
return f.read()
|
||||
return ""
|
||||
|
||||
|
||||
def get_config(config_path: Optional[str] = None) -> Config:
|
||||
"""Get configuration instance.
|
||||
|
||||
Args:
|
||||
config_path: Optional path to config file.
|
||||
|
||||
Returns:
|
||||
Config instance.
|
||||
"""
|
||||
return Config(config_path)
|
||||
Reference in New Issue
Block a user