Added support for AllowNewNamespaces option.
Added debug output whenever a namespace is changed.
1 parent 55eb5dd commit c4c841697889f0fd7091cb21f091d5b2f3747219
@tundra tundra authored on 7 Apr 2004
Showing 1 changed file
View
146
tconfpy.py
 
# Program Information
 
PROGNAME = "tconfpy"
RCSID = "$Id: tconfpy.py,v 1.162 2004/04/07 00:10:37 tundra Exp $"
RCSID = "$Id: tconfpy.py,v 1.163 2004/04/07 08:54:08 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
###########
 
# Formatting Constants
 
ATEOF = "EOF" # Use as line number when at EOF
FILENUM = "[File: %s Line: %s] " # Display filename and linenum
MSGPROMPT = "%s>"
FILENUM = "[File: %s Line: %s] " # Display filename and linenum
ATEOF = "EOF" # Use as line number when at EOF
PTR = " ---> " # Textual pointer for debug output
 
STARTUP = "STARTUP" # Indicates message before any lines processed
 
# Special And Reserved Symbols
 
HASH = r'#'
# Global Variables & Data Structures #
#----------------------------------------------------------#
 
 
ALLOWNEW = True # Allow new variable creation in cfg file
ALLOWNEWNS = True # Allow new namespace creation in cfg file
ALLOWNEWVAR = True # Allow new variable creation in cfg file
DEBUG = False # Control Debug output
LITERALVARS = False
INLITERAL = False # Indicates we are currently in a literal block
 
 
# Messages
 
Messages["dLINEIGNORE"] = FILENUM + " '%s' " + PTR + "%s\n"
Messages["dNAMESPACE"] = FILENUM + "Setting Current Namespace To: '%s'"
Messages["dNUMLINES"] = "Processing File '%s' Resulted In %d Total Lines Parsed"
Messages["dPARSEDLINE"] = FILENUM + " '%s'" + PTR + "'%s'\n"
 
 
eSTARTUP = "<Program Starting>"
 
# Messages
 
