Finished implementing case conversion renaming request support.
1 parent 4ab4291 commit 3703851d623c1a110ea32e193f6ffad75af00661
@tundra tundra authored on 25 Aug 2010
Showing 1 changed file
View
474
tren.py
PROGNAME = "tren.py"
BASENAME = PROGNAME.split(".py")[0]
PROGENV = BASENAME.upper()
INCLENV = PROGENV + "INCL"
RCSID = "$Id: tren.py,v 1.223 2010/08/20 01:18:23 tundra Exp $"
RCSID = "$Id: tren.py,v 1.224 2010/08/25 18:04:39 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
name = oldname
 
for renrequest in self.RenRequests:
 
# Resolve any embedded renaming tokens
 
old = self.__ResolveRenameTokens(target, renrequest[OLD])
new = self.__ResolveRenameTokens(target, renrequest[NEW])
 
oldstrings = []
 
# Build a list of indexes to every occurence of the old string,
# taking case sensitivity into account
 
# Handle the case when old = "". This means to
# *replace the entire* old name with new. More
# specifically, replace the entire old name *as
# modified so far by preceding rename commands*.
# Handle conventional string replacement renaming requests
 
if renrequest[OLD] or renrequest[NEW]:
 
# Resolve any embedded renaming tokens
 
old = self.__ResolveRenameTokens(target, renrequest[OLD])
new = self.__ResolveRenameTokens(target, renrequest[NEW])
 
oldstrings = []
 
# Build a list of indexes to every occurence of the old string,
# taking case sensitivity into account
 
# Handle the case when old = "". This means to
# *replace the entire* old name with new. More
# specifically, replace the entire old name *as
# modified so far by preceding rename commands*.
 
if not old:
old = name
 
# 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
# string match.
#
# Either way, each 'hit' is recorded as a tuple:
#
# (index to beginning of hit, beginning of next non-hit text)
#
# This is so subsequent replacement logic knows:
#
# 1) Where the replacement begins
# 2) Where the replacement ends
#
# These two values are used during actual string
# replacement to properly replace the 'new' string
# into the requested locations.
 
 
# Handle regular expression pattern matching
 
if renrequest[REGEX]:
 
try:
# 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.span())
 
except:
ErrorMsg(eBADREGEX % old)
 
# 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:
 
nextloc = i + oldlen
oldstrings.append((i, nextloc))
i = name.find(old, nextloc)
 
# If we found any matching strings, replace them
 
if oldstrings:
 
# But only process the instances the user asked for
 
todo = []
 
# Handle single instance requests doing bounds checking as we go
 
start = renrequest[INSTANCESTART]
end = renrequest[INSTANCEEND]
 
if (end == SINGLEINST):
 
# Compute bounds for positive and negative indicies.
# This is necessary because positive indicies are 0-based,
# but negative indicies start at -1.
 
bound = len(oldstrings)
 
if start < 0:
bound += 1
 
# Now go get that entry
 
if abs(start) < bound:
todo.append(oldstrings[start])
 
# Handle instance range requests
 
else:
todo = oldstrings[start:end]
 
 
# Replace selected substring(s). Substitute from
# R->L in original string so as not to mess up the
# replacement indicies.
 
todo.reverse()
for i in todo:
newname = newname[:i[0]] + new + newname[i[1]:]
 
 
# Handle case conversion renaming requests
if not old:
old = name
 
# 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
# string match.
#
# Either way, each 'hit' is recorded as a tuple:
#
# (index to beginning of hit, beginning of next non-hit text)
#
# This is so subsequent replacement logic knows:
#
# 1) Where the replacement begins
# 2) Where the replacement ends
#
# These two values are used during actual string
# replacement to properly replace the 'new' string
# into the requested locations.
 
 
# Handle regular expression pattern matching
 
if renrequest[REGEX]:
 
try:
# 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.span())
 
except:
ErrorMsg(eBADREGEX % old)
 
# 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:
 
nextloc = i + oldlen
oldstrings.append((i, nextloc))
i = name.find(old, nextloc)
 
# If we found any matching strings, replace them
 
if oldstrings:
 
# But only process the instances the user asked for
 
todo = []
 
# Handle single instance requests doing bounds checking as we go
start = renrequest[INSTANCESTART]
end = renrequest[INSTANCEEND]
 
if (end == SINGLEINST):
# Compute bounds for positive and negative indicies.
# This is necessary because positive indicies are 0-based,
# but negative indicies start at -1.
 
bound = len(oldstrings)
 
if start < 0:
bound += 1
 
# Now go get that entry
 
if abs(start) < bound:
todo.append(oldstrings[start])
 
# Handle instance range requests
 
else:
todo = oldstrings[start:end]
 
 
# Replace selected substring(s). Substitute from
# R->L in original string so as not to mess up the
# replacement indicies.
 
todo.reverse()
for i in todo:
newname = newname[:i[0]] + new + newname[i[1]:]
elif renrequest[CASECONV]:
newname = CASETBL[renrequest[CASECONV]](name)
 
# Any subsequent replacements operate on the modified name
 
name = newname
 
# Nothing to do, if old- and new names are the same
 
if opt == "-e":
 
# Make sure we support the requested case conversion
if val in CASEOPS:
 
ProgramOptions[CASECONV] = val
 
# Construct a renaming request
req = {}
req[OLD], req[NEW] = None, None
for opt in ProgramOptions:
req[opt] = ProgramOptions[opt]
 
targs.RenRequests.append(req)
 
# Error out if we don't recognize it
else:
ErrorMsg(eBADCASECONV % (val, ", ".join(CASEOPS)))
 
if opt == "-r":
req = {}
req[OLD], req[NEW] = GetOldNew(val)
ProgramOptions[CASECONV] = None
for opt in ProgramOptions:
req[opt] = ProgramOptions[opt]
targs.RenRequests.append(req)