from pydantic import BaseModel from config.paths import Paths from models import IocsOverview, MasterHLANames, MasterIocSubs, ServiceManagerUI, ServiceRegistry class ServicesContext(BaseModel): paths: Paths _write_paths: Paths | None = None service_registry: ServiceRegistry master_hla_names: MasterHLANames iocs_overview: IocsOverview master_ioc_subs: MasterIocSubs service_manager_ui: ServiceManagerUI @property def write_paths(self) -> Paths: """ Mainly used to change the write location in tests """ if self._write_paths is None: return self.paths return self._write_paths @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 backup of all configs for rollback.""" return ServicesContext( paths=self.paths, service_registry=self.service_registry.model_copy(deep=True), master_hla_names=self.master_hla_names.model_copy(deep=True), iocs_overview=self.iocs_overview.model_copy(deep=True), master_ioc_subs=self.master_ioc_subs.model_copy(deep=True), service_manager_ui=self.service_manager_ui.model_copy(deep=True), ) def write_all(self) -> None: """Persists all modified objects to disk.""" self.service_registry.write_to_file(self.write_paths.service_registry_file) self.master_hla_names.write_to_file(self.write_paths.master_hla_names_file) self.iocs_overview.write_to_file(self.write_paths.iocs_overview_filename) self.master_ioc_subs.write_to_file(self.write_paths.master_ioc_subs_file) self.service_manager_ui.write_to_file(self.write_paths.service_manager_ui_file)