From c23498126941c94fd1bbf521995684b7e02bf1d5 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Tue, 15 Nov 2016 20:16:09 +0800 Subject: configs/manager.py: Keep configuration file comments * Enable the "copy" mode when validating, which also copies all the comments from the configspec to the validated configurations; * Use "copy.deepcopy()" to make a deep copy of the configurations, which also copies all the comments. Since the comments are preserved, therefore they are also written together to the saved/output configuration file. --- fg21sim/configs/manager.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/fg21sim/configs/manager.py b/fg21sim/configs/manager.py index 70c32b5..eb6b1d0 100644 --- a/fg21sim/configs/manager.py +++ b/fg21sim/configs/manager.py @@ -17,6 +17,7 @@ import operator from functools import reduce from collections import MutableMapping import pkg_resources +import copy from configobj import ConfigObj, ConfigObjError, flatten_errors from validate import Validator @@ -144,13 +145,12 @@ class ConfigManager: configspec = _get_configspec() self._configspec = ConfigObj(configspec, interpolation=False, list_values=False, _inspec=True) - # FIXME/NOTE: The comments are LOST! configs_default = ConfigObj(interpolation=False, configspec=self._configspec) # Keep a copy of the default configurations self._config_default = self._validate(configs_default) - # NOTE: `_config_default.copy()` only returns a *shallow* copy. - self._config = ConfigObj(self._config_default, interpolation=False) + # NOTE: use ``copy.deepcopy``; see ``self.reset()`` for more details + self._config = copy.deepcopy(self._config_default) if userconfig: self.read_userconfig(userconfig) @@ -226,8 +226,10 @@ class ConfigManager: NOTE: Also reset ``self.userconfig`` to ``None``. """ - # NOTE: `_config_default.copy()` only returns a *shallow* copy. - self._config = ConfigObj(self._config_default, interpolation=False) + # NOTE: + # * ``_config_default.copy()`` only returns a *shallow* copy. + # * ``ConfigObj(_config_default)`` will lost all comments + self._config = copy.deepcopy(self._config_default) self.userconfig = None logger.warning("Reset the configurations to the copy of defaults!") @@ -238,7 +240,11 @@ class ConfigManager: """ validator = Validator() try: - results = config.validate(validator, preserve_errors=True) + # NOTE: + # Use the "copy" mode, which will copy both the default values + # and all the comments. + results = config.validate(validator, preserve_errors=True, + copy=True) except ConfigObjError as e: raise ConfigError(e) if results is not True: -- cgit v1.2.2