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.119 2004/03/25 00:58:32 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["boo"].Writeable = False
  112. symtbl["fox"].Min = -1
  113. symtbl["fox"].Max = 200
  114. symtbl["foo"].Min = -100
  115. symtbl["bar"].Min = 8
  116. symtbl["bar"].Max = 8
  117.  
  118.  
  119.  
  120.  
  121.  
  122. # Here's a way to inherit the base symbol definition from
  123. # the module and then derive a new object type for your
  124. # own convenience
  125.  
  126. class mycmplx(tconfpy.VarDescriptor):
  127. def __init__(self):
  128. tconfpy.VarDescriptor.__init__(self)
  129. self.Value = 2-2j
  130. self.Default = 39-4j
  131. self.Type = tconfpy.TYPE_COMPLEX
  132.  
  133. # Instantiate some of these
  134. mc1 = mycmplx()
  135. mc2 = mycmplx()
  136.  
  137. mc2.Value = 8-3.14159j
  138. mc2.Writeable = False
  139.  
  140. # And stuff them into the symbol table
  141. symtbl["MyComplex1"] = mc1
  142. symtbl["MyComplex2"] = mc2
  143.  
  144.  
  145. # Make sure we got legit arguments
  146.  
  147. if len(sys.argv) < 2:
  148. print BANNER
  149. print "Usage: test-tc.py [debug] [litvars] file file ..."
  150. sys.exit(1)
  151.  
  152. # Process all the requested configuration files,
  153. # dumping the what tconfpy returns for each one.
  154.  
  155. DEBUG = False
  156. LITVARS = False
  157.  
  158. for fn in sys.argv[files:]:
  159.  
  160. # Handle inline options
  161. if fn == "debug":
  162. DEBUG = True
  163.  
  164. elif fn == "litvars":
  165. LITVARS = True
  166. # Everything else presumed to be a configuration file
  167. else:
  168. retval = tconfpy.ParseConfig(fn, symtbl, Debug=DEBUG, LiteralVars=LITVARS)
  169.  
  170. if retval[1]:
  171. print "Errors Were Found In '%s'!" % fn
  172. else:
  173. print "No Errors Found In '%s'!" % fn
  174.  
  175. print
  176.  
  177. dumpreturn("SYMBOL TABLE", retval[0], isdict=True)
  178. dumpreturn("ERRORS", retval[1])
  179. dumpreturn("WARNINGS", retval[2])
  180. dumpreturn("DEBUG", retval[3])
  181. dumpreturn("LITERAL LINES", retval[4])
  182.  
  183.  
  184.