diff --git a/tren.py b/tren.py index b74f132..3495816 100755 --- a/tren.py +++ b/tren.py @@ -8,7 +8,7 @@ PROGNAME = "tren.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -RCSID = "$Id: tren.py,v 1.185 2010/03/10 23:37:27 tundra Exp $" +RCSID = "$Id: tren.py,v 1.186 2010/03/11 02:49:28 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -90,7 +90,7 @@ # List all legal command line options that will be processed by getopt() later. # We exclude -I here because it is parsed manually before the getopt() call. -OPTIONSLIST = "Ccdfhi:P:qR:r:S:tvw:Xx" # All legal command line options in getopt() format +OPTIONSLIST = "aCcdfhi:P:qR:r:S:tvw:Xx" # All legal command line options in getopt() format ##### @@ -98,6 +98,10 @@ ##### ARROW = "--->" # Used for formatting renaming messages +ASKDOREST = "!" # Do rest of renaming without asking +ASKNO = "N" # Do not rename current file +ASKQUIT = "q" # Quit renaming all further files +ASKYES = "y" # Rename current file COMMENT = "#" # Comment character in include files DEFINST = 0 # Default replacement instance DEFLEN = 80 # Default output line length @@ -180,6 +184,7 @@ # Internal program state literals ##### +ASK = "ASK" DEBUG = "DEBUG" CASESENSITIVE = "CASESENSITIVE" ESCAPE = "ESCAPE" @@ -292,8 +297,9 @@ uTable = [PROGVER, HOMEPAGE, - "usage: " + PROGNAME + " [[-CcdfhqtvwXx] [-I file] [-i instance] [-P escape] [ -R separator] [-S suffix] [-r old=new]] ... file|dir file|dir ...", + "usage: " + PROGNAME + " [[-aCcdfhqtvwXx] [-I file] [-i instance] [-P escape] [ -R separator] [-S suffix] [-r old=new]] ... file|dir file|dir ...", " where,", + " -a Turn on interactive asking before each rename (Default: Off)", " -C Do case-sensitive renaming (Default)", " -c Collapse case when doing string substitution (Default: False)", " -d Dump debugging information (Default: False)", @@ -360,7 +366,7 @@ IncludedFiles = [] ProgramOptions = { - + ASK : False, # Interactively ask user before renaming each file DEBUG : False, # Debugging off CASESENSITIVE : True, # Search is case-sensitive ESCAPE : DEFESC, # Escape string @@ -417,7 +423,9 @@ } self.RenRequests = [ - { OLD : old rename string, + { + ASK : interactive ask flag + OLD : old rename string, NEW : new rename string, DEBUG : debug flag, CASESENSITIVE : case sensitivity flag, @@ -779,17 +787,47 @@ InfoMsg(indent + iRENSKIPPED % (fullnew, fullold)) doit = False + if doit: - self.NewNames.append(fullnew) - InfoMsg(indent + iRENAMING % (fullold, fullnew)) + if ProgramOptions[ASK]: - if not ProgramOptions[TESTMODE]: + answer = "" + while answer.lower() not in [ASKNO.lower(), ASKYES.lower(), ASKDOREST.lower(), ASKQUIT.lower()]: - try: - os.rename(fullold, fullnew) - except OSError as e: - ErrorMsg(eRENAMEFAIL % (fullold, fullnew, e.args[1])) + PrintStdout("Rename %s to %s? [%s]: " % (fullold, fullnew, ASKNO+ASKYES+ASKDOREST+ASKQUIT), TRAILING="") + + answer = sys.stdin.readline().lower().strip() + + # A blank line means take the default - do nothing. + + if not answer: + answer = ASKNO.lower() + + if answer == ASKNO.lower(): + doit = False + + if answer == ASKYES.lower(): + doit = True + + if answer == ASKDOREST.lower(): + doit = True + ProgramOptions[ASK] = False + + if answer == ASKQUIT.lower(): + sys.exit(1) + + if doit: + + self.NewNames.append(fullnew) + InfoMsg(indent + iRENAMING % (fullold, fullnew)) + + if not ProgramOptions[TESTMODE]: + + try: + os.rename(fullold, fullnew) + except OSError as e: + ErrorMsg(eRENAMEFAIL % (fullold, fullnew, e.args[1])) self.indentlevel -= 1 @@ -1424,6 +1462,11 @@ for opt, val in opts: + # Turn on interactive "ask" mode + + if opt == "-a": + ProgramOptions[ASK] = True + # Select case-sensitivity for replacements (or not) if opt == "-C":