diff --git a/tconfpy.py b/tconfpy.py index 0476b4f..7e29081 100755 --- a/tconfpy.py +++ b/tconfpy.py @@ -6,7 +6,7 @@ # Program Information PROGNAME = "tconfpy" -RCSID = "$Id: tconfpy.py,v 1.110 2004/03/13 00:19:04 tundra Exp $" +RCSID = "$Id: tconfpy.py,v 1.111 2004/03/14 01:39:00 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -69,11 +69,17 @@ # Reserved Symbols -COMMENT = r'#' # Comment introducer character +HASH = r'#' +COMMENT = HASH # Comment introducer character DELIML = r'[' # Left delimiter for vbl reference DELIMR = r']' # Right delimiter for vbl reference DOLLAR = r'$' # Used to note enviro vbl +EQUAL = r'=' # Used in vbl definition +EQUIV = r"==" # Used in conditional tests +NOTEQUIV = r"!=" # Used in conditional tests +Reserved = ["DELIML", "DELIMR", "DOLLAR", "EQUAL", "EQUIV", "NOTEQUIV", + "HASH", "INCLUDE", "ENDIF", "IF"] # Control and conditional symbols @@ -81,18 +87,6 @@ ENDIF = ".endif" IF = ".if" -# Table of reserved symbols used by parser. User is able -# to include this in the right side of a variable definition -# via [reserved sym]. - -Reserved = {"DELIML" : DELIML, - "DELIMR" : DELIMR, - "DOLLAR" : DOLLAR, - "HASH" : COMMENT, - "INCLUDE" : INCLUDE, - "ENDIF" : ENDIF, - "IF" : IF - } # Regular Expressions @@ -119,10 +113,63 @@ CondStack = [True,] # Conditional stack -SymTable = {} # Results of the parsing stored here TotalLines = 0 # Total number of lines parsed +########## +# Symbol Table +########## + +# Symbol Table is a dictionary in the form: +# +# {varname : descriptor} +# +# where the descriptor is a list: +# +# [value, isbuiltin, iswriteable, type, default value, [list of legal vals], min, max] + + +# Indexes Into Symbol Table Variable Descriptor + +SYM_VALUE = 0 +SYM_WRITE = 1 +SYM_TYPE = 2 +SYM_DEFAULT = 3 +SYM_VALUES = 4 +SYM_MIN = 5 +SYM_MAX = 6 + +# Legal Variable Types + +TYP_BOOL = 'b' +TYPE_CMPLX = 'x' +TYP_FLOAT = 'f' +TYP_INT = 'i' +TYP_STRING = 's' + +# Boolean Flags + +SYM_WRITEABLE = True + + +# Initialize the table using the builtin symbols + +SymTable = {} + +for sym in Reserved: + + SymTable[sym] = [eval(sym), + not SYM_WRITEABLE, + TYP_STRING, + eval(sym), + None, + None, + None + ] + + + + #----------------------------------------------------------# # Prompts, & Application Strings # @@ -248,7 +295,7 @@ # Public API To Module # #----------------------------------------------------------# -def ParseConfig(cfgfile, Options={}, IgnoreCase=False, debug=False): +def ParseConfig(cfgfile, symtbl={}, IgnoreCase=False, debug=False): global DebugMsgs, ErrMsgs, WarnMsgs global CondStack, DEBUG, IGNORECASE, SymTable, TotalLines @@ -264,9 +311,13 @@ WarnMsgs = [] CondStack = [True,] - SymTable = Options TotalLines = 0 + # Add any passed symbols to the SymbolTable + + for sym in symtbl: + SymTable[sym] = symtbl[sym] + # Parse the file ParseFile(cfgfile) @@ -298,6 +349,21 @@ # End of 'ConditionLine()' + +########## +# Dereference Variables +########## + +def DeRefVar(line, cfgfile, linenum): + + for sym in Reserved: + line = line.replace("%s%s%s" % (DELIML, sym, DELIMR), SymTable[sym][SYM_VALUE]) + + return line + +# End of 'DeRefVar()' + + ########## # Parse A File ########## @@ -388,14 +454,24 @@ ParseFile(line.split(INCLUDE)[1].strip()) ##### - # Replace Explicit References To Reserved Symbols + # .if Processing + # + # Must be one of the following forms - + # + # .if [var] + # .if [var] == string + # .if [var] != string + # ##### - else: - for ref in Reserved.keys(): - line = line.replace("%s%s%s" % (DELIML, ref, DELIMR), Reserved[ref]) - + if line.startswith(IF): + line = line.split(IF)[1].strip() + + else: + line = DeRefVar(line, cfgfile, linenum) + + ########## # End Of Line Parser ########## @@ -409,3 +485,4 @@ DebugMsg(dPARSEDLINE %(cfgfile, linenum, orig, line)) # End of 'ParseLine' +