| |
---|
| | |
---|
| | 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 |
---|
| | |
---|
| |
---|
| | NULLSUFFIX = "Forced renaming suffix string" # Cannot be null |
---|
| | 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 |
---|
| | |
---|
| | DEBUG = "DEBUG" |
---|
| |
---|
| | eBADINCL = "option -%s requires argument" % INCL |
---|
| | 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!" |
---|
| | eLINELEN = "Specified line length too short! Must be at least %s" % MINLEN |
---|
| |
---|
| | # 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 |
---|
| |
---|
| | # End of '__RenameIt()' |
---|
| | |
---|
| | |
---|
| | ##### |
---|
| | # 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()' |
---|
| |
---|
| | |