Restructured RenRequests to be a single dictionary that now lives in the RenameTargets class.
The old and new strings now appear in the RenRequests dictionary with keys "OLD" and "NEW" respectively.
Defined symbolic constants for accessing state and old, new strings rather than embedding literal strings all over the code.
1 parent 9fe2c1a commit 62a35cbd1a19341e90805167d7a79373846784f9
@tundra tundra authored on 2 Feb 2010
Showing 1 changed file
View
175
tren.py
 
PROGNAME = "tren.py"
BASENAME = PROGNAME.split(".py")[0]
PROGENV = BASENAME.upper()
RCSID = "$Id: tren.py,v 1.135 2010/02/02 17:53:55 tundra Exp $"
RCSID = "$Id: tren.py,v 1.136 2010/02/02 18:31:42 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
EXTDELIM = "." # Extension delimeter
INCL = "-I" # Include file command line option
NAM = "Nam" # Rename target is name
RENSEP = "=" # Rename command separator: oldRENSEPnew
 
# Internal program state literals
 
DEBUG = "DEBUG"
CASESENSITIVE = "CASESENSITIVE"
ERRORCONTINUE = "ERRORCONTINUE"
FORCERENAM = "FORCERENAM"
GLOBAL = "GLOBAL"
QUIET = "QUIET"
REGEX = "REGEX"
TARGET = "TARGET"
TESTMODE = "TESTMODE"
 
# Rename string keys
 
NEW = "NEW"
OLD = "OLD"
 
 
 
#----------------------------------------------------------#
# Prompts, & Application Strings #
# Program toggle and option defaults
 
ProgramOptions = {
 
"DEBUG" : False, # Debugging off
"CASESENSITIVE" : True, # Search is case-sensitive
"ERRORCONTINUE" : False, # Do not continue after error
"FORCERENAM" : False, # Do not rename if target already exists
"GLOBAL" : False, # Only rename first instance of old string
"QUIET" : False, # Display progress
"REGEX" : False, # Do not treat old string as a regex
"TARGET" : ALL, # Can be "All", "Name", or "Ext"
"TESTMODE" : False # Global data structures
DEBUG : False, # Debugging off
CASESENSITIVE : True, # Search is case-sensitive
ERRORCONTINUE : False, # Do not continue after error
FORCERENAM : False, # Do not rename if target already exists
GLOBAL : False, # Only rename first instance of old string
QUIET : False, # Display progress
REGEX : False, # Do not treat old string as a regex
TARGET : ALL, # Can be "All", "Name", or "Ext"
TESTMODE : False # Global data structures
}
 
 
#--------------------------- Code Begins Here ---------------------------------#
#----------------------------------------------------------#
 
 
#####
# Container For Holding Rename Targets
# Container For Holding Rename Targets And Renaming Requests
#####
 
class RenameTargets:
 
# Dictionary of all rename targets and their stat info
 
self.RenNames = {}
 
# Dictionary of all the renaming requests
 
self.RenRequests = []
 
# Ordered lists used by sequence renaming tokens
 
args = {} # Keys = 0, Values = Rename targets from command line
modes = {} # Keys = modes, Values = List of corresponding files
self.RenNames[name].append(t.index(name)) # Ascending index
self.RenNames[name].append(tblz - t.index(name) - 1) # Descending Index
 
if ProgramOptions["DEBUG"]:
if ProgramOptions[DEBUG]:
l=[]
for item in vieworder:
DumpList(DebugMsg, debugmsg, item, view[item])
 
if ProgramOptions["DEBUG"]:
if ProgramOptions[DEBUG]:
 
SEPARATOR = dSEPCHAR * MAXLINELEN
DebugMsg(SEPARATOR)
DebugMsg(dDUMPOBJ % str(self))
l = l.split(COMMENT)[0]
n += l.split()
f.close()
if ProgramOptions["DEBUG"]:
if ProgramOptions[DEBUG]:
DebugMsg(dINCLUDING % file)
 
OPTIONS = lhs + n + rhs
# Check for debug manually to see if we want
# debug info about includes
 
if DEBUGFLAG in OPTIONS:
ProgramOptions["DEBUG"] = True
ProgramOptions[DEBUG] = True
 
# Deal with include files
 
