Consolidated the builtins into the master symbol table.
Defined indexing constants to manipulate a variable descriptor entry.
1 parent 1bdcfd4 commit 3fb60fc02795d9ce414a0f2b16119e27f60997dc
@tundra tundra authored on 13 Mar 2004
Showing 1 changed file
View
142
tconfpy.py
 
# 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
 
 
 
# 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
 
INCLUDE = ".include"
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
 
reVARREF = r"\%s.+?\%s" % (DELIML, DELIMR) # Variable reference
WarnMsgs = [] # Place to store and return warnings
 
 
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
]
 
 
 
#----------------------------------------------------------#
#----------------------------------------------------------#
# 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
ErrMsgs = []
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)
 
# 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
##########
 
if line.startswith(INCLUDE):
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
#
#####
 
if line.startswith(IF):
line = line.split(IF)[1].strip()
 
else:
for ref in Reserved.keys():
line = line.replace("%s%s%s" % (DELIML, ref, DELIMR), Reserved[ref])
 
line = DeRefVar(line, cfgfile, linenum)
 
##########
# End Of Line Parser
##########
 
DebugMsg(dPARSEDLINE %(cfgfile, linenum, orig, line))
# End of 'ParseLine'