62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
"""
|
|
Config Loader
|
|
|
|
Loads and caches tool configurations from YAML files.
|
|
"""
|
|
|
|
import yaml
|
|
from pathlib import Path
|
|
from typing import Dict, Any, Optional
|
|
|
|
|
|
class ConfigLoader:
|
|
"""Tool configuration loader with caching support"""
|
|
|
|
def __init__(self, config_dir: Optional[Path] = None):
|
|
if config_dir is None:
|
|
config_dir = Path(__file__).parent
|
|
self.config_dir = Path(config_dir)
|
|
self._cache: Dict[str, Any] = {}
|
|
|
|
def load(self, tool_name: str) -> Dict[str, Any]:
|
|
"""Load configuration for a specific tool"""
|
|
if tool_name in self._cache:
|
|
return self._cache[tool_name]
|
|
|
|
config_file = self.config_dir / f"{tool_name}.yaml"
|
|
if not config_file.exists():
|
|
# Try without .yaml extension
|
|
config_file = self.config_dir / tool_name
|
|
|
|
if not config_file.exists():
|
|
return {}
|
|
|
|
with open(config_file, encoding="utf-8") as f:
|
|
config = yaml.safe_load(f) or {}
|
|
|
|
self._cache[tool_name] = config
|
|
return config
|
|
|
|
def reload(self, tool_name: str) -> Dict[str, Any]:
|
|
"""Reload configuration for a specific tool"""
|
|
if tool_name in self._cache:
|
|
del self._cache[tool_name]
|
|
return self.load(tool_name)
|
|
|
|
def get(self, tool_name: str, key: str, default: Any = None) -> Any:
|
|
"""Get a specific configuration value"""
|
|
config = self.load(tool_name)
|
|
return config.get(key, default)
|
|
|
|
def clear_cache(self) -> None:
|
|
"""Clear the configuration cache"""
|
|
self._cache.clear()
|
|
|
|
def load_all(self) -> Dict[str, Dict[str, Any]]:
|
|
"""Load all configuration files in the config directory"""
|
|
all_configs = {}
|
|
for config_file in self.config_dir.glob("*.yaml"):
|
|
tool_name = config_file.stem
|
|
all_configs[tool_name] = self.load(tool_name)
|
|
return all_configs
|