OPTIONS = ProcessIncludes(OPTIONS)
# Check for debug manually again before we process the options
# to get debug info on command line expansion
 
if DEBUGFLAG in OPTIONS:
ProgramOptions["DEBUG"] = True
 
RenRequests = []
ProgramOptions[DEBUG] = True
 
try:
opts, args = getopt.getopt(OPTIONS, '1abbCcdEefghl:qR:r:tvw:Xx]')
except getopt.GetoptError as e:
ErrorMsg(eBADARG % e.args[0])
sys.exit(1)
 
# Create and populate an object with rename targets. We have to
# This must be done here because this object also stores the
# -r renaming requests we may find in the options processing below.
# Also, this object must be fully populated before any actual renaming
# can take places since many of the renaming tokens derive information
# about the file.
 
targs = RenameTargets(args)
 
# Now process the options
 
for opt, val in opts:
 
if opt == "-1":
ProgramOptions["GLOBAL"] = False
ProgramOptions[GLOBAL] = False
 
if opt == "-a":
ProgramOptions["TARGET"] = ALL
ProgramOptions[TARGET] = lALL
 
if opt == "-b":
ProgramOptions["TARGET"] = NAM
ProgramOptions[TARGET] = NAM
 
if opt == "-C":
ProgramOptions["CASESENSITIVE"] = True
ProgramOptions[CASESENSITIVE] = True
 
if opt == "-c":
ProgramOptions["CASESENSITIVE"] = False
ProgramOptions[CASESENSITIVE] = False
 
if opt == "-d":
DumpState()
 
if opt == "-E":
ProgramOptions["ERRORCONTINUE"] = True
ProgramOptions[ERRORCONTINUE] = True
 
if opt == "-e":
ProgramOptions["TARGET"] = EXT
ProgramOptions[TARGET] = EXT
 
if opt == "-f":
ProgramOptions["FORCERENAM"] = True
ProgramOptions[FORCERENAM] = True
 
if opt == "-g":
ProgramOptions["GLOBAL"] = True
ProgramOptions[GLOBAL] = True
 
if opt == "-h":
Usage()
sys.exit(0)
 
if opt == "-l":
EXTDELIM = val
 
if opt == "-q":
ProgramOptions["QUIET"] = True
ProgramOptions[QUIET] = True
 
if opt == '-R':
RENSEP = val
 
if opt == "-r":
old, new = GetOldNew(val)
RenRequests.append([old,new, copy.deepcopy(ProgramOptions)])
req = {}
req[OLD], req[NEW] = GetOldNew(val)
for opt in ProgramOptions:
req[opt] = ProgramOptions[opt]
targs.RenRequests.append(req)
 
if opt == "-t":
ProgramOptions["TESTMODE"] = True
ProgramOptions[TESTMODE] = True
 
if opt == "-v":
PrintStdout(RCSID)
sys.exit(0)
 
if opt == "-w":
try:
l = int(val)
except:
if l < MINLEN:
ErrorMsg(eLINELEN)
sys.exit(1)
MAXLINELEN = l
 
if opt == "-X":
ProgramOptions["REGEX"] = False
ProgramOptions[REGEX] = False
 
if opt == "-x":
ProgramOptions["REGEX"] = True
 
 
if ProgramOptions["DEBUG"]:
ProgramOptions[REGEX] = True
 
 
if ProgramOptions[DEBUG]:
 
# Dump what we know about the command line
 
DumpList(DebugMsg, dCMDLINE, "", sys.argv)
DumpList(DebugMsg, dPROGENV, "", envopt)
DumpList(DebugMsg, dRESOLVEDOPTS, "", OPTIONS)
 
# Create and populate an object with rename targets. We have to
# do *before* we process any renaming requests because they may make
# reference to renaming tokens that only can be resolved with the
# contents of the 'targs' data structure.
 
targs = None
if args:
targs = RenameTargets(args)
 
# Display outstanding renaming requests if we're debugging
 
if ProgramOptions["DEBUG"]:
for i in RenRequests:
# Display the list of renaming requests if we're debugging
 
if ProgramOptions[DEBUG]:
for i in targs.RenRequests:
DumpList(DebugMsg, dRENREQ , "", i)
 
# Release the target container if we created one
 
if targs:
del targs
 
del targs