Initial upload of auto-changelog-generator
This commit is contained in:
92
src/changeloggen/config.py
Normal file
92
src/changeloggen/config.py
Normal file
@@ -0,0 +1,92 @@
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
import os
|
||||
import yaml
|
||||
|
||||
|
||||
class Config:
|
||||
"""Configuration for changeloggen."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
ollama_url: str = "http://localhost:11434",
|
||||
model: str = "llama3.2",
|
||||
temperature: float = 0.3,
|
||||
output_format: str = "markdown",
|
||||
include_unstaged: bool = False,
|
||||
custom_prompts: Optional[dict] = None
|
||||
):
|
||||
self.ollama_url = ollama_url
|
||||
self.model = model
|
||||
self.temperature = temperature
|
||||
self.output_format = output_format
|
||||
self.include_unstaged = include_unstaged
|
||||
self.custom_prompts = custom_prompts or {}
|
||||
|
||||
|
||||
def find_config_file() -> Optional[Path]:
|
||||
"""Find configuration file in standard locations."""
|
||||
search_paths = [
|
||||
Path(".changeloggen.yaml"),
|
||||
Path(".changeloggen.yml"),
|
||||
Path(os.path.expanduser("~/.config/changeloggen/config.yaml")),
|
||||
]
|
||||
|
||||
for path in search_paths:
|
||||
if path.exists():
|
||||
return path
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def load_config(config_path: Optional[Path] = None) -> Config:
|
||||
"""Load configuration from file or environment variables."""
|
||||
if config_path is None:
|
||||
config_path = find_config_file()
|
||||
|
||||
config = Config()
|
||||
|
||||
if config_path and config_path.exists():
|
||||
try:
|
||||
with open(config_path, 'r') as f:
|
||||
data = yaml.safe_load(f) or {}
|
||||
|
||||
if 'ollama_url' in data:
|
||||
config.ollama_url = data['ollama_url']
|
||||
if 'model' in data:
|
||||
config.model = data['model']
|
||||
if 'temperature' in data:
|
||||
config.temperature = float(data['temperature'])
|
||||
if 'output_format' in data:
|
||||
config.output_format = data['output_format']
|
||||
if 'include_unstaged' in data:
|
||||
config.include_unstaged = data['include_unstaged']
|
||||
if 'custom_prompts' in data:
|
||||
config.custom_prompts = data['custom_prompts']
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
config.ollama_url = os.environ.get("CHANGELOGGEN_OLLAMA_URL", config.ollama_url)
|
||||
config.model = os.environ.get("CHANGELOGGEN_MODEL", config.model)
|
||||
|
||||
if os.environ.get("CHANGELOGGEN_NO_COLOR"):
|
||||
os.environ["NO_COLOR"] = os.environ.get("CHANGELOGGEN_NO_COLOR")
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def save_config(config: Config, config_path: Path) -> None:
|
||||
"""Save configuration to file."""
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
data = {
|
||||
'ollama_url': config.ollama_url,
|
||||
'model': config.model,
|
||||
'temperature': config.temperature,
|
||||
'output_format': config.output_format,
|
||||
'include_unstaged': config.include_unstaged,
|
||||
'custom_prompts': config.custom_prompts
|
||||
}
|
||||
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(data, f, default_flow_style=False)
|
||||
Reference in New Issue
Block a user