From 460b4b2e182c6c740b5faf5c42803c3c263ee269 Mon Sep 17 00:00:00 2001 From: Thomas Wilson Date: Tue, 31 Dec 2019 23:21:40 +0800 Subject: [PATCH] Implement fallback methods. Closes #11 --- configspec/manager.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/configspec/manager.py b/configspec/manager.py index 6420b23..ef095eb 100644 --- a/configspec/manager.py +++ b/configspec/manager.py @@ -10,6 +10,32 @@ class ConfigManager(): self.root_config = {} # validated config self.confspecs = {} self.frozen_config = {} # validated config to reapply each load + self._saved_state = {} + + def save_fallback(self): + """ + Save the current state of the ConfigManager for future restoration with ``fallback()``. + Includes the loaded config source, the validated config, any added config specifications, + and any frozen values. + """ + self._saved_state["config_source"] = deepcopy(self.config_source) + self._saved_state["root_config"] = deepcopy(self.root_config) + self._saved_state["confspecs"] = deepcopy(self.confspecs) + self._saved_state["frozen_config"] = deepcopy(self.frozen_config) + + def fallback(self): + """ + Restore the state of the ConfigManager to what is was when ``save_fallback()`` was last + called. Includes the loaded config source, the validated config, any added config + specifications, and any frozen values. + """ + if not all(k in self._saved_state for k in ("config_source", "root_config", + "confspecs", "frozen_config")): + raise Exception("Can't fallback ConfigManager without calling save_fallback() first!") + self.config_source = self._saved_state["config_source"] + self.root_config = self._saved_state["root_config"] + self.confspecs = self._saved_state["confspecs"] + self.frozen_config = self._saved_state["frozen_config"] @staticmethod def _get_source(source):