Changed the "nonewvar" option to "nonewvars".
Added support for templating.

Reorganized the code to make it clearer how the initial symbol table
and template dictionary get intialized.

Cleaned up and improved comments throughout.
1 parent 8858d7c commit eb642e3f154625413b8332f0df1536971cdefb87
@tundra tundra authored on 26 Apr 2004
Showing 1 changed file
View
251
test-tc.py
# test-tc.py - A Test Driver For The 'tconfpy' Configuration File Parser
# Copyright (c) 2003-2004 TundraWare Inc. All Rights Reserved.
 
PROGNAME = "tconfpy Test Driver"
RCSID = "$Id: test-tc.py,v 1.133 2004/04/17 19:31:04 tundra Exp $"
RCSID = "$Id: test-tc.py,v 1.134 2004/04/27 02:30:43 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
# Build An Initial Symbol Table #
#---------------------------------------------------------------------------#
 
 
# One of the options of this test driver is to initialize a test symbol
# table. We build that table here in case the user requested it.
 
# Create An Empty Symbol Table
symtbl = {}
 
 
# This test driver makes it easy to add pre-defined symbols of
# your own choosing. The first way is like this ...
 
# The data below is used to populate an initial symbol table. You can add or
# change things here without modifying any of the following code.
# Each symbol has an entry which is a list with the following items:
# One of the options of this test driver is to initialize a test
# symbol table. To make it easy to change the contents of this test
# symbol table, we use the following scheme:
 
 
# The data below is used to populate an initial symbol table. The
# init code takes care of actually reading this table and populating
# the symbol table for you. So, you just have to maintain this table
# properly and the program will do all the actual initialization work
# for you.
 
 
# Each symbol has an entry in the form of a Python list with the
# following items/order:
#
# Name, Value, Writeable, Type, Default, LegalVals, Min, Max
#
# Where 'None' appears, the init code just uses the default established
# by the VarDescriptor base class.
# **** NOTE: Where 'None' appears, the init code just uses the
# ***default*** established by the VarDescriptor base class.
 
 
syms = [ ["int1", 1, True, TYPE_INT, 0, [1, 2, 23], None, None ],
["int2", 1, True, TYPE_INT, 0, [], 1, 30 ],
["ro1", "ReadVar", False, None, None, None, None, None ],
]
 
# Another way to add pre-defined symbols is to explicitly load
# them into the symbol table. The example below does this as
# well as demonstrating how to use inheritance to create
# your own types derviced from the tconfpy variable descriptor
# base class.
 
# Here's a way to inherit the base symbol definition from
# the module and then derive a new object type for your
# own convenience
 
# Note the use of 'super()' here to run each constructor in
# the inheritance tree only once.
# We use the exact same scheme for building variable templates
 
templs = [ ["templb", 1, True, TYPE_BOOL, False, [], None, None ],
["templc", 4+5j, None, TYPE_COMPLEX, 0-0j, [1-1j, 1+1j], None, None ],
["templf", 1.0, None, TYPE_FLOAT, 0.5, [3.14, 2.73], None, None ],
["templi", 1, None, TYPE_INT, 0, [1, 2, 23], None, None ],
["templs", "stringy", None, None, None, [r'^box$', r'^Bax', r'a+bc'], 3, 8 ]
]
 
 
#####
# Deriving Your Own Types
#####
 
# Here's a way to inherit the base symbol definition from the module
# and then derive a new object type for your own convenience. This is
# a way to set your own defaults, for example.
 
# Note the use of 'super()' here to run each constructor in the
# inheritance tree only once.
 
 
class mycmplx(VarDescriptor):
def __init__(self):
super(mycmplx, self).__init__()
self.Value = 2-2j
self.Default = 39-4j
self.Type = TYPE_COMPLEX
 
# Instantiate some of these
mc1 = mycmplx()
mc2 = mycmplx()
 
mc2.Value = 8-3.14159j
mc2.Writeable = False
 
