diff options
author | Aaron LI <aly@aaronly.me> | 2019-02-27 22:46:01 +0800 |
---|---|---|
committer | Aaron LI <aly@aaronly.me> | 2019-02-27 23:57:12 +0800 |
commit | 25f0b980d7b9001fe6e32df81a839082f7759580 (patch) | |
tree | c2987c84217ca705d0932c1e82d065c5c4342e3d /fg21sim | |
parent | eddca6e37e3d8079a4f2bf4653588dd98f789f37 (diff) | |
download | fg21sim-25f0b980d7b9001fe6e32df81a839082f7759580.tar.bz2 |
configs/manager: Clean up save()
Diffstat (limited to 'fg21sim')
-rw-r--r-- | fg21sim/configs/manager.py | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/fg21sim/configs/manager.py b/fg21sim/configs/manager.py index c93eb28..9f5f3b3 100644 --- a/fg21sim/configs/manager.py +++ b/fg21sim/configs/manager.py @@ -588,7 +588,7 @@ class ConfigManager: data = _flatten_dict(data) return data - def save(self, outfile=None, clobber=False, backup=True): + def save(self, outfile=None, clobber=False): """ Save the configurations to file. @@ -598,13 +598,9 @@ class ConfigManager: The path to the output configuration file. If not provided, then use ``self.userconfig``, however, set ``clobber=True`` may be required. - NOTE: - This must be an *absolute path*. - Prefix ``~`` (tilde) is allowed and will be expanded. + NOTE: Must be an *absolute path*. clobber : bool, optional - Overwrite the output file if already exists. - backup : bool, optional - Backup the output file with suffix ``.old`` if already exists. + Whether to backup existing output file and overwrite it? Raises ------ @@ -621,19 +617,19 @@ class ConfigManager: outfile = self.userconfig logger.warning("outfile not provided, " + "use self.userconfig: {0}".format(outfile)) + outfile = os.path.expanduser(outfile) if not os.path.isabs(outfile): raise ValueError("not an absolute path: {0}".format(outfile)) + if os.path.exists(outfile): if clobber: - # Make a backup with suffix ``.old`` backfile = outfile + ".old" shutil.copyfile(outfile, backfile) logger.info("Backed up old configuration file as: " + backfile) else: 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: + + with open(outfile, "wb") as f: # binary mode required self._config.indent_type = " " self._config.write(f) |