diff --git a/tren.py b/tren.py index a218f2d..f085e38 100755 --- a/tren.py +++ b/tren.py @@ -8,7 +8,7 @@ PROGNAME = "tren.py" BASENAME = PROGNAME.split(".py")[0] PROGENV = BASENAME.upper() -RCSID = "$Id: tren.py,v 1.164 2010/02/27 00:31:57 tundra Exp $" +RCSID = "$Id: tren.py,v 1.165 2010/03/05 19:05:23 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -98,6 +98,7 @@ OPTINTRO = "-" # Option introducer RANGESEP = ":" # Separator for instance ranges SINGLEINST = "SINGLEINST" # Indicates a single, not range, replacement instance +TOKENDELIM = "/""" # Symbol used to delimit renaming tokens # Internal program state literals @@ -172,6 +173,7 @@ eBADINSTANCE = "%s is an invalid replacement instance! Must be integer values in the form: n, n:n, :n, n:, or :" eBADLEN = "Bad line length '%s'!" eBADNEWOLD = "Bad -r argument '%s'! Requires exactly one new, old string separator (Default: " + DEFSEP + ")" +eBADRENAMETOK = "Renaming token missing delimiter: %s" eBADREGEX = "Invalid Regular Expression: %s" eERROR = "ERROR" eFILEOPEN = "Cannot open file '%s': %s!" @@ -499,6 +501,7 @@ if renrequest[CASESENSITIVE]: rematches = re.finditer(old, name) + else: rematches = re.finditer(old, name, re.I) @@ -647,16 +650,51 @@ ##### - # Resolve Rename Strings + # Resolve Rename Tokens ##### - """ 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. + """ This replaces all renaming token references in 'renstring' + with the appropriate content and returns the resolved string. + 'target' is the name of the current file being renamed. We + need that as well because some renaming tokens refer to file + stat attributes or even the file name itself. """ def __ResolveRenameTokens(self, target, renstring): + # Find all token delimiters + + rentokens = [] + td = re.finditer(TOKENDELIM, renstring) + + # Build list of begin/end delimiter pairs + + odd = True + for i in td: + + if odd: + rentokens.append([i.start()]) + + else: + rentokens[-1].append(i.start()) + + odd = not odd + + # There must be an even number of token delimiters + # or the renaming token is malformed + + if rentokens and len(rentokens[-1]) != 2: + ErrorMsg(eBADRENAMETOK % renstring) + + # Now add the renaming token contents. This will be used to + # figure out what the replacement text should be. + + i = 0 + while i < len(rentokens): + + rentokens[i].append(renstring[rentokens[i][0]+1 : rentokens[i][1]]) + i += 1 + return renstring # End of '__ResolveRenameTokens()'