diff --git a/tren.py b/tren.py index c8f3c94..3582e13 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.135 2010/02/02 17:53:55 tundra Exp $" +RCSID = "$Id: tren.py,v 1.136 2010/02/02 18:31:42 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -91,6 +91,24 @@ NAM = "Nam" # Rename target is name RENSEP = "=" # Rename command separator: oldRENSEPnew +# Internal program state literals + +DEBUG = "DEBUG" +CASESENSITIVE = "CASESENSITIVE" +ERRORCONTINUE = "ERRORCONTINUE" +FORCERENAM = "FORCERENAM" +GLOBAL = "GLOBAL" +QUIET = "QUIET" +REGEX = "REGEX" +TARGET = "TARGET" +TESTMODE = "TESTMODE" + +# Rename string keys + +NEW = "NEW" +OLD = "OLD" + + #----------------------------------------------------------# # Prompts, & Application Strings # @@ -182,15 +200,15 @@ ProgramOptions = { - "DEBUG" : False, # Debugging off - "CASESENSITIVE" : True, # Search is case-sensitive - "ERRORCONTINUE" : False, # Do not continue after error - "FORCERENAM" : False, # Do not rename if target already exists - "GLOBAL" : False, # Only rename first instance of old string - "QUIET" : False, # Display progress - "REGEX" : False, # Do not treat old string as a regex - "TARGET" : ALL, # Can be "All", "Name", or "Ext" - "TESTMODE" : False # Global data structures + DEBUG : False, # Debugging off + CASESENSITIVE : True, # Search is case-sensitive + ERRORCONTINUE : False, # Do not continue after error + FORCERENAM : False, # Do not rename if target already exists + GLOBAL : False, # Only rename first instance of old string + QUIET : False, # Display progress + REGEX : False, # Do not treat old string as a regex + TARGET : ALL, # Can be "All", "Name", or "Ext" + TESTMODE : False # Global data structures } @@ -203,7 +221,7 @@ ##### -# Container For Holding Rename Targets +# Container For Holding Rename Targets And Renaming Requests ##### class RenameTargets: @@ -247,6 +265,10 @@ self.RenNames = {} + # Dictionary of all the renaming requests + + self.RenRequests = [] + # Ordered lists used by sequence renaming tokens args = {} # Keys = 0, Values = Rename targets from command line @@ -347,12 +369,12 @@ self.RenNames[name].append(tblz - t.index(name) - 1) # Descending Index - if ProgramOptions["DEBUG"]: + if ProgramOptions[DEBUG]: l=[] for item in vieworder: DumpList(DebugMsg, debugmsg, item, view[item]) - if ProgramOptions["DEBUG"]: + if ProgramOptions[DEBUG]: SEPARATOR = dSEPCHAR * MAXLINELEN DebugMsg(SEPARATOR) @@ -588,7 +610,7 @@ n += l.split() f.close() - if ProgramOptions["DEBUG"]: + if ProgramOptions[DEBUG]: DebugMsg(dINCLUDING % file) OPTIONS = lhs + n + rhs @@ -653,7 +675,7 @@ # debug info about includes if DEBUGFLAG in OPTIONS: - ProgramOptions["DEBUG"] = True + ProgramOptions[DEBUG] = True # Deal with include files @@ -663,9 +685,7 @@ # to get debug info on command line expansion if DEBUGFLAG in OPTIONS: - ProgramOptions["DEBUG"] = True - -RenRequests = [] + ProgramOptions[DEBUG] = True try: opts, args = getopt.getopt(OPTIONS, '1abbCcdEefghl:qR:r:tvw:Xx]') @@ -673,47 +693,76 @@ ErrorMsg(eBADARG % e.args[0]) sys.exit(1) +# Create and populate an object with rename targets. We have to +# This must be done here because this object also stores the +# -r renaming requests we may find in the options processing below. +# Also, this object must be fully populated before any actual renaming +# can take places since many of the renaming tokens derive information +# about the file. + +targs = RenameTargets(args) + # Now process the options for opt, val in opts: if opt == "-1": - ProgramOptions["GLOBAL"] = False + ProgramOptions[GLOBAL] = False + if opt == "-a": - ProgramOptions["TARGET"] = ALL + ProgramOptions[TARGET] = lALL + if opt == "-b": - ProgramOptions["TARGET"] = NAM + ProgramOptions[TARGET] = NAM + if opt == "-C": - ProgramOptions["CASESENSITIVE"] = True + ProgramOptions[CASESENSITIVE] = True + if opt == "-c": - ProgramOptions["CASESENSITIVE"] = False + ProgramOptions[CASESENSITIVE] = False + if opt == "-d": DumpState() + if opt == "-E": - ProgramOptions["ERRORCONTINUE"] = True + ProgramOptions[ERRORCONTINUE] = True + if opt == "-e": - ProgramOptions["TARGET"] = EXT + ProgramOptions[TARGET] = EXT + if opt == "-f": - ProgramOptions["FORCERENAM"] = True + ProgramOptions[FORCERENAM] = True + if opt == "-g": - ProgramOptions["GLOBAL"] = True + ProgramOptions[GLOBAL] = True + if opt == "-h": Usage() sys.exit(0) + if opt == "-l": EXTDELIM = val + if opt == "-q": - ProgramOptions["QUIET"] = True + ProgramOptions[QUIET] = True + if opt == '-R': RENSEP = val + if opt == "-r": - old, new = GetOldNew(val) - RenRequests.append([old,new, copy.deepcopy(ProgramOptions)]) + req = {} + req[OLD], req[NEW] = GetOldNew(val) + for opt in ProgramOptions: + req[opt] = ProgramOptions[opt] + targs.RenRequests.append(req) + if opt == "-t": - ProgramOptions["TESTMODE"] = True + ProgramOptions[TESTMODE] = True + if opt == "-v": PrintStdout(RCSID) sys.exit(0) + if opt == "-w": try: l = int(val) @@ -724,13 +773,15 @@ ErrorMsg(eLINELEN) sys.exit(1) MAXLINELEN = l + if opt == "-X": - ProgramOptions["REGEX"] = False + ProgramOptions[REGEX] = False + if opt == "-x": - ProgramOptions["REGEX"] = True + ProgramOptions[REGEX] = True -if ProgramOptions["DEBUG"]: +if ProgramOptions[DEBUG]: # Dump what we know about the command line @@ -738,23 +789,13 @@ DumpList(DebugMsg, dPROGENV, "", envopt) DumpList(DebugMsg, dRESOLVEDOPTS, "", OPTIONS) -# Create and populate an object with rename targets. We have to -# do *before* we process any renaming requests because they may make -# reference to renaming tokens that only can be resolved with the -# contents of the 'targs' data structure. +# Display the list of renaming requests if we're debugging -targs = None -if args: - targs = RenameTargets(args) - -# Display outstanding renaming requests if we're debugging - -if ProgramOptions["DEBUG"]: - for i in RenRequests: +if ProgramOptions[DEBUG]: + for i in targs.RenRequests: DumpList(DebugMsg, dRENREQ , "", i) # Release the target container if we created one -if targs: - del targs +del targs