diff --git a/tconfpy.py b/tconfpy.py index 43a6cf9..c448a7b 100755 --- a/tconfpy.py +++ b/tconfpy.py @@ -6,7 +6,7 @@ # 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 @@ -56,11 +56,11 @@ # Formatting Constants -MSGPROMPT = "%s>" -FILENUM = "[File: %s Line: %s] " # Display filename and linenum ATEOF = "EOF" # Use as line number when at EOF +FILENUM = "[File: %s Line: %s] " # Display filename and linenum +MSGPROMPT = "%s>" PTR = " ---> " # Textual pointer for debug output - +STARTUP = "STARTUP" # Indicates message before any lines processed # Special And Reserved Symbols @@ -138,7 +138,8 @@ #----------------------------------------------------------# -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 @@ -263,6 +264,7 @@ # 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" @@ -285,28 +287,29 @@ # 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'" ########### @@ -399,14 +402,15 @@ # 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 @@ -494,6 +498,11 @@ SymTable[NAMESPACE].Value = "" SymTable[NAMESPACE].LegalVals.append("") + # Report namespace to debug output + + if DEBUG: + DebugMsg("dNAMESPACE", (STARTUP, STARTUP, SymTable[NAMESPACE].Value)) + # Parse the file @@ -790,19 +799,25 @@ DebugMsg("dLINEIGNORE", (cfgfile, linenum, orig, dNOTINCLUDE)) return - - # Set the new namespace + # Get the new namespace ns = line[1:-1] - SymTable[NAMESPACE].Value = 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: - # Save newly seen namespaces in list of legal vals - if ns not in SymTable[NAMESPACE].LegalVals: - SymTable[NAMESPACE].LegalVals.append(ns) + 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 @@ -1042,19 +1057,30 @@ # 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 + # 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: - # Save the new namespace - SymTable[NAMESPACE].Value = r + if l == NSSEP + NAMESPACE: + l=NAMESPACE - # Add to unique list of namespaces seen - if r not in SymTable[NAMESPACE].LegalVals: - SymTable[NAMESPACE].LegalVals.append(r) + # 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 @@ -1078,7 +1104,7 @@ 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