Messages["eBADCOND"] = FILENUM + "Bad '%s' Directive. %s"
Messages["eBADREGEX"] = FILENUM + "Bad Regular Expression, '%s', In Legal Values List For Variable '%s'"
Messages["eBADSYNTAX"] = FILENUM + "Syntax Error. Statement Not In Known Form"
Messages["eCONFOPEN"] = FILENUM + "Cannot Open The File '%s'"
Messages["eDESCRIPTBAD"] = "API Error: %s For Variable '%s'"
Messages["eELSENOIF"] = FILENUM + "'%s' Without Preceding '%s' Form" % (ELSE, IF)
Messages["eENDIFEXTRA"] = FILENUM + "'" + ENDIF + "' Without Matching Condition"
Messages["eENDIFMISS"] = FILENUM + "Missing %d" + " '%s' " % ENDIF + " Statement(s)"
Messages["eEQUIVEXTRA"] = FILENUM + "Only a single '%s' Or '%s' Operator Permitted" % (EQUIV, NOTEQUIV)
Messages["eIFEXTRATXT"] = FILENUM + "Extra Text On Line. '%s' Only Accepts Variable References As Arguments"
Messages["eNOTLEGALVAL"] = FILENUM + "'%s' Not Found In List Of Legal Values For Variable '%s'"
Messages["eSTRINGLONG"] = FILENUM + "Right-Hand-Side Too Long. '%s' Must Be No More Than %s Characters Long"
Messages["eSTRINGSHORT"] = FILENUM + "Right-Hand-Side Too Short. '%s' Must Be At Least %s Characters Long"
Messages["eTYPEBAD"] = FILENUM + "Type Mismatch. '%s' Must Be Assigned Values Of Type %s Only"
Messages["eVALLARGE"] = FILENUM + "%s Is Larger Than The Maximum Allowed, %s, For Variable '%s'"
Messages["eVALSMALL"] = FILENUM + "%s Is Smaller Than The Minimum Allowed, %s, For Variable '%s'"
Messages["eVARRESERVED"] = FILENUM + "Variable Name Cannot Begin With The '%s' Symbol"
Messages["eVARNAMESPC"] = FILENUM + "Variable Names May Not Contain Whitespace"
Messages["eVARNEW"] = FILENUM + "New Variable Creation Not Permitted"
Messages["eVARNONAME"] = FILENUM + "Variable Name Evaluates To Null String. Not Permitted"
Messages["eVARREADONLY"] = FILENUM + "Variable '%s' Is Read-Only. Cannot Change Its Value"
Messages["eVARUNDEF"] = FILENUM + "Attempt To Reference Undefined Variable '%s'"
Messages["eBADCOND"] = FILENUM + "Bad '%s' Directive. %s"
Messages["eBADREGEX"] = FILENUM + "Bad Regular Expression, '%s', In Legal Values List For Variable '%s'"
Messages["eBADSYNTAX"] = FILENUM + "Syntax Error. Statement Not In Known Form"
Messages["eCONFOPEN"] = FILENUM + "Cannot Open The File '%s'"
Messages["eDESCRIPTBAD"] = "API Error: %s For Variable '%s'"
Messages["eELSENOIF"] = FILENUM + "'%s' Without Preceding '%s' Form" % (ELSE, IF)
Messages["eENDIFEXTRA"] = FILENUM + "'" + ENDIF + "' Without Matching Condition"
Messages["eENDIFMISS"] = FILENUM + "Missing %d" + " '%s' " % ENDIF + " Statement(s)"
Messages["eEQUIVEXTRA"] = FILENUM + "Only a single '%s' Or '%s' Operator Permitted" % (EQUIV, NOTEQUIV)
Messages["eIFEXTRATXT"] = FILENUM + "Extra Text On Line. '%s' Only Accepts Variable References As Arguments"
Messages["eNOTLEGALVAL"] = FILENUM + "'%s' Not Found In List Of Legal Values For Variable '%s'"
Messages["eNAMESPACENEW"] = FILENUM + "New Namespace Creation Not Permitted. Current Namespace Unchanged: '%s'"
Messages["eSTRINGLONG"] = FILENUM + "Right-Hand-Side Too Long. '%s' Must Be No More Than %s Characters Long"
Messages["eSTRINGSHORT"] = FILENUM + "Right-Hand-Side Too Short. '%s' Must Be At Least %s Characters Long"
Messages["eTYPEBAD"] = FILENUM + "Type Mismatch. '%s' Must Be Assigned Values Of Type %s Only"
Messages["eVALLARGE"] = FILENUM + "%s Is Larger Than The Maximum Allowed, %s, For Variable '%s'"
Messages["eVALSMALL"] = FILENUM + "%s Is Smaller Than The Minimum Allowed, %s, For Variable '%s'"
Messages["eVARRESERVED"] = FILENUM + "Variable Name Cannot Begin With The '%s' Symbol"
Messages["eVARNAMESPC"] = FILENUM + "Variable Names May Not Contain Whitespace"
Messages["eVARNEW"] = FILENUM + "New Variable Creation Not Permitted"
Messages["eVARNONAME"] = FILENUM + "Variable Name Evaluates To Null String. Not Permitted"
Messages["eVARREADONLY"] = FILENUM + "Variable '%s' Is Read-Only. Cannot Change Its Value"
Messages["eVARUNDEF"] = FILENUM + "Attempt To Reference Undefined Variable '%s'"
 
 
###########
# Warning Literals And Messages
#----------------------------------------------------------#
# Public API To Module #
#----------------------------------------------------------#
 
def ParseConfig(cfgfile, InitialSymTbl={}, AllowNewVars=True, Debug=False, LiteralVars=False):
def ParseConfig(cfgfile, InitialSymTbl={}, AllowNewVars=True, AllowNewNamespaces=True, Debug=False, LiteralVars=False):
 
