47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
import yaml
|
|
from pathlib import Path
|
|
from typing import Dict, Any, Optional
|
|
|
|
class ConMan:
|
|
def __init__(self, file_path: str = None) -> None:
|
|
if file_path is None:
|
|
self.file_path = Path.home() / '.config' / 'wiederkunft' / 'config.yaml'
|
|
else:
|
|
self.file_path = Path(file_path)
|
|
|
|
self.config: Dict[str, Any] = {}
|
|
self.load_config()
|
|
|
|
def load_config(self) -> None:
|
|
if not self.file_path.exists():
|
|
raise FileNotFoundError(f"Config file not found: {self.file_path}")
|
|
|
|
with open(self.file_path, 'r') as f:
|
|
self.config = yaml.safe_load(f) or {}
|
|
|
|
def get(self, key: str, default: Optional[Any] = None) -> Any:
|
|
keys = key.split('.')
|
|
value = self.config
|
|
|
|
try:
|
|
for k in keys:
|
|
value = value[k]
|
|
return value
|
|
except (KeyError, TypeError):
|
|
return default
|
|
|
|
def __getitem__(self, key: str) -> Any:
|
|
return self.get(key)
|
|
|
|
def __contains__(self, key: str) -> bool:
|
|
try:
|
|
self.get(key)
|
|
return True
|
|
except KeyError:
|
|
return False
|
|
|
|
def reload(self) -> None:
|
|
self.load_config()
|
|
|
|
def get_all(self) -> Dict[str, Any]:
|
|
return self.config
|