Initial upload: PatternForge CLI tool with pattern detection and boilerplate generation
This commit is contained in:
59
src/patternforge/config.py
Normal file
59
src/patternforge/config.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
default_patterns: Path | None = None,
|
||||||
|
template_dir: Path | None = None,
|
||||||
|
language: str = "python",
|
||||||
|
indent_size: int = 4,
|
||||||
|
custom_config: dict[str, Any] | None = None,
|
||||||
|
) -> None:
|
||||||
|
self._config: dict[str, Any] = {}
|
||||||
|
self._config_dir = Path.home() / ".config" / "patternforge"
|
||||||
|
self._config_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
self._patterns_dir = default_patterns or self._config_dir / "patterns"
|
||||||
|
self._templates_dir = template_dir or self._config_dir / "templates"
|
||||||
|
self._patterns_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
self._templates_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
if custom_config:
|
||||||
|
self._config.update(custom_config)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def load(cls, config_path: str | None = None) -> "Config":
|
||||||
|
config = cls()
|
||||||
|
if config_path and Path(config_path).exists():
|
||||||
|
with open(config_path) as f:
|
||||||
|
data = yaml.safe_load(f) or {}
|
||||||
|
config._config.update(data)
|
||||||
|
return config
|
||||||
|
|
||||||
|
@property
|
||||||
|
def patterns_dir(self) -> Path:
|
||||||
|
return self._patterns_dir
|
||||||
|
|
||||||
|
@property
|
||||||
|
def templates_dir(self) -> Path:
|
||||||
|
return self._templates_dir
|
||||||
|
|
||||||
|
def get(self, key: str, default: Any = None) -> Any:
|
||||||
|
return self._config.get(key, default)
|
||||||
|
|
||||||
|
def set(self, key: str, value: Any) -> None:
|
||||||
|
self._config[key] = value
|
||||||
|
|
||||||
|
def save(self, config_path: str | None = None) -> None:
|
||||||
|
path = Path(config_path) if config_path else self._config_dir / "config.yaml"
|
||||||
|
with open(path, "w") as f:
|
||||||
|
yaml.dump(self.to_dict(), f)
|
||||||
|
|
||||||
|
def to_dict(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"patterns_dir": str(self._patterns_dir),
|
||||||
|
"templates_dir": str(self._templates_dir),
|
||||||
|
**self._config,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user