Fix CI issues: add Config class, fix config access patterns, remove unused imports
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
@@ -10,6 +10,97 @@ CONFIG_DIR = Path(os.environ.get("XDG_CONFIG_HOME", Path.home() / ".config")) /
|
|||||||
CONFIG_FILE = CONFIG_DIR / "config.yaml"
|
CONFIG_FILE = CONFIG_DIR / "config.yaml"
|
||||||
|
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
"""Configuration class for ShellGenius."""
|
||||||
|
|
||||||
|
def __init__(self, config_path: str | None = None):
|
||||||
|
"""Initialize configuration.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
config_path: Path to config file
|
||||||
|
"""
|
||||||
|
if config_path is None:
|
||||||
|
config_path = str(CONFIG_FILE)
|
||||||
|
|
||||||
|
self._config_path = config_path
|
||||||
|
self._config = self._load_config()
|
||||||
|
|
||||||
|
def _load_config(self) -> dict[str, Any]:
|
||||||
|
"""Load configuration from file.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Configuration dictionary
|
||||||
|
"""
|
||||||
|
defaults = {
|
||||||
|
"ollama": {
|
||||||
|
"host": "localhost:11434",
|
||||||
|
"model": "llama3",
|
||||||
|
},
|
||||||
|
"safety": {
|
||||||
|
"level": "moderate",
|
||||||
|
},
|
||||||
|
"default_shell": "bash",
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(self._config_path) as f:
|
||||||
|
config = yaml.safe_load(f) or {}
|
||||||
|
except FileNotFoundError:
|
||||||
|
config = {}
|
||||||
|
|
||||||
|
if isinstance(config, dict):
|
||||||
|
defaults.update(config)
|
||||||
|
|
||||||
|
return defaults
|
||||||
|
|
||||||
|
def get(self, key: str, default: Any = None) -> Any:
|
||||||
|
"""Get configuration value by key.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
key: Dot-separated key (e.g., "ollama.host")
|
||||||
|
default: Default value if key not found
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Configuration value
|
||||||
|
"""
|
||||||
|
keys = key.split(".")
|
||||||
|
value: Any = self._config
|
||||||
|
|
||||||
|
for k in keys:
|
||||||
|
if isinstance(value, dict):
|
||||||
|
value = value.get(k) if k in value else None
|
||||||
|
else:
|
||||||
|
return default
|
||||||
|
|
||||||
|
return value if value is not None else default
|
||||||
|
|
||||||
|
def save(self) -> None:
|
||||||
|
"""Save configuration to file."""
|
||||||
|
Path(self._config_path).parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
with open(self._config_path, "w") as f:
|
||||||
|
yaml.dump(self._config, f)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ollama_host(self) -> str:
|
||||||
|
"""Get Ollama host URL."""
|
||||||
|
return self.get("ollama.host", "localhost:11434")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ollama_model(self) -> str:
|
||||||
|
"""Get Ollama model name."""
|
||||||
|
return self.get("ollama.model", "llama3")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def safety_level(self) -> str:
|
||||||
|
"""Get safety level."""
|
||||||
|
return self.get("safety.level", "moderate")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def default_shell(self) -> str:
|
||||||
|
"""Get default shell type."""
|
||||||
|
return self.get("default_shell", "bash")
|
||||||
|
|
||||||
|
|
||||||
def get_config(config_path: str | None = None) -> dict[str, Any]:
|
def get_config(config_path: str | None = None) -> dict[str, Any]:
|
||||||
"""Load configuration from file or return defaults."""
|
"""Load configuration from file or return defaults."""
|
||||||
if config_path is None:
|
if config_path is None:
|
||||||
|
|||||||
Reference in New Issue
Block a user