Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 11th, 2012  |  syntax: None  |  size: 6.03 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import xml.dom.minidom
  2.  
  3. #  **********************************************************************
  4. #  Get the parent node path to a particular node
  5. def getNodeParentPath(node, path):
  6.         path = "%s.%s" % (node.nodeName, path)
  7.         if node.parentNode != None and node.parentNode.nodeType != node.DOCUMENT_NODE:
  8.                 path = getNodeParentPath(node.parentNode, path)
  9.         return path
  10.  
  11. #  **********************************************************************
  12. #  Given a node return the full node path to that node
  13. def getNodeFullPath(node):
  14.         path = node.nodeName
  15.         if node.parentNode != None and node.parentNode.nodeType != node.DOCUMENT_NODE:
  16.                 return getNodeParentPath(node.parentNode, path)
  17.         else:
  18.                 return path
  19.  
  20. #  **********************************************************************
  21. #  Given a document return the nodeName off the documents root node or
  22. #  create and return if it doesn't already exist
  23. def getRootNode(nodeName, resultsElement, resultsDoc):
  24.         nodes = resultsElement.getElementsByTagName(nodeName)
  25.         node = nodes.item(0)
  26.         if node == None:
  27.                 node = resultsDoc.createElement(nodeName)
  28.                 resultsElement.appendChild(node)
  29.         return node
  30.        
  31. #  **********************************************************************
  32. #  Create an Duration_error node on the resultsElement parameter
  33. def reportDurationError(baselineDuration, duration, submissionNode):
  34.         submissionNode.setAttributeNS('Duration_delta', 'Duration_delta', '%f' % (duration - baselineDuration))
  35.  
  36. #  **********************************************************************
  37. #  Create an Attribute_mismatch node on the resultsElement parameter
  38. def reportAttributeMismatch(node, attributeName, expecting, found, resultElement, resultsDoc):
  39.         # create an xml node to hold the error
  40.         errorNode = resultsDoc.createElement("Attribute_mismatch")
  41.        
  42.         # get full path of the node that held the mismatched attribute
  43.         path = getNodeFullPath(node)
  44.        
  45.         # set appropriate attributes on the error node and append to the resultElement
  46.         errorNode.setAttributeNS("Node", "Node", path)
  47.         errorNode.setAttributeNS("Attribute", "Attribute", attributeName)
  48.         errorNode.setAttributeNS("Found", "Found", found)
  49.         errorNode.setAttributeNS("Expecting", "Expecting", expecting)
  50.        
  51.         getRootNode("Discrepencies", resultElement, resultsDoc).appendChild(errorNode)
  52.  
  53. #  **********************************************************************
  54. #  Create a Missing_data node on the resultsElement parameter
  55. def reportMissingData(node, attributeName, resultsElement, resultsDoc):
  56.         # create an xml node to hold the error
  57.         errorNode = resultsDoc.createElement("Missing_data")
  58.        
  59.         # get full path of the node that held the mismatched attribute
  60.         path = getNodeFullPath(node)
  61.        
  62.         # set appropriate attributes on the error node and append to the resultElement
  63.         errorNode.setAttributeNS("Node", "Node", path)
  64.         errorNode.setAttributeNS("Attribute", "Attribute", attributeName)
  65.        
  66.         getRootNode("Data_errors", resultsElement, resultsDoc).appendChild(errorNode)
  67.  
  68. #  **********************************************************************
  69. #  Given a dump filename (dump from a pathfinder submission), open the file
  70. #  and return the first model node from the xml
  71. def getModelNodeFromDump(dumpFilename):
  72.         dumpDoc = xml.dom.minidom.parse(dumpFilename)
  73.         submissionModelNodes = dumpDoc.getElementsByTagName('Results')
  74.         submissionModelNode = None
  75.         if submissionModelNodes.length > 0:
  76.                 submissionModelNode = submissionModelNodes[0]
  77.         return submissionModelNode
  78.        
  79. #  **********************************************************************
  80. #  Get a particular submission node from a results doc
  81. def getSubmissionNode(submissionName, resultsDoc):
  82.         toReturn = None
  83.         submissions = resultsDoc.getElementsByTagName('Submission_report')
  84.         for submissionElement in submissions:
  85.                 thisSubmissionName = submissionElement.getAttribute('Name')
  86.                 if thisSubmissionName == submissionName:
  87.                         toReturn = submissionElement
  88.                         break
  89.        
  90.         return toReturn
  91.  
  92. #  **********************************************************************
  93. #  Danger, danger! Recursive...
  94. def compareNodes(node1, node2, resultElement, resultsDoc, indentString):
  95.         # log out some debuggy stuff
  96.         #print "%sComparing nodes: %s and %s" % (indentString, node1.localName, node2.localName)
  97.         errorCount = 0
  98.        
  99.         # make sure we've got the node we expect to have
  100.         if node1.localName != node2.localName:
  101.                 # chuck an error in the error doc
  102.                 ReportNodeMismatch(node1, node2, resultElement, resultsDoc)
  103.                 errorCount = errorCount + 1
  104.                 return
  105.         else:
  106.                 # we have matching nodes... check their attributes
  107.                 for attributeName in node1.attributes.keys():
  108.                         if node2.hasAttribute(attributeName):
  109.                                 attribute1 = node1.getAttribute(attributeName)
  110.                                 attribute2 = node2.getAttribute(attributeName)
  111.                                
  112.                                 if attribute1 != attribute2:
  113.                                         # values have changed report the chance
  114.                                         reportAttributeMismatch(node1, attributeName, attribute1, attribute2, resultElement, resultsDoc)
  115.                                         errorCount = errorCount + 1
  116.                         else:
  117.                                 # node2 doesn't have this expected attribute - report as missing
  118.                                 reportMissingData(node2, attributeName, resultElement, resultsDoc)
  119.                                 errorCount = errorCount + 1
  120.                
  121.                 # iterate through any child nodes, checking them in turn
  122.                 if node1.hasChildNodes():
  123.                         node1Child = node1.firstChild
  124.                         while (node1Child != None):
  125.                                 if (node1Child.nodeType != node1Child.TEXT_NODE):
  126.                                         node2Children = node2.getElementsByTagName(node1Child.nodeName)
  127.                                         if node2Children.item(0) == None:
  128.                                                 reportMissingData(node1Child, "", resultElement, resultsDoc)
  129.                                                 errorCount = errorCount + 1
  130.                                         else:
  131.                                                 errorCount = errorCount + compareNodes(node1Child, node2Children.item(0), resultElement, resultsDoc, indentString + " ")
  132.                                 node1Child = node1Child.nextSibling
  133.        
  134.         return errorCount
  135.  
  136. #  **********************************************************************
  137. def compareSubmissionDumps(baselineElement, newSubmissionElement, resultElement, resultsDoc):
  138.         # compare the documents, this function is recursive
  139.         return compareNodes(baselineElement, newSubmissionElement, resultElement, resultsDoc, "")