Fixed a bug that occured when multiple renames iterated over the same filename.
1 parent 541dd4d commit 23fb393d61e5cb66da7b6227d64fe83327ed7b00
@tundra tundra authored on 26 Feb 2010
Showing 1 changed file
View
41
tren.py
 
PROGNAME = "tren.py"
BASENAME = PROGNAME.split(".py")[0]
PROGENV = BASENAME.upper()
RCSID = "$Id: tren.py,v 1.161 2010/02/26 18:15:15 tundra Exp $"
RCSID = "$Id: tren.py,v 1.162 2010/02/26 18:56:41 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
for target in self.SortViews[ORDERBYCMDLINE]:
 
oldname, pathname = self.RenNames[target][BASE], self.RenNames[target][PATHNAME]
newname = oldname
name = oldname
 
for renrequest in self.RenRequests:
 
old, new = self.ResolveRenameTokens(renrequest[OLD], renrequest[NEW])
# This means to *replace the entire* old name with new.
if not old:
old = oldname
 
# Collapse case if requested
 
name = oldname
if not renrequest[CASESENSITIVE]:
 
name = name.lower()
old = old.lower()
 
# Find every instance of the 'old' string in the
# current filename. 'Find' in this case can be either
# a regular expression pattern match or a literal
 
if renrequest[REGEX]:
 
try:
rematches = re.finditer(old, name)
# Do the match either case-insentitive or not
if renrequest[CASESENSITIVE]:
rematches = re.finditer(old, name)
else:
rematches = re.finditer(old, name, re.I)
 
# And save off the results
 
for match in rematches:
oldstrings.append((match.start(), match.end()))
 
except:
 
# Handle literal string replacement
 
else:
 
# Collapse case if requested
 
if not renrequest[CASESENSITIVE]:
 
name = name.lower()
old = old.lower()
 
oldlen = len(old)
i = name.find(old)
while i >= 0:
 
# If we found any matching strings, replace them
 
if oldstrings:
 
# But only process the instances the user asked for
 
todo = []
 
todo.reverse()
for i in todo:
newname = newname[:i[0]] + new + newname[i[1]:]
 
# Any subsequent replacements operate on the modified name
 
name = newname
 
# Nothing to do, if old- and new names are the same
 
if newname != oldname: