diff --git a/tren.py b/tren.py index b9c9f33..752f59f 100755 --- a/tren.py +++ b/tren.py @@ -9,7 +9,7 @@ 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 @@ -928,128 +928,137 @@ for renrequest in self.RenRequests: - # Resolve any embedded renaming tokens + # Handle conventional string replacement renaming requests - old = self.__ResolveRenameTokens(target, renrequest[OLD]) - new = self.__ResolveRenameTokens(target, renrequest[NEW]) + if renrequest[OLD] or renrequest[NEW]: - oldstrings = [] + # Resolve any embedded renaming tokens - # Build a list of indexes to every occurence of the old string, - # taking case sensitivity into account + old = self.__ResolveRenameTokens(target, renrequest[OLD]) + new = self.__ResolveRenameTokens(target, renrequest[NEW]) - # 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 + oldstrings = [] - # 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. + # 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 + # Handle regular expression pattern matching - if renrequest[REGEX]: + if renrequest[REGEX]: - try: - # Do the match either case-insentitive or not - - if renrequest[CASESENSITIVE]: - rematches = re.finditer(old, name) + try: + # Do the match either case-insentitive or not - else: - rematches = re.finditer(old, name, re.I) + if renrequest[CASESENSITIVE]: + rematches = re.finditer(old, name) - # And save off the results + else: + rematches = re.finditer(old, name, re.I) - for match in rematches: - oldstrings.append(match.span()) + # And save off the results - except: - ErrorMsg(eBADREGEX % old) + for match in rematches: + oldstrings.append(match.span()) - # Handle literal string replacement + except: + ErrorMsg(eBADREGEX % old) - 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 + # Handle literal string replacement else: - todo = oldstrings[start:end] + + # 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. + # 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]:] + todo.reverse() + for i in todo: + newname = newname[:i[0]] + new + newname[i[1]:] + + + # Handle case conversion renaming requests + + 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 @@ -2085,8 +2094,18 @@ # 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))) @@ -2170,6 +2189,7 @@ 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)