Implemented GetOldNew to properly parse a -r old=new arg, honoring RENSEP.
1 parent d31d87a commit b33854982e61a430c1be708fdbf924f258eec6ee
@tundra tundra authored on 1 Feb 2010
Showing 1 changed file
View
50
tren.py
 
PROGNAME = "tren.py"
BASENAME = PROGNAME.split(".py")[0]
PROGENV = BASENAME.upper()
RCSID = "$Id: tren.py,v 1.131 2010/01/30 22:10:13 tundra Exp $"
RCSID = "$Id: tren.py,v 1.132 2010/02/01 23:08:22 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
#####
 
eBADARG = "Invalid command line: %s!"
eBADINCL = "option %s requires argument" % INCL
eBOGUSLEN = "Bogus line length '%s'!"
eBADNEWOLD = "Bad -r argument '%s'! Requires exactly one new, old string separator (Default: " + RENSEP + ")"
eBADLEN = "Bad line length '%s'!"
eERROR = "ERROR"
eFILEOPEN = "Cannot open file '%s': %s!"
eLINELEN = "Specified line length too short! Must be at least %s" % MINLEN
eTOOMANYINC = "Too many includes! (Max is %d) Possible circular reference?" % MAXINCLUDES
for emsg in l:
PrintStderr(PROGNAME + " " + eERROR + ": " + emsg)
 
# End of 'ErrorMsg()'
 
#####
# Split -r Argument Into Separate Old And New Strings
#####
 
def GetOldNew(arg):
 
 
escaping = False
numseps = 0
sepindex = 0
 
i = 0
while i < len(arg):
 
# Scan string ignoring escaped separators
 
if arg[i:].startswith(RENSEP):
 
if (i > 0 and (arg[i-1] != ESC)) or i == 0:
sepindex = i
numseps += 1
i += len(RENSEP)
 
else:
i += 1
 
 
if numseps != 1:
ErrorMsg(eBADNEWOLD % arg)
sys.exit(1)
 
else:
old, new = arg[:sepindex], arg[sepindex + len(RENSEP):]
old = old.replace(ESC + RENSEP, RENSEP)
new = new.replace(ESC + RENSEP, RENSEP)
return [old, new]
 
# End of 'GetOldNew()'
 
 
#####
# Parse Renaming Requests
QUIET = True
if opt == '-R':
RENSEP = val
if opt == "-r":
RenRequests.append(val.split(RENSEP))
RenRequests.append(GetOldNew(val))
if opt == "-t":
TESTMODE = True
if opt == "-v":
PrintStdout(RCSID)
if opt == "-w":
try:
l = int(val)
except:
ErrorMsg(eBOGUSLEN % val)
ErrorMsg(eBADLEN % val)
sys.exit(1)
if l < MINLEN:
ErrorMsg(eLINELEN)
sys.exit(1)