refactor: add paths, services ctx, and extract models to own files

This commit is contained in:
Benjamin Labrecque
2026-07-22 12:04:55 +02:00
parent e324e3abed
commit 7e690a907e
16 changed files with 474 additions and 524 deletions
+54
View File
@@ -0,0 +1,54 @@
from typing import Any, Dict
from pydantic import BaseModel
from config.paths import Paths
from models import IocsOverview, MasterHLANames, MasterIocSubs, ServiceManagerUI, ServiceRegistry
class ServicesContext(BaseModel):
paths: Paths
service_registry: ServiceRegistry
master_hla_names: MasterHLANames
iocs_overview: IocsOverview
master_ioc_subs: MasterIocSubs
service_manager_ui: ServiceManagerUI
@classmethod
def load_from_disk(cls, paths: Paths) -> "ServicesContext":
return cls(
paths=paths,
service_registry=ServiceRegistry.read_from_file(paths.service_registry_file),
master_hla_names=MasterHLANames.read_from_file(paths.master_hla_names_file),
iocs_overview=IocsOverview.read_from_file(paths.iocs_overview_filename),
master_ioc_subs=MasterIocSubs.read_from_file(paths.master_ioc_subs_file),
service_manager_ui=ServiceManagerUI.read_from_file(paths.service_manager_ui_file),
)
def snapshot(self) -> "ServicesContext":
"""Creates an in-memory backup of all configs for rollback."""
return ServicesContext(
paths=self.paths,
service_registry=self.service_registry.model_copy(),
master_hla_names=self.master_hla_names.model_copy(),
iocs_overview=self.iocs_overview.model_copy(),
master_ioc_subs=self.master_ioc_subs.model_copy(),
service_manager_ui=self.service_manager_ui.model_copy(),
)
def restore_snapshot(self, snapshot: Dict[str, Any]) -> None:
"""Restores state from a backup dictionary."""
self.service_registry = snapshot["service_registry"]
self.master_hla_names = snapshot["master_hla_names"]
self.iocs_overview = snapshot["iocs_overview"]
self.master_ioc_subs = snapshot["master_ioc_subs"]
self.service_manager_ui = snapshot["service_manager_ui"]
def write_all(self) -> None:
"""Persists all modified objects to disk."""
self.service_registry.write_to_file(self.paths.service_registry_file)
self.master_hla_names.write_to_file(self.paths.master_hla_names_file)
self.iocs_overview.write_to_file(self.paths.iocs_overview_filename)
self.master_ioc_subs.write_to_file(self.paths.master_ioc_subs_file)
self.service_manager_ui.write_to_file(self.paths.service_manager_ui_file)