diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2006-05-15 23:30:10 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2006-05-15 23:30:10 +0200 |
commit | 90e202333a0744cbac021cf28f3a0e6bba2eb43c (patch) | |
tree | 13722364b0671a2bbdbb6c93fae538c9c8cf3e81 /plugins/__init__.py | |
parent | 3608d831b1728bd84fe1fc70ec7317edaaa03156 (diff) |
add makebackup option to open_file() so we can implement updating files (e.g. serialization files)
git-svn-id: http://arthurdejong.org/svn/webcheck/webcheck@273 86f53f14-5ff3-0310-afe5-9b438ce3f40c
Diffstat (limited to 'plugins/__init__.py')
-rw-r--r-- | plugins/__init__.py | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/plugins/__init__.py b/plugins/__init__.py index bbcbbac..92aa772 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -156,7 +156,7 @@ def print_parents(fp, link, indent=' '): indent+' </ul>\n'+ indent+'</div>\n' ) -def open_file(filename, istext=True): +def open_file(filename, istext=True, makebackup=False): """This returns an open file object which can be used for writing. This file is created in the output directory. The output directory (stored in config.OUTPUT_DIR is created if it does not yet exist. If the second @@ -173,15 +173,20 @@ def open_file(filename, istext=True): sys.exit(1) # build the output file name fname = os.path.join(config.OUTPUT_DIR, filename) - # check if file exists and ask to overwrite - if os.path.exists(fname) and not config.OVERWRITE_FILES: - res = raw_input('webcheck: overwrite %s? [y]es, [a]ll, [q]uit: ' % fname) - res = res.lower() + ' ' - if res[0] == 'a': - config.OVERWRITE_FILES = True - elif res[0] != 'y': - print 'Aborted.' - sys.exit(1) + # check if file exists + if os.path.exists(fname): + if makebackup: + # create backup of original (overwriting previous backup) + os.rename(fname, fname+'~') + elif not config.OVERWRITE_FILES: + # ask to overwrite + res = raw_input('webcheck: overwrite %s? [y]es, [a]ll, [q]uit: ' % fname) + res = res.lower() + ' ' + if res[0] == 'a': + config.OVERWRITE_FILES = True + elif res[0] != 'y': + print 'Aborted.' + sys.exit(1) # open the file for writing try: if istext: |