diff --git a/test-tc.py b/test-tc.py index f037d3c..7850d7d 100755 --- a/test-tc.py +++ b/test-tc.py @@ -2,7 +2,7 @@ # Copyright (c) 2003-2004 TundraWare Inc. All Rights Reserved. PROGNAME = "tconfpy Test Driver" -RCSID = "$Id: test-tc.py,v 1.120 2004/03/25 10:38:04 tundra Exp $" +RCSID = "$Id: test-tc.py,v 1.121 2004/04/01 00:09:40 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -15,14 +15,115 @@ PROGINFO = PROGNAME + " " + VERSION BANNER = "%s\n%s\n%s\n" % (PROGINFO, COPYRIGHT, RCSID) -import tconfpy +#---------------------------------------------------------------------------# +# Imports And Other Odds-And-Ends # +#---------------------------------------------------------------------------# + +from tconfpy import * import sys -# Translate booleans into equivalent writeable state + + +# Translation table for booleans into equivalent writeable state TF = {True:"RW", False:"RO"} +#---------------------------------------------------------------------------# +# 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: +# +# Name, Value, Writeable, Type, Default, LegalVals, Min, Max +# +# Where 'None' appears, the init code just uses the default established +# by the VarDescriptor base class. + + +syms = [ ["foo", 1, True, TYPE_INT, 0, [1, 2, 23], 1, 30 ], + ["fox", 1.0, None, TYPE_FLOAT, 0.5, [3.14, 2.73], None, None ], + ["bar", "stringy", None, None, None, [r'^box$', r'^Bax', r'a+bc'], 3, 8 ], + ["baz", 4+5j, None, TYPE_COMPLEX, 0-0j, [], None, None ], + ["boo", True, None, TYPE_BOOL, None, None, None, None ], + ["read", "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. + +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 + + +#---------------------------------------------------------------------------# +# 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 # +#---------------------------------------------------------------------------# + + ########## # Format and return contents of a symbol table entry ########## @@ -82,82 +183,22 @@ files = 1 -syms = {"foo" : 1, - "fox" : 1.0, - "bar" : "stringy", - "baz" : 4+5j, - "boo" : True - } - -symtbl = {} - -for sym in syms: - - # Create and init the descriptor - - d = tconfpy.VarDescriptor() - d.Value = syms[sym] - d.Type = type(syms[sym]) - - # Set default value according to type - - for typ in ((tconfpy.TYPE_BOOL, True), (tconfpy.TYPE_COMPLEX, 0+0j), - (tconfpy.TYPE_FLOAT, 0.00), (tconfpy.TYPE_INT, 0), - (tconfpy.TYPE_STRING, "astring")): - - if typ[0] == d.Type: - d.Default = typ[1] - - # Load the table with this entry - symtbl[sym] = d - -# Put some values in for testing legal variables and bounds - -symtbl["foo"].LegalVals = [1,2, 23] -symtbl["foo"].Min = 1 -symtbl["foo"].Max = 30 -symtbl["fox"].LegalVals = [3.14, 34.0] -symtbl["fox"].Min = -1 -symtbl["fox"].Max = 300 -symtbl["bar"].LegalVals = [r'^foo$', r'a+'] # Add this to see regex error [r'ss^*'] -symtbl["bar"].Min = 2 -symtbl["bar"].Max = 8 - -# Here's a way to inherit the base symbol definition from -# the module and then derive a new object type for your -# own convenience - -class mycmplx(tconfpy.VarDescriptor): - def __init__(self): - tconfpy.VarDescriptor.__init__(self) - self.Value = 2-2j - self.Default = 39-4j - self.Type = tconfpy.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 - - # Make sure we got legit arguments if len(sys.argv) < 2: print BANNER - print "Usage: test-tc.py [debug] [litvars] file file ..." + print "Usage: test-tc.py [symtbl] [debug] [litvars] file file ..." sys.exit(1) # Process all the requested configuration files, # dumping the what tconfpy returns for each one. -DEBUG = False -LITVARS = False +DEBUG = False +LITVARS = False + + +# The default is no pre-defined symbols +st = {} for fn in sys.argv[files:]: @@ -168,11 +209,14 @@ elif fn == "litvars": LITVARS = True + + elif fn == "symtbl": + st = symtbl # Everything else presumed to be a configuration file else: - retval = tconfpy.ParseConfig(fn, symtbl, Debug=DEBUG, LiteralVars=LITVARS) + retval = ParseConfig(fn, st, Debug=DEBUG, LiteralVars=LITVARS) if retval[1]: print "Errors Were Found In '%s'!" % fn