| |
---|
| | |
---|
| | # Program Information |
---|
| | |
---|
| | PROGNAME = "tconfpy" |
---|
| | RCSID = "$Id: tconfpy.py,v 2.101 2005/01/13 23:03:24 tundra Exp $" |
---|
| | RCSID = "$Id: tconfpy.py,v 2.102 2005/01/14 08:15:52 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 |
---|
| | INMEMORY = "In-Memory Configuration" # Configuration is in-memory |
---|
| | MSGPROMPT = "%s>" |
---|
| | PTR = " ---> " # Textual pointer for debug output |
---|
| | STARTUP = "STARTUP" # Indicates message before any lines processed |
---|
| | |
---|
| |
---|
| | |
---|
| | 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["eCONFIGTYPE"] = "Don't Know How To Process Configurations Of Type '%s'" |
---|
| | 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" |
---|
| |
---|
| | #----------------------------------------------------------# |
---|
| | # Public API To Module # |
---|
| | #----------------------------------------------------------# |
---|
| | |
---|
| | def ParseConfig(cfgfile, |
---|
| | def ParseConfig(configuration, |
---|
| | CallingProgram=PROGINFO, |
---|
| | InitialSymTable=SymbolTable(), |
---|
| | AllowNewVars=True, |
---|
| | Templates=Template(), |
---|
| |
---|
| | |
---|
| | if SymTable.DEBUG: |
---|
| | DebugMsg("dNAMESPACE", (STARTUP, STARTUP, SymTable.Symbols[NAMESPACE].Value)) |
---|
| | |
---|
| | |
---|
| | # Parse the file |
---|
| | |
---|
| | ParseFile(cfgfile, eSTARTUP, 0) |
---|
| | # We now can parse the configuration, either in a file or in-memory. |
---|
| | |
---|
| | cfgtype = type(configuration) |
---|
| | |
---|
| | # It's a file if the passed parameter is a string - assumed to be the filename |
---|
| | |
---|
| | if cfgtype == type('x'): |
---|
| | ParseFile(configuration, eSTARTUP, 0) |
---|
| | configname = configuration # Name of the configuration |
---|
| | |
---|
| | # It's an in-memory configuration if the passed parameter is a list |
---|
| | |
---|
| | elif cfgtype == type([]): |
---|
| | ParseInMemory(configuration) |
---|
| | configname = INMEMORY |
---|
| | pass |
---|
| | |
---|
| | # Anything else is illegal |
---|
| | |
---|
| | else: |
---|
| | ErrorMsg("eCONFIGTYPE", str(cfgtype).split()[-1][1:-2]) |
---|
| | |
---|
| | # Make sure we had all condition blocks balanced with matching '.endif' |
---|
| | |
---|
| | finalcond = len(SymTable.CondStack) |
---|
| | if finalcond != 1: |
---|
| | ErrorMsg("eENDIFMISS", (cfgfile, ATEOF, finalcond-1)) |
---|
| | ErrorMsg("eENDIFMISS", (configname, ATEOF, finalcond-1)) |
---|
| | |
---|
| | # Make sure we ended any literal processing properly |
---|
| | if SymTable.INLITERAL: |
---|
| | WarningMsg("wENDLITMISS", (cfgfile, ATEOF)) |
---|
| | WarningMsg("wENDLITMISS", (configname, ATEOF)) |
---|
| | |
---|
| | # Return the parsing results |
---|
| | |
---|
| | if SymTable.DEBUG: |
---|
| | DebugMsg("dNUMLINES", (cfgfile, SymTable.TotalLines)) |
---|
| | DebugMsg("dNUMLINES", (configname, SymTable.TotalLines)) |
---|
| | |
---|
| | # Strip out Prefefined Variables if user does not want them |
---|
| | |
---|
| | if not ReturnPredefs: |
---|
| |
---|
| | except IOError: |
---|
| | ErrorMsg("eCONFOPEN", (current_cfg, current_linenum, cfgfile)) # Record the error |
---|
| | |
---|
| | # End of 'ParseFile()' |
---|
| | |
---|
| | |
---|
| | ########## |
---|
| | # Parse An In-Memory Configuration |
---|
| | ########## |
---|
| | |
---|
| | def ParseInMemory(cfglist): |
---|
| | |
---|
| | global SymTable |
---|
| | |
---|
| | linenum=0 |
---|
| | |
---|
| | # Process and massage the configuration file |
---|
| | for line in cfglist: |
---|
| | linenum += 1 |
---|
| | SymTable.TotalLines += 1 |
---|
| | |
---|
| | # Parse this line |
---|
| | ParseLine(line, INMEMORY, linenum) |
---|
| | |
---|
| | # End of 'ParseInMemory()' |
---|
| | |
---|
| | |
---|
| | ########## |
---|
| | # Parse A Line |
---|
| |
---|
| | |