Newer
Older
tconfpy / test-tc.py
  1. #!/usr/bin/env python
  2. # Copyright (c) 2003-2004 TundraWare Inc. All Rights Reserved.
  3.  
  4. PROGNAME = "tconfpy Test Driver"
  5. RCSID = "$Id: test-tc.py,v 1.118 2004/03/21 13:50:56 tundra Exp $"
  6. VERSION = RCSID.split()[2]
  7.  
  8. # Copyright Information
  9.  
  10. CPRT = chr(169)
  11. DATE = "2004"
  12. OWNER = "TundraWare Inc."
  13. RIGHTS = "All Rights Reserved"
  14. COPYRIGHT = "Copyright %s %s %s, %s." % (CPRT, DATE, OWNER, RIGHTS)
  15. PROGINFO = PROGNAME + " " + VERSION
  16. BANNER = "%s\n%s\n%s\n" % (PROGINFO, COPYRIGHT, RCSID)
  17.  
  18. import tconfpy
  19. import sys
  20.  
  21. # Translate booleans into equivalent writeable state
  22.  
  23. TF = {True:"RW", False:"RO"}
  24.  
  25.  
  26. ##########
  27. # Format and return contents of a symbol table entry
  28. ##########
  29.  
  30.  
  31. def dumpsymbol(val, d):
  32.  
  33. retval = str(val) + " " * (15 - len(val))
  34.  
  35. for v, p in ((d.Value, 12),
  36. (TF[d.Writeable], 2),
  37. (str(d.Type).split()[1][1:-2], 7),
  38. (d.Default, 8),
  39. (d.LegalVals, 8),
  40. (d.Min, 8),
  41. (d.Max, 8)):
  42.  
  43. retval += str(v) + (p - len(str(v)) + 2) * ' '
  44.  
  45. return retval
  46.  
  47. # End of 'dumpsymbol()'
  48.  
  49. ##########
  50. # Routine to dump returned values
  51. ##########
  52.  
  53.  
  54. def dumpreturn(name, data, isdict=False):
  55.  
  56. print name
  57. print len(name) * '=' + '\n'
  58.  
  59.  
  60. items = data
  61. if isdict:
  62. items = data.keys()
  63. items.sort()
  64.  
  65. for val in items:
  66. if isdict:
  67. print dumpsymbol(val, data[val])
  68. else:
  69. print val
  70.  
  71. print '\n'
  72.  
  73. # End of 'dumpreturn()'
  74.  
  75.  
  76. #---------------------------------------------------------------------------#
  77.  
  78. ##########
  79. # Beginning of test driver
  80. ##########
  81.  
  82. files = 1
  83.  
  84. syms = {"foo" : 1,
  85. "fox" : 1.0,
  86. "bar" : "stringy",
  87. "baz" : 4+5j,
  88. "boo" : True
  89. }
  90.  
  91. symtbl = {}
  92.  
  93. for sym in syms:
  94.  
  95. # Create and init the descriptor
  96. d = tconfpy.VarDescriptor()
  97. d.Value = syms[sym]
  98. d.Type = type(syms[sym])
  99.  
  100. # Set default value according to type
  101.  
  102. for typ in ((tconfpy.TYPE_BOOL, True), (tconfpy.TYPE_COMPLEX, 0+0j),
  103. (tconfpy.TYPE_FLOAT, 0.00), (tconfpy.TYPE_INT, 0),
  104. (tconfpy.TYPE_STRING, "")):
  105. if typ[0] == d.Type:
  106. d.Default = typ[1]
  107.  
  108. # Load the table with this entry
  109. symtbl[sym] = d
  110.  
  111. symtbl["fox"].Writeable = False
  112.  
  113.  
  114.  
  115. # Here's a way to inherit the base symbol definition from
  116. # the module and then derive a new object type for your
  117. # own convenience
  118.  
  119. class mycmplx(tconfpy.VarDescriptor):
  120. def __init__(self):
  121. tconfpy.VarDescriptor.__init__(self)
  122. self.Value = 2-2j
  123. self.Default = 39-4j
  124. self.Type = tconfpy.TYPE_COMPLEX
  125.  
  126. # Instantiate some of these
  127. mc1 = mycmplx()
  128. mc2 = mycmplx()
  129.  
  130. mc2.Value = 8-3.14159j
  131. mc2.Writeable = False
  132.  
  133. # And stuff them into the symbol table
  134. symtbl["MyComplex1"] = mc1
  135. symtbl["MyComplex2"] = mc2
  136.  
  137.  
  138. # Make sure we got legit arguments
  139.  
  140. if len(sys.argv) < 2:
  141. print BANNER
  142. print "Usage: test-tc.py [debug] [litvars] file file ..."
  143. sys.exit(1)
  144.  
  145. # Process all the requested configuration files,
  146. # dumping the what tconfpy returns for each one.
  147.  
  148. DEBUG = False
  149. LITVARS = False
  150.  
  151. for fn in sys.argv[files:]:
  152.  
  153. # Handle inline options
  154. if fn == "debug":
  155. DEBUG = True
  156.  
  157. elif fn == "litvars":
  158. LITVARS = True
  159. # Everything else presumed to be a configuration file
  160. else:
  161. retval = tconfpy.ParseConfig(fn, symtbl, Debug=DEBUG, LiteralVars=LITVARS)
  162.  
  163. if retval[1]:
  164. print "Errors Were Found In '%s'!" % fn
  165. else:
  166. print "No Errors Found In '%s'!" % fn
  167.  
  168. print
  169.  
  170. dumpreturn("SYMBOL TABLE", retval[0], isdict=True)
  171. dumpreturn("ERRORS", retval[1])
  172. dumpreturn("WARNINGS", retval[2])
  173. dumpreturn("DEBUG", retval[3])
  174. dumpreturn("LITERAL LINES", retval[4])
  175.  
  176.  
  177.