Implement fallback methods. Closes #11

master
Tom Wilson 6 years ago
parent ac4d089417
commit 460b4b2e18

@ -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):

Loading…
Cancel
Save