diff --git a/tren.py b/tren.py index 9face03..6e7ab6f 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.132 2010/02/01 23:08:22 tundra Exp $" +RCSID = "$Id: tren.py,v 1.133 2010/02/02 17:41:21 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -37,6 +37,7 @@ # Imports # #----------------------------------------------------------# +import copy import getopt import os from stat import * @@ -82,7 +83,6 @@ ##### ALL = "All" # Rename target is whole filename -ARROW = "--->" # Text pointer COMMENT = "#" # Comment character in include files ESC = "\\" # Escape character EXT = "Ext" # Rename target is extension @@ -108,7 +108,7 @@ dDUMPOBJ = "Dumping Object %s" dINCLUDING = "Including file '%s'" dPROGENV = "$" + PROGENV -dRENREQ = "Renaming Requests:" +dRENREQ = "Renaming Request:" dRESOLVEDOPTS = "Resolved Command Line" dSEPCHAR = "-" # Used for debug separator lines dSEQATIME = "Access Time Sequence:" @@ -180,18 +180,18 @@ # Program toggle and option defaults -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 +ProgramOptions = { - -# 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 + } #--------------------------- Code Begins Here ---------------------------------# @@ -347,13 +347,24 @@ self.RenNames[name].append(tblz - t.index(name) - 1) # Descending Index - if DEBUG: + if ProgramOptions["DEBUG"]: l=[] for item in vieworder: DumpList(DebugMsg, debugmsg, item, view[item]) - if DEBUG: - DumpRenameObj(self) + if ProgramOptions["DEBUG"]: + + SEPARATOR = dSEPCHAR * MAXLINELEN + DebugMsg(SEPARATOR) + DebugMsg(dDUMPOBJ % str(self)) + DebugMsg(SEPARATOR) + + # Dump abspath, basename, & stat information + + for name in self.RenNames: + DumpList(DebugMsg, name, "", self.RenNames[name]) + + DebugMsg(SEPARATOR) # Now get rid of the working dictionaries to free up their memory @@ -426,36 +437,13 @@ def DumpList(handler, msg, listname, content): handler(msg) - itemarrow = ColumnPad([listname, ARROW], padwidth=LSTPAD) + itemarrow = ColumnPad([listname, " "], padwidth=LSTPAD) handler(ColumnPad([" ", " %s %s" % (itemarrow, content)])) # End of 'DumpList()' ##### -# Dump The Contents Of A Rename Object -##### - -def DumpRenameObj(obj): - - SEPARATOR = dSEPCHAR * MAXLINELEN - DebugMsg(SEPARATOR) - DebugMsg(dDUMPOBJ % str(obj)) - DebugMsg(SEPARATOR) - - # Dump abspath, basename, & stat information - - for name in obj.RenNames: - DebugMsg(name) - for item in obj.RenNames[name]: - DebugMsg(ColumnPad([" ", item])) - - DebugMsg(SEPARATOR) - -# End of 'DumpRenameObj()' - - -##### # Dump The State Of The Program ##### @@ -466,25 +454,10 @@ DebugMsg(dCURSTATE) DebugMsg(SEPARATOR) - # Names of all the state variables we want dumped - state = [ - "PROGVER", - "DEBUG", - "CASESENSITIVE", - "ERRORCONTINUE", - "EXTDELIM", - "FORCERENAM", - "GLOBAL", - "MAXLINELEN", - "QUIET", - "RENSEP", - "REGEX", - "TARGET", - "TESTMODE", - ] - - for k in state: - DebugMsg(ColumnPad([k, eval(k)])) + opts = ProgramOptions.keys() + opts.sort() + for o in opts: + DebugMsg(ColumnPad([o, ProgramOptions[o]])) DebugMsg(SEPARATOR) @@ -547,24 +520,6 @@ ##### -# Parse Renaming Requests -##### - -def ParseRenReq(val, filename): - - """ This routine parses a old=new - renaming pair, resolves any - outstanding renaming tokens, - and returns them in the form - of: [old, new] - """ - - return val.split("=") - -# End of 'ParseRenReq()' - - -##### # Print To stderr ##### @@ -633,7 +588,7 @@ n += l.split() f.close() - if DEBUG: + if ProgramOptions["DEBUG"]: DebugMsg(dINCLUDING % file) OPTIONS = lhs + n + rhs @@ -698,7 +653,7 @@ # debug info about includes if DEBUGFLAG in OPTIONS: - DEBUG = True + ProgramOptions["DEBUG"] = True # Deal with include files @@ -708,7 +663,7 @@ # to get debug info on command line expansion if DEBUGFLAG in OPTIONS: - DEBUG = True + ProgramOptions["DEBUG"] = True RenRequests = [] @@ -723,38 +678,39 @@ for opt, val in opts: if opt == "-1": - GLOBAL = False + ProgramOptions["GLOBAL"] = False if opt == "-a": - TARGET = ALL + ProgramOptions["TARGET"] = ALL if opt == "-b": - TARGET = NAM + ProgramOptions["TARGET"] = NAM if opt == "-C": - CASESENSITIVE = True + ProgramOptions["CASESENSITIVE"] = True if opt == "-c": - CASESENSITIVE = False + ProgramOptions["CASESENSITIVE"] = False if opt == "-d": DumpState() if opt == "-E": - ERRORCONTINUE = True + ProgramOptions["ERRORCONTINUE"] = True if opt == "-e": - TARGET = EXT + ProgramOptions["TARGET"] = EXT if opt == "-f": - FORCERENAM = True + ProgramOptions["FORCERENAM"] = True if opt == "-g": - GLOBAL = True + ProgramOptions["GLOBAL"] = True if opt == "-h": Usage() sys.exit(0) if opt == "-l": EXTDELIM = val if opt == "-q": - QUIET = True + ProgramOptions["QUIET"] = True if opt == '-R': RENSEP = val if opt == "-r": - RenRequests.append(GetOldNew(val)) + old, new = GetOldNew(val) + RenRequests.append([old,new, copy.deepcopy(ProgramOptions)]) if opt == "-t": - TESTMODE = True + ProgramOptions["TESTMODE"] = True if opt == "-v": PrintStdout(RCSID) sys.exit(0) @@ -769,12 +725,12 @@ sys.exit(1) MAXLINELEN = l if opt == "-X": - REGEX = False + ProgramOptions["REGEX"] = False if opt == "-x": - REGEX = True + ProgramOptions["REGEX"] = True -if DEBUG: +if ProgramOptions["DEBUG"]: # Dump what we know about the command line @@ -793,8 +749,9 @@ # Display outstanding renaming requests if we're debugging -if DEBUG: - DumpList(DebugMsg, dRENREQ , "", RenRequests) +if ProgramOptions["DEBUG"]: + for i in RenRequests: + DumpList(DebugMsg, dRENREQ , "", i) # Release the target container if we created one