Files
env-pro/app/env_pro/core/config.py
2026-01-31 01:35:43 +00:00

58 lines
1.8 KiB
Python

"""Configuration management for env-pro."""
import os
from pathlib import Path
from typing import Optional
import yaml
class Config:
"""Configuration management class."""
def __init__(self, config_home: Optional[Path] = None):
"""Initialize configuration."""
if config_home is None:
config_home = Path(os.environ.get("ENV_PRO_HOME", "~/.config/env-pro"))
self.config_home = config_home.expanduser()
self.config_file = self.config_home / "config.yaml"
def get(self, key: str, default=None):
"""Get a configuration value."""
config = self._load_config()
return config.get(key, default)
def set(self, key: str, value):
"""Set a configuration value."""
config = self._load_config()
config[key] = value
self._save_config(config)
def _load_config(self) -> dict:
"""Load configuration from file."""
if self.config_file.exists():
try:
with open(self.config_file, 'r') as f:
return yaml.safe_load(f) or {}
except yaml.YAMLError:
return {}
return {}
def _save_config(self, config: dict):
"""Save configuration to file."""
self.config_home.mkdir(parents=True, exist_ok=True)
with open(self.config_file, 'w') as f:
yaml.dump(config, f)
def get_keyring_path(self) -> str:
"""Get the keyring service name for storing keys."""
return self.get("keyring_service", "env-pro")
def set_keyring_path(self, service: str):
"""Set the keyring service name."""
self.set("keyring_service", service)
def get_config(config_home: Optional[Path] = None) -> Config:
"""Get the global configuration instance."""
return Config(config_home)