feat: add config manager module with YAML loading for settings and services
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
44
backend/modules/config_manager.py
Normal file
44
backend/modules/config_manager.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
class ConfigManager:
|
||||
def __init__(self, settings_path: str, services_path: str):
|
||||
self._settings_path = settings_path
|
||||
self._services_path = services_path
|
||||
self._settings: dict[str, Any] = {}
|
||||
self._services: dict[str, Any] = {}
|
||||
|
||||
def _load_yaml(self, path: str) -> dict[str, Any]:
|
||||
p = Path(path)
|
||||
if not p.exists():
|
||||
return {}
|
||||
with open(p) as f:
|
||||
return yaml.safe_load(f) or {}
|
||||
|
||||
def get_settings(self) -> dict[str, Any]:
|
||||
if not self._settings:
|
||||
self._settings = self._load_yaml(self._settings_path)
|
||||
return self._settings
|
||||
|
||||
def get_nodes(self) -> list[dict[str, Any]]:
|
||||
if not self._services:
|
||||
self._services = self._load_yaml(self._services_path)
|
||||
return self._services.get("nodes", [])
|
||||
|
||||
def get_node_by_ip(self, ip: str) -> dict[str, Any] | None:
|
||||
for node in self.get_nodes():
|
||||
if node.get("ip") == ip:
|
||||
return node
|
||||
return None
|
||||
|
||||
def get_network_scan_config(self) -> dict[str, Any]:
|
||||
if not self._services:
|
||||
self._services = self._load_yaml(self._services_path)
|
||||
return self._services.get("network_scan", {})
|
||||
|
||||
def reload(self) -> None:
|
||||
self._settings = {}
|
||||
self._services = {}
|
||||
Reference in New Issue
Block a user