Package qm :: Package test :: Package classes :: Module xml_result_stream
[hide private]
[frames] | no frames]

Source Code for Module qm.test.classes.xml_result_stream

  1  ######################################################################## 
  2  # 
  3  # File:   xml_result_stream.py 
  4  # Author: Mark Mitchell 
  5  # Date:   10/10/2001 
  6  # 
  7  # Contents: 
  8  #   XMLResultStream, XMLResultReader 
  9  # 
 10  # Copyright (c) 2001, 2002, 2003 by CodeSourcery, LLC.  All rights reserved.  
 11  # 
 12  ######################################################################## 
 13   
 14  ######################################################################## 
 15  # Imports 
 16  ######################################################################## 
 17   
 18  import qm.xmlutil 
 19  from   qm.test.file_result_reader import FileResultReader 
 20  from   qm.test.result import Result 
 21  from   qm.test.file_result_stream import FileResultStream 
 22   
 23  ######################################################################## 
 24  # classes 
 25  ######################################################################## 
 26   
27 -class XMLResultStream(FileResultStream):
28 """An 'XMLResultStream' writes out results as XML. 29 30 An 'XMLResultStream' writes out results as XML. This stream is 31 used to write out QMTest results files. The DTD is designed in 32 such a way that if QMTest is terminated in the middle of a test 33 run, the file will still be nearly valid, as long as the 34 interruption did not occur in the midst of writing out an 35 individual result. The closing tag for the results file will 36 be missing.""" 37
38 - def __init__(self, arguments = None, **args):
39 40 # Initialize the base class. 41 super(XMLResultStream, self).__init__(arguments, **args) 42 43 # Create an XML document, since the DOM API requires you 44 # to have a document when you create a node. 45 self.__document = qm.xmlutil.create_dom_document( 46 public_id="QMTest/Result", 47 document_element_tag="results") 48 # Write out the prologue. 49 self.file.write("<?xml version='1.0' encoding='ISO-8859-1'?>\n") 50 self.file.write('<!DOCTYPE results PUBLIC "%s" "%s">\n' 51 % (qm.xmlutil.make_public_id("QMTest/Result"), 52 qm.xmlutil.make_system_id("qmtest/result.dtd"))) 53 # Begin the list of results. 54 self.file.write("<results>\n")
55 56
57 - def WriteAnnotation(self, key, value):
58 59 element = self.__document.createElement("annotation") 60 element.setAttribute("key", key) 61 text = self.__document.createTextNode(value) 62 element.appendChild(text) 63 element.writexml(self.file, addindent = " ", newl = "\n")
64 65
66 - def WriteResult(self, result):
67 68 element = result.MakeDomNode(self.__document) 69 element.writexml(self.file, indent = " ", addindent = " ", 70 newl = "\n")
71 72
73 - def Summarize(self):
74 75 # Finish the list of results. 76 self.file.write("\n</results>\n") 77 78 FileResultStream.Summarize(self)
79 80 81
82 -class XMLResultReader(FileResultReader):
83 """Reads in 'Result's from an XML-formatted results file. 84 85 To write such a file, see 'XMLResultStream'.""" 86
87 - def __init__(self, arguments = None, **args):
88 89 super(XMLResultReader, self).__init__(arguments, **args) 90 91 # Make sure that this file really is an XML result stream. 92 tag = self.file.read(5) 93 if tag != "<?xml": 94 raise FileResultReader.InvalidFile, \ 95 "file is not an XML result stream" 96 self.file.seek(0) 97 98 document = qm.xmlutil.load_xml(self.file) 99 node = document.documentElement 100 results = node.getElementsByTagName("result") 101 self.__node_iterator = iter(results) 102 103 # Read out annotations 104 self._annotations = {} 105 annotation_nodes = node.getElementsByTagName("annotation") 106 for node in annotation_nodes: 107 key = node.getAttribute("key") 108 value = qm.xmlutil.get_dom_text(node).strip() 109 self._annotations[key] = value
110 111
112 - def GetAnnotations(self):
113 114 return self._annotations
115 116
117 - def GetResult(self):
118 119 try: 120 return self._GetResultFromDomNode(self.__node_iterator.next()) 121 except StopIteration: 122 return None
123 124
125 - def _GetResultFromDomNode(self, node):
126 """Extract a result from a DOM node. 127 128 'node' -- A DOM node corresponding to a "result" element. 129 130 returns -- A 'Result' object.""" 131 132 assert node.tagName == "result" 133 # Extract the outcome. 134 outcome = node.getAttribute("outcome") 135 # If the outcome doesn't exist as an attribute, fall back 136 # to the outcome child node. 137 if not outcome: 138 outcome = qm.xmlutil.get_child_text(node, "outcome").strip() 139 # Extract the test ID. 140 test_id = node.getAttribute("id") 141 kind = node.getAttribute("kind") 142 # Build a Result. 143 result = Result(kind, test_id, outcome) 144 # Extract annotations. 145 for n in node.childNodes: 146 if n.nodeType != node.ELEMENT_NODE: 147 continue 148 if n.tagName == "annotation": 149 quoted = 1 150 elif n.tagName == "property": 151 # Versions of QMTest before 2.1 used the "property" tag, 152 # and did not quote the contained text. 153 quoted = 0 154 else: 155 continue 156 # Get the name of the annotation. 157 name = n.getAttribute("name") 158 # Get the value of the annotation. 159 value = qm.xmlutil.get_dom_text(n) 160 if quoted: 161 # Remove whitespace and then remove the enclosing quotes. 162 value = value.strip()[1:-1] 163 # Remember the annotation. 164 result[name] = value 165 166 return result
167 168 169 170 ######################################################################## 171 # Local Variables: 172 # mode: python 173 # indent-tabs-mode: nil 174 # fill-column: 72 175 # End: 176