Newer
Older
tconfpy / test-tc.py
#!/usr/bin/env python
# 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 $"
VERSION = RCSID.split()[2]

# Copyright Information

CPRT         = chr(169)
DATE         = "2004"
OWNER        = "TundraWare Inc."
RIGHTS       = "All Rights Reserved"
COPYRIGHT    = "Copyright %s %s %s,  %s." % (CPRT, DATE, OWNER, RIGHTS)
PROGINFO     = PROGNAME + " " + VERSION
BANNER       = "%s\n%s\n%s\n" % (PROGINFO, COPYRIGHT, RCSID)

import tconfpy
import sys

# Translate booleans into equivalent writeable state

TF = {True:"RW", False:"RO"}


##########
# Format and return contents of a symbol table entry
##########


def dumpsymbol(val, d):

    retval = str(val) + " " * (15 - len(val))

    for v, p in ((d.Value, 12),
                 (TF[d.Writeable], 2),
                 (str(d.Type).split()[1][1:-2], 7),
                 (d.Default, 8),
                 (d.LegalVals, 8),
                 (d.Min, 8),
                 (d.Max, 8)):

        retval += str(v) + (p - len(str(v)) + 2) * ' '

    return retval

# End of 'dumpsymbol()'

##########
# Routine to dump returned values
##########


def dumpreturn(name, data, isdict=False):

    print name
    print len(name) * '=' + '\n'


    items = data
    if isdict:
        items = data.keys()
        items.sort()

    for val in items:
        if isdict:
            print dumpsymbol(val, data[val])
        else:
            print val

    print '\n'
    

# End of 'dumpreturn()'


#---------------------------------------------------------------------------#

##########
# Beginning of test driver
##########

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 ..."
    sys.exit(1)

# Process all the requested configuration files,
# dumping the what tconfpy returns for each one.

DEBUG   = False
LITVARS = False

for fn in sys.argv[files:]:

    # Handle inline options
    
    if fn == "debug":
        DEBUG = True

    elif fn == "litvars":
        LITVARS = True
    
    # Everything else presumed to be a configuration file
    else:
        
        retval = tconfpy.ParseConfig(fn, symtbl, Debug=DEBUG, LiteralVars=LITVARS)

        if retval[1]:
            print "Errors Were Found In '%s'!" % fn
        else:
            print "No Errors Found In '%s'!" % fn

        print

        dumpreturn("SYMBOL TABLE", retval[0], isdict=True)
        dumpreturn("ERRORS", retval[1])
        dumpreturn("WARNINGS", retval[2])
        dumpreturn("DEBUG", retval[3])
        dumpreturn("LITERAL LINES", retval[4])