Built basic scaffolding for renaming files.
Changed -1 option to -G.
Implemented support for -G, -g.
1 parent 1214d77 commit a01840c8156525002c56b206f79db287c2a5dea5
@tundra tundra authored on 3 Feb 2010
Showing 1 changed file
View
84
tren.py
 
PROGNAME = "tren.py"
BASENAME = PROGNAME.split(".py")[0]
PROGENV = BASENAME.upper()
RCSID = "$Id: tren.py,v 1.138 2010/02/03 01:04:08 tundra Exp $"
RCSID = "$Id: tren.py,v 1.139 2010/02/03 23:43:20 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
eBADLEN = "Bad line length '%s'!"
eERROR = "ERROR"
eFILEOPEN = "Cannot open file '%s': %s!"
eLINELEN = "Specified line length too short! Must be at least %s" % MINLEN
eNOTHINGTODO = "Nothing to do!"
eTOOMANYINC = "Too many includes! (Max is %d) Possible circular reference?" % MAXINCLUDES
 
 
#####
#####
 
uTable = [PROGVER,
HOMEPAGE,
"usage: " + PROGNAME + " [-1abCcdEefghqtvwXx] [-I file] [-l string] [-r old=new]... file|dir file|dir ...",
"usage: " + PROGNAME + " [-abCcdEefGghqtvwXx] [-I file] [-l string] [-r old=new]... file|dir file|dir ...",
" where,",
" -1 Rename only the first instance of the specified string (Default)",
" -a Rename within the entire file or directory name (Default)",
" -C Do case-sensitive renaming (Default)",
" -c Collapse case when doing string substitution.",
" -d Dump debugging information",
" -e Only perform renaming within extension portion of or directory name.",
" -E Continue renaming even after an error is encountered",
" -f Force renaming even if target file or directory name already exists.",
" -G Rename only the first instance of the specified string (Default)",
" -g Replace all instances (global rename) of the old string with the new.",
" -h Print help information.",
" -I file Include command line arguments from file",
" -l string File extension delimiter string. (Default: .)",
[ST_ATIME, {}, ORDERBYATIME],
[ST_CTIME, {}, ORDERBYCTIME],
[ST_MTIME, {}, ORDERBYMTIME],
[ST_SIZE, {}, ORDERBYSIZE],
]
]
 
# Populate the data structures with each targets' stat information
 
for fullname in targs:
DebugMsg(SEPARATOR + "\n\n")
 
# End of 'DumpObj()'
 
 
#####
# Go Do The Requested Renaming
#####
 
def Rename(self):
 
 
# Make sure we actually have work to do
 
if not self.SortViews[ORDERBYCMDLINE] or not self.RenRequests:
 
ErrorMsg(eNOTHINGTODO)
return
 
# Iterate over all the target filenames in command line order,
# applying each renaming in requested order
 
 
for target in self.SortViews[ORDERBYCMDLINE]:
 
newname = target
for renrequest in self.RenRequests:
 
old, new = self.ResolveRenameStrings(renrequest[OLD], renrequest[NEW])
 
# Handle global vs. 1st occurence replacement
 
if renrequest[GLOBAL]:
newname = newname.replace(old, new)
 
else:
i = newname.find(old)
if i >= 0:
newname = newname[:i] + new + newname[i + len(old):]
 
print ColumnPad([target, newname], padwidth = 50)
 
# End of 'Rename()'
 
 
#####
# Resolve Rename Strings
#####
 
""" This takes "old" and "new" renaming strings as input and resolves
all outstanding renaming token references so that they can
then be applied to the rename.
"""
 
def ResolveRenameStrings(self, old, new):
 
return [old, new]
 
# End of 'ReolveRenameStrings()'
 
 
# End of class 'RenameTargets'
 
#----------------------------------------------------------#
if DEBUGFLAG in OPTIONS:
ProgramOptions[DEBUG] = True
 
try:
opts, args = getopt.getopt(OPTIONS, '1abbCcdEefghl:qR:r:tvw:Xx]')
opts, args = getopt.getopt(OPTIONS, 'abbCcdEefGghl:qR:r:tvw:Xx]')
except getopt.GetoptError as e:
ErrorMsg(eBADARG % e.args[0])
sys.exit(1)
 
# Now process the options
 
for opt, val in opts:
 
if opt == "-1":
ProgramOptions[GLOBAL] = False
 
if opt == "-a":
ProgramOptions[TARGET] = ALL
 
if opt == "-b":
ProgramOptions[TARGET] = EXT
 
if opt == "-f":
ProgramOptions[FORCERENAM] = True
 
if opt == "-G":
ProgramOptions[GLOBAL] = False
 
if opt == "-g":
ProgramOptions[GLOBAL] = True
 
# Dump what we know about the container
 
targs.DumpObj()
 
for target in targs.SortViews[ORDERBYCMDLINE]:
print target
 
# Perform reqested renamings
 
targs.Rename()
 
 
# Release the target container if we created one
 
del targs