diff --git a/tren.py b/tren.py index 5403388..72ca857 100755 --- a/tren.py +++ b/tren.py @@ -7,7 +7,7 @@ PROGNAME = "tren.py" PROGENV = PROGNAME.split(".py")[0].upper() -RCSID = "$Id: tren.py,v 1.111 2010/01/26 01:23:14 tundra Exp $" +RCSID = "$Id: tren.py,v 1.112 2010/01/26 23:02:40 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -33,6 +33,7 @@ import getopt import os +from stat import * import sys @@ -81,6 +82,8 @@ dCMDLINE = "Command Line" dCURSTATE = "Current State Of Program Options" dDEBUG = "DEBUG" +dDUMPOBJ = "Dumping Object %s" +dELLIPSIS = "..." dINCLUDING = "Including file '%s'" dPAIR = "Option/Target Pair" dPROGENV = "$" + PROGENV @@ -159,6 +162,51 @@ # Object Base Class Definitions # #----------------------------------------------------------# + +##### +# Container For Holding Rename Targets +##### + +class RenameTargets: + + def __init__(self, targs): + + # Dictionary of all rename targets and their stat info + + self.RenNames = {} + + # Ordered lists used by sequence renaming tokens + + self.cmdorder = [] # Order targets appear on command line + self.ctimes = {} # List of creation times + self.sizes = {} # list of sizes + + + # Populate the data structures + + for t in targs: + + fullname = os.path.abspath(t) + basename = os.path.basename(t) + stat = os.stat(fullname) + self.RenNames[fullname] = [basename, stat] + + self.cmdorder.append(t) + self.ctimes[stat[ST_CTIME]] = fullname + self.sizes[stat[ST_SIZE]] = fullname + + + # Sort the relevant sequence renaming token lists + + self.ByCtime = self.ctimes.keys() + self.ByCtime.sort() + self.BySize = self.sizes.keys() + self.BySize.sort() + + if DEBUG: + DumpRenameObj(self) + +# End of class 'RenameTargets()' #----------------------------------------------------------# @@ -177,7 +225,7 @@ l = str(l) retval += l + ((padwidth - len(l)) * padchar) - return retval.strip() + return retval.strip() # End of 'ColumnPad()' @@ -193,11 +241,34 @@ ##### +# Dump The Contents Of A Rename Object +##### + +def DumpRenameObj(obj): + + DebugMsg("\n") + DebugMsg(SEPARATOR) + DebugMsg(dDUMPOBJ % str(obj)) + DebugMsg(SEPARATOR) + + for name in obj.RenNames: + DebugMsg(name) + for item in obj.RenNames[name]: + DebugMsg(ColumnPad([dELLIPSIS, item])) + + DebugMsg(SEPARATOR + "\n\n") + +# End of 'DumpRenameObj()' + + +##### # Dump The State Of The Program ##### def DumpState(): + DebugMsg("\n") + DebugMsg(SEPARATOR) DebugMsg(dCURSTATE) DebugMsg(SEPARATOR) @@ -218,7 +289,8 @@ for k in state: DebugMsg(ColumnPad([k, eval(k)])) - DebugMsg(SEPARATOR + "\n") + DebugMsg(SEPARATOR + "\n\n") + # End of 'DumpState()' @@ -440,6 +512,15 @@ ErrorMsg(eBADARG % e.args[0]) sys.exit(1) + # Create and populate an object with rename targets. + # We have to do this here so that subsequent + # renaming tokens references can be resolved. + + targs = RenameTargets(args) + + + # Now process the options + for opt, val in opts: if opt == "-1": @@ -481,3 +562,6 @@ if opt == "-x": REGEX = True + + # Release the target container + del targs