diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-11-08 15:55:56 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-11-08 15:55:56 +0800 |
commit | 40017dc8af4a4f46bc376b2f8561c75416424cbb (patch) | |
tree | 3ee0e36df2fae47dab631106618ee899ac74c7c4 /fg21sim/configs | |
parent | f6f382b90b6f7b3e4e66f42dba1a238453efe0ec (diff) | |
download | fg21sim-40017dc8af4a4f46bc376b2f8561c75416424cbb.tar.bz2 |
configs/manager.py: Implement the "save()" method
Diffstat (limited to 'fg21sim/configs')
-rw-r--r-- | fg21sim/configs/manager.py | 25 |
1 files 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) |