From 40017dc8af4a4f46bc376b2f8561c75416424cbb Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Tue, 8 Nov 2016 15:55:56 +0800 Subject: configs/manager.py: Implement the "save()" method --- fg21sim/configs/manager.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/fg21sim/configs/manager.py b/fg21sim/configs/manager.py index 28ff24d..4997437 100644 --- a/fg21sim/configs/manager.py +++ b/fg21sim/configs/manager.py @@ -144,6 +144,7 @@ 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 @@ -530,9 +531,6 @@ class ConfigManager: def save(self, outfile=None, clobber=False): """Save the configurations to file. - XXX/TODO: - Will the comments be preserved when save to file ?? - Parameters ---------- outfile : str, optional @@ -546,9 +544,24 @@ class ConfigManager: Raises ------ ValueError : - The given ``filepath`` is not an *absolute path*, or the - ``self.userconfig`` is invalid while the ``filepath`` not given. + The given ``outfile`` is not an *absolute path*, or the + ``self.userconfig`` is invalid while the ``outfile`` not given. OSError : If the target filename already exists. """ - raise NotImplementedError("TODO") + if outfile is None: + if self.userconfig is None: + raise ValueError("no outfile and self.userconfig is None") + else: + outfile = self.userconfig + logger.warning("outfile not provided, " + + "use self.userconfig: {0}".format(outfile)) + if not os.path.isabs(outfile): + raise ValueError("not an absolute path: {0}".format(outfile)) + if os.path.exists(outfile) and not clobber: + raise OSError("outfile already exists: {0}".format(outfile)) + # Write out the configurations + # NOTE: need open the output file in *binary* mode + with open(outfile, "wb") as f: + self._config.indent_type = " " + self._config.write(f) -- cgit v1.2.2