Salome HOME
Correct the message when incompatible options are called
[tools/sat.git] / src / xmlManager.py
index ee3e425c906118a53545bba8cb369573775660f4..78e84e7d8c9054731b245f7af595a186c3e16c05 100644 (file)
@@ -45,12 +45,15 @@ class XmlLogFile(object):
         # Initialize the field that contain the xml in memory
         self.xmlroot = etree.Element(rootname, attrib = attrib)
     
-    def write_tree(self, stylesheet=None):
+    def write_tree(self, stylesheet=None, file_path = None):
         '''Write the xml tree in the log file path. Add the stylesheet if asked.
         
         :param stylesheet str: The stylesheet to apply to the xml file
         '''
-        f = open(self.logFile, 'w')
+        log_file_path = self.logFile
+        if file_path:
+            log_file_path = file_path
+        f = open(log_file_path, 'w')
         f.write("<?xml version='1.0' encoding='utf-8'?>\n")
         if stylesheet:
             f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % 
@@ -143,7 +146,7 @@ class ReadXmlFile(object):
         :rtype: str
         '''
         return self.xmlroot.find(node).text
-
+    
 def add_simple_node(root_node, node_name, text=None, attrib={}):
     '''Add a node with some attibutes and text to the root node.
 
@@ -166,3 +169,41 @@ def append_node_attrib(root_node, attrib):
     :param attrib dixt: The attrib to append
     '''
     root_node.attrib.update(attrib)
+
+def find_node_by_attrib(xmlroot, name_node, key, value):
+    '''Find the nfirst ode from xmlroot that has name name_node and that has in 
+       its attributes {key : value}. Return the node
+    
+    :param xmlroot etree.Element: the Etree element where to search
+    :param name_node str: the name of node to search
+    :param key str: the key to search
+    :param value str: the value to search
+    :return: the found node
+    :rtype: xmlroot etree.Element
+    '''
+    l_nodes =  xmlroot.findall(name_node)
+    for node in l_nodes:
+        if key not in node.attrib.keys():
+            continue
+        if node.attrib[key] == value:
+            return node
+    return None
+    
+
+def write_report(filename, xmlroot, stylesheet):
+    """Writes a report file from a XML tree.
+    
+    :param filename str: The path to the file to create
+    :param xmlroot etree.Element: the Etree element to write to the file
+    :param stylesheet str: The stylesheet to add to the begin of the file
+    """
+    if not os.path.exists(os.path.dirname(filename)):
+        os.makedirs(os.path.dirname(filename))
+
+    f = open(filename, "w")
+    f.write("<?xml version='1.0' encoding='utf-8'?>\n")
+    if len(stylesheet) > 0:
+        f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % stylesheet)
+    f.write(etree.tostring(xmlroot, encoding='utf-8'))
+    f.close()   
+    
\ No newline at end of file