Added code to sort entries alphabetically if more than one has same ctime or size.
Added code to store ascending and descending sequence numbers by ctime and size for each rename target.
1 parent a74c194 commit 4dab3b0518ef75556bab945f14d3133cf9cfd25a
@tundra tundra authored on 28 Jan 2010
Showing 1 changed file
View
94
tren.py
# Program Information
 
PROGNAME = "tren.py"
PROGENV = PROGNAME.split(".py")[0].upper()
RCSID = "$Id: tren.py,v 1.115 2010/01/27 23:32:30 tundra Exp $"
RCSID = "$Id: tren.py,v 1.116 2010/01/28 22:55:52 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
#----------------------------------------------------------#
 
import getopt
import os
from stat import *
from stat import *
import sys
 
 
#----------------------------------------------------------#
#####
 
class RenameTargets:
 
"""
This class is used to keep track of all the files and/or
directories we're renaming. When __init__ finishes,
RenNames dictionary will be populated as follows:
 
fully-qualified name : [ basename,
stat information for the entry,
order of appearance on command line (0-based),
ascending order of appearance by-ctime (0-based),
descending order of appearance by-ctime (0-based),
ascending order of appearance by-size (0-based)
descending order of appearance by-size (0-based)
]
"""
 
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
 
self.Ctimes = {} # Keys = ctimes, Values = List of corresponding files
self.Sizes = {} # Keys = sizes, Values = List of corresponding files
self.ByCtime = [] # Sorted list of all file ctimes
self.BySize = [] # Sorted list od all file sizes
 
# Populate the data structures
 
cmdorder = 0
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)
 
time = stat[ST_CTIME]
if time in self.ctimes:
self.ctimes[time].append(fullname)
if time in self.Ctimes:
self.Ctimes[time].append(fullname)
else:
self.ctimes[time] = [fullname]
self.Ctimes[time] = [fullname]
size = stat[ST_SIZE]
if size in self.sizes:
self.sizes[size].append(fullname)
if size in self.Sizes:
self.Sizes[size].append(fullname)
else:
self.sizes[size] = [fullname]
self.Sizes[size] = [fullname]
 
self.RenNames[fullname] = [basename, stat, cmdorder]
cmdorder += 1
 
 
# Sort the relevant sequence renaming token lists
 
self.ByCtime = self.ctimes.keys()
self.ByCtime = self.Ctimes.keys()
self.ByCtime.sort()
self.BySize = self.sizes.keys()
self.BySize = self.Sizes.keys()
self.BySize.sort()
 
 
# Sort alphabetically when multiple targets map to the same key
 
for s in (self.Ctimes, self.Sizes):
 
for i in s:
s[i].sort()
 
# Now store order, ascending- and descending in master dictionary
 
for order, table in ((self.ByCtime, self.Ctimes), (self.BySize, self.Sizes)):
 
t = []
for i in order:
for j in table[i]:
t.append(j)
 
tblz = len(t)
for name in t:
self.RenNames[name].append(t.index(name)) # Ascending index
self.RenNames[name].append(tblz - t.index(name) - 1) # Descending Index
if DEBUG:
DumpRenameObj(self)
 
# End of class 'RenameTargets()'
# Now get rid of temporary working data structures
del self.Ctimes, self.ByCtime, self.Sizes, self.BySize
 
# End of class 'RenameTargets'
 
#----------------------------------------------------------#
# Supporting Function Definitions #
DebugMsg(name)
for item in obj.RenNames[name]:
DebugMsg(ColumnPad([" ", item]))
 
# Dump command line sequence
 
DebugMsg(dSEQCMD)
for item in obj.cmdorder:
DebugMsg(ColumnPad([" ", item]))
 
# Dump creation date sequence
 
DebugMsg(dSEQCTIME)
for item in obj.ByCtime:
itemarrow = ColumnPad([item, ARROW], padwidth=SEQPAD)
DebugMsg(ColumnPad([" ", " %s %s" % (itemarrow, obj.ctimes[item])]))
DebugMsg(ColumnPad([" ", " %s %s" % (itemarrow, obj.Ctimes[item])]))
 
# Dump size sequence
 
DebugMsg(dSEQSIZE)
for item in obj.BySize:
itemarrow = ColumnPad([item, ARROW], padwidth=SEQPAD)
DebugMsg(ColumnPad([" ", " %s %s" % (itemarrow, obj.sizes[item])]))
DebugMsg(ColumnPad([" ", " %s %s" % (itemarrow, obj.Sizes[item])]))
 
DebugMsg(SEPARATOR + "\n\n")
 
# End of 'DumpRenameObj()'
 
targs = None
if args:
targs = RenameTargets(args)
 
 
# Now process the options
 
for opt, val in opts: