X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FxmlManager.py;h=c125c5124999487eb06cf2eba62fb16a012c9c7b;hb=78cadadada66b6e6c2e5a5c2dc9f307fff33f2ab;hp=fa175ff1c94518a73f81621397e3308c3558b9f6;hpb=0917d86acde72055f25aed5cee20faa69654e76a;p=tools%2Fsat.git diff --git a/src/xmlManager.py b/src/xmlManager.py index fa175ff..c125c51 100644 --- a/src/xmlManager.py +++ b/src/xmlManager.py @@ -17,6 +17,12 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os +try: # For python2 + import sys + reload(sys) + sys.setdefaultencoding('utf8') +except: + pass import src from . import ElementTree as etree @@ -39,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("\n") if stylesheet: f.write("\n" % @@ -137,3 +146,43 @@ 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. + + :param root_node etree.Element: the Etree element where to add the new node + :param node_name str: the name of the node to add + :param text str: the text of the node + :param attrib dict: the dictionary containing the + attribute of the new node + ''' + n = etree.Element(node_name, attrib=attrib) + n.text = text + root_node.append(n) + return n + +def append_node_attrib(root_node, attrib): + '''Append a new attributes to the node that has node_name as name + + :param root_node etree.Element: the Etree element + where to append the new attibutes + :param attrib dixt: The attrib to append + ''' + root_node.attrib.update(attrib) + +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("\n") + if len(stylesheet) > 0: + f.write("\n" % stylesheet) + f.write(etree.tostring(xmlroot, encoding='utf-8')) + f.close() \ No newline at end of file