diff options
| author | Aaron LI <aaronly.me@outlook.com> | 2016-11-16 21:29:49 +0800 | 
|---|---|---|
| committer | Aaron LI <aaronly.me@outlook.com> | 2016-11-16 21:29:49 +0800 | 
| commit | e6e3027e6dd29febd1e72ad044ba3ccc1cd455a5 (patch) | |
| tree | 74cea29b0c7a629e610e5797bbbb7a4e9929643c /fg21sim/configs | |
| parent | cfd21163c06013b0637047382e32527f223973c5 (diff) | |
| download | fg21sim-e6e3027e6dd29febd1e72ad044ba3ccc1cd455a5.tar.bz2 | |
configs/manager.py: Make a backup when output file already exists
Also expand the output file for the prefix "~" (tilde)
Diffstat (limited to 'fg21sim/configs')
| -rw-r--r-- | fg21sim/configs/manager.py | 19 | 
1 files changed, 15 insertions, 4 deletions
| diff --git a/fg21sim/configs/manager.py b/fg21sim/configs/manager.py index 59af78a..964bcfc 100644 --- a/fg21sim/configs/manager.py +++ b/fg21sim/configs/manager.py @@ -18,6 +18,7 @@ from functools import reduce  from collections import MutableMapping  import pkg_resources  import copy +import shutil  from configobj import ConfigObj, ConfigObjError, flatten_errors  from validate import Validator @@ -540,7 +541,7 @@ class ConfigManager:          #          return data -    def save(self, outfile=None, clobber=False): +    def save(self, outfile=None, clobber=False, backup=True):          """Save the configurations to file.          Parameters @@ -549,9 +550,13 @@ 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*. +            NOTE: +            This must be an *absolute path*. +            Prefix ``~`` (tilde) is allowed and will be expanded.          clobber : bool, optional              Overwrite the output file if already exists. +        backup : bool, optional +            Backup the output file with suffix ``.old`` if already exists.          Raises          ------ @@ -568,10 +573,16 @@ 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) and not clobber: -            raise OSError("outfile already exists: {0}".format(outfile)) +        if os.path.exists(outfile): +            if clobber: +                # Make a backup with suffix ``.old`` +                backfile = outfile + ".old" +                shutil.copyfile(outfile, 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: | 