global DebugMsgs, ErrMsgs, WarnMsgs, LiteralLines
global CondStack, ALLOWNEW, DEBUG, SymTable, TotalLines, LITERALVARS, INLITERAL
global CondStack, ALLOWNEWVAR, ALLOWNEWNS, DEBUG, SymTable, TotalLines, LITERALVARS, INLITERAL
# Initialize the globals
 
ALLOWNEW = AllowNewVars
ALLOWNEWVAR = AllowNewVars
ALLOWNEWNS = AllowNewNamespaces
DEBUG = Debug
LITERALVARS = LiteralVars
DebugMsgs = []
if NAMESPACE not in SymTable:
SymTable[NAMESPACE] = VarDescriptor()
SymTable[NAMESPACE].Value = ""
SymTable[NAMESPACE].LegalVals.append("")
 
# Report namespace to debug output
 
if DEBUG:
DebugMsg("dNAMESPACE", (STARTUP, STARTUP, SymTable[NAMESPACE].Value))
 
 
# Parse the file
 
if DEBUG:
DebugMsg("dLINEIGNORE", (cfgfile, linenum, orig, dNOTINCLUDE))
return
 
 
# Set the new namespace
# Get the new namespace
ns = line[1:-1]
 
SymTable[NAMESPACE].Value = ns
 
# Save newly seen namespaces in list of legal vals
if ns not in SymTable[NAMESPACE].LegalVals:
SymTable[NAMESPACE].LegalVals.append(ns)
# Flag attempts to create new namespaces if this is disabled
if not ALLOWNEWNS and ns != SymTable[NAMESPACE].Value and ns not in SymTable[NAMESPACE].LegalVals:
ErrorMsg("eNAMESPACENEW", (cfgfile, linenum, SymTable[NAMESPACE].Value))
# Install the new namespace
else:
 
SymTable[NAMESPACE].Value = ns
 
# Save newly seen namespaces in list of legal vals
if ns not in SymTable[NAMESPACE].LegalVals:
SymTable[NAMESPACE].LegalVals.append(ns)
 
if DEBUG:
DebugMsg("dNAMESPACE", (cfgfile, linenum, SymTable[NAMESPACE].Value))
DebugMsg("dPARSEDLINE", (cfgfile, linenum, orig, line))
 
return
 
 
#####
# Munge the variable name to incorporate
# the current namespace
 
# The NAMESPACE variable is special - It is presumed to reset
# the top level namespace.
# the current namespace.
 
if l in (NAMESPACE, NSSEP+NAMESPACE):
 
if l == NSSEP + NAMESPACE:
l=NAMESPACE
 
# Save the new namespace
SymTable[NAMESPACE].Value = r
 
# Add to unique list of namespaces seen
if r not in SymTable[NAMESPACE].LegalVals:
SymTable[NAMESPACE].LegalVals.append(r)
# Flag attempts to create new namespaces if this is disabled
if not ALLOWNEWNS and r != SymTable[NAMESPACE].Value and r not in SymTable[NAMESPACE].LegalVals:
ErrorMsg("eNAMESPACENEW", (cfgfile, linenum, SymTable[NAMESPACE].Value))
# Install the new namespace
else:
 
if l == NSSEP + NAMESPACE:
l=NAMESPACE
 
# Save the new namespace
SymTable[NAMESPACE].Value = r
 
# Add to unique list of namespaces seen
if r not in SymTable[NAMESPACE].LegalVals:
SymTable[NAMESPACE].LegalVals.append(r)
 
if DEBUG:
DebugMsg("dNAMESPACE", (cfgfile, linenum, SymTable[NAMESPACE].Value))
 
# Handle absolute variable references
 
elif l.startswith(NSSEP):
 
if l not in SymTable:
 
# Only do this if new variable creation allowed
if ALLOWNEW:
if ALLOWNEWVAR:
d.Default = r
d.Value = r
SymTable[l] = d