# And stuff them into the symbol table
symtbl["MyComplex1"] = mc1
symtbl["MyComplex2"] = mc2
 
# We'll use this below to manually enter additional variables into the
# initial symbol table.
 
 
#---------------------------------------------------------------------------#
# Nothing Else Should Need Changing Below Here
#---------------------------------------------------------------------------#
 
 
# Fill in the symbol table with stuff defined in the table above
 
for sym in syms:
 
# Create and init the descriptor
des = VarDescriptor()
 
pos = 1
for attr in ("Value", "Writeable", "Type", "Default", "LegalVals", "Min", "Max"):
 
val = sym[pos]
if val != None:
exec("des." + attr + "=val")
 
pos += 1
 
symtbl[sym[0]] = des
 
#---------------------------------------------------------------------------#
# Utility And Support Functions #
#---------------------------------------------------------------------------#
 
 
##########
#####
# Build A Symbol Table Using The Data Table Format Defined above
#####
 
def BuildSymTbl(syms):
 
symtbl ={}
 
for sym in syms:
 
# Create and init the descriptor
des = VarDescriptor()
 
pos = 1
for attr in ("Value", "Writeable", "Type", "Default", "LegalVals", "Min", "Max"):
 
val = sym[pos]
if val != None:
exec("des." + attr + "=val")
 
pos += 1
 
symtbl[sym[0]] = des
 
return symtbl
 
# End of 'BuildSymTbl()'
 
 
#####
# Format and return contents of a symbol table entry
##########
#####
 
 
def dumpsymbol(val, d):
 
return retval
 
# End of 'dumpsymbol()'
 
##########
#####
# Routine to dump returned values
##########
#####
 
 
def dumpreturn(name, data, isdict=False):
 
 
 
#---------------------------------------------------------------------------#
 
##########
#####
# Beginning of test driver
##########
#####
 
files = 1
 
# Make sure we got legit arguments
 
if len(sys.argv) < 2:
print BANNER
print "Usage: test-tc.py [symtbl] [nonewvar] [limitns] [litvars] [debug] file file ..."
print "Usage: test-tc.py [symtbl] [nonewvars] [template] [temponly] [limitns] [litvars] [debug] file file ..."
sys.exit(1)
 
# Process all the requested configuration files,
# dumping the what tconfpy returns for each one.
 
ST = False
ALLOWVAR = True
DEBUG = False
TEMPLATE = False
TEMPONLY = False
LIMITNS = False
LITVARS = False
ST = False
DEBUG = False
 
 
 
for fn in sys.argv[files:]:
 
if fn == "symtbl":
ST = True
 
elif fn == "nonewvar":
elif fn == "nonewvars":
ALLOWVAR = False
 
elif fn == "template":
TEMPLATE = True
 
elif fn == "temponly":
TEMPONLY = True
 
elif fn == "limitns":
LIMITNS = True
 
# The default is no pre-defined symbols
st = {}
 
if ST:
st = symtbl
st = BuildSymTbl(syms)
 
# Another way to add pre-defined symbols is to explicitly
# load them into the symbol table. We'll the derived
# complex type we created earlier for this.
 
mc1 = mycmplx()
mc2 = mycmplx()
 
mc2.Value = 8-3.14159j
mc2.Writeable = False
 
# And stuff them into the symbol table
 
st["MyComplex1"] = mc1
st["MyComplex2"] = mc2
 
if LIMITNS:
 
# Limit the number of namespaces the user can use
des.Max = 8
st["NAMESPACE"] = des
 
# Support for templating
 
# Default is no templates
tl = {}
 
if TEMPLATE:
tl = BuildSymTbl(templs)
 
# Call the parser and process the results
 
retval = ParseConfig(fn, InitialSymTable=st,
AllowNewVars=ALLOWVAR,
Templates=tl,
TemplatesOnly=TEMPONLY,
LiteralVars=LITVARS,
Debug=DEBUG
)