From 90e202333a0744cbac021cf28f3a0e6bba2eb43c Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Mon, 15 May 2006 21:30:10 +0000 Subject: 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 --- plugins/__init__.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'plugins/__init__.py') 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+' \n'+ indent+'\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: -- cgit v1.2.3