classpulse-edge / core /config_manager.py
thegoatcc's picture
Upload ClassPulse Edge codebase, Dockerfile, and model weights
af70f62 verified
Raw History Blame Contribute Delete
5.05 kB
from __future__ import annotations
from enum import Enum
from pathlib import Path
import threading
from typing import Any, Optional
import yaml
class ModuleState(str, Enum):
ENABLED = "ENABLED"
DISABLED = "DISABLED"
PAUSED = "PAUSED"
class ConfigManager:
"""Thread-safe YAML configuration and runtime Zero-Deletion toggle manager."""
def __init__(self, config_path: str | Path = "config.yaml") -> None:
self.config_path = Path(config_path)
self._lock = threading.RLock()
self._data: dict[str, Any] = {}
self.reload()
def reload(self) -> None:
with self._lock:
if not self.config_path.exists():
raise FileNotFoundError(f"Configuration file not found: {self.config_path}")
with self.config_path.open("r", encoding="utf-8") as f:
self._data = yaml.safe_load(f) or {}
def get_system(self, key: str, default: Any = None) -> Any:
with self._lock:
return self._data.get("system", {}).get(key, default)
def get_threshold(self, key: str, default: Any = None) -> Any:
with self._lock:
return self._data.get("thresholds", {}).get(key, default)
def get(self, key: str, default: Any = None) -> Any:
"""Read a dotted configuration path such as ``toggles.drink_detection``."""
with self._lock:
value: Any = self._data
for part in key.split("."):
if not isinstance(value, dict) or part not in value:
return default
value = value[part]
return value
def get_model_path(self, key: str) -> str:
with self._lock:
models = self._data.get("models", {})
aliases = {
"pose_hef": "yolo_pose",
"classroom_hef": "yolo_classroom",
"scrfd_hef": "scrfd",
"arcface_hef": "arcface",
"head_pose_onnx": "head_pose",
"face_landmarker_task": "face_landmarker",
}
model_key = key if key in models else aliases.get(key, key)
return str(models[model_key])
def get_path(self, key: str) -> Path:
with self._lock:
p = Path(self._data.get("paths", {})[key])
if p.suffix == "":
p.mkdir(parents=True, exist_ok=True)
else:
p.parent.mkdir(parents=True, exist_ok=True)
return p
def get_branch_state(self, branch_name: str) -> ModuleState:
with self._lock:
val = self._data.get("branches", {}).get(
branch_name,
self._data.get("toggles", {}).get(branch_name, ModuleState.ENABLED.value),
)
return ModuleState(val.upper())
def set_branch_state(self, branch_name: str, state: ModuleState | str) -> None:
with self._lock:
target = "toggles" if "toggles" in self._data and "branches" not in self._data else "branches"
self._data.setdefault(target, {})[branch_name] = ModuleState(state).value
def get_module_state(self, module_name: str) -> ModuleState:
with self._lock:
val = self._data.get("modules", {}).get(
module_name,
self._data.get("toggles", {}).get(module_name, ModuleState.ENABLED.value),
)
return ModuleState(val.upper())
def set_module_state(self, module_name: str, state: ModuleState | str) -> None:
with self._lock:
target = "toggles" if "toggles" in self._data and "modules" not in self._data else "modules"
self._data.setdefault(target, {})[module_name] = ModuleState(state).value
def get_sub_rule_state(self, rule_name: str) -> ModuleState:
with self._lock:
val = self._data.get("sub_rules", {}).get(rule_name, ModuleState.ENABLED.value)
return ModuleState(val.upper())
def set_sub_rule_state(self, rule_name: str, state: ModuleState | str) -> None:
with self._lock:
self._data.setdefault("sub_rules", {})[rule_name] = ModuleState(state).value
def is_active(self, module_name: str, branch_name: Optional[str] = None) -> bool:
with self._lock:
if branch_name is not None:
if self.get_branch_state(branch_name) != ModuleState.ENABLED:
return False
return self.get_module_state(module_name) == ModuleState.ENABLED
def snapshot(self) -> dict[str, Any]:
with self._lock:
return {
"system": dict(self._data.get("system", {})),
"branches": dict(self._data.get("branches", {})),
"modules": dict(self._data.get("modules", {})),
"sub_rules": dict(self._data.get("sub_rules", {})),
"thresholds": dict(self._data.get("thresholds", {})),
}