Implemented /`cmd`/ command execution renaming tokens.
1 parent 6148884 commit 56b4571b7c178fe608bce0a90848450b524943a8
@tundra tundra authored on 5 Mar 2010
Showing 1 changed file
View
43
tren.py
 
PROGNAME = "tren.py"
BASENAME = PROGNAME.split(".py")[0]
PROGENV = BASENAME.upper()
RCSID = "$Id: tren.py,v 1.166 2010/03/05 21:54:36 tundra Exp $"
RCSID = "$Id: tren.py,v 1.167 2010/03/06 01:18:02 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
#####
# Replacement Token Literals
#####
 
TOKENDELIM = "/" # Symbol used to delimit renaming tokens
TOKENENV = "$" # Environment variable token
TOKENCMDEXEC = "`" # Delimiter for command execution renaming tokens
TOKENDELIM = "/" # Delimiter for all renaming tokens
TOKENENV = "$" # Introducer for environment variable replacement tokens
 
 
# Internal program state literals
 
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"
eEXECFAIL = "Command: '%s' Failed To Execute!"
eFILEOPEN = "Cannot open file '%s': %s!"
eLINELEN = "Specified line length too short! Must be at least %s" % MINLEN
eNAMELONG = "Renaming '%s' to new name '%s' too long! (Maximum length is %s.)"
eNAMESHORT = "Renaming '%s' to new name '%s' too short! (Minimum length is %s.)"
rentokens.reverse()
 
for r in rentokens:
 
# Environment variable replacement token
# Environment Variable replacement token
if r[2].startswith(TOKENENV):
r[2] = os.getenv(r[2][1:])
# Command Run replacement token
 
elif r[2].startswith(TOKENCMDEXEC) and r[2].endswith(TOKENCMDEXEC):
 
command = r[2][1:-1]
 
# Handle Windows variants which act differently
 
if os.name != 'posix':
pipe = os.popen(command, 'r')
 
# Handle Unix variants
 
else:
pipe = os.popen('{ ' + command + '; } 2>&1', 'r')
 
output = pipe.read()
status = pipe.close()
 
if status == None:
status = 0
 
# Nonzero status means error attempting to execute the command
if status:
ErrorMsg(eEXECFAIL % command)
 
# Otherwise swap the command with its results, stripping newlines
else:
r[2] = output.replace("\n", "")
# Do the actual replacement
 
renstring = renstring[:r[0]] + r[2] + renstring[r[1]+1:]