3 # Copyright (C) 2010-2013 CEA/DEN
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 from . import ElementTree as etree
24 class XmlLogFile(object):
25 '''Class to manage writing in salomeTools xml log file
27 def __init__(self, filePath, rootname, attrib = {}):
30 :param filePath str: The path to the file where to write the log file
31 :param rootname str: The name of the root node of the xml file
32 :param attrib dict: the dictionary that contains the attributes and value of the root node
34 # Initialize the filePath and ensure that the directory that contain the file exists (make it if necessary)
35 self.logFile = filePath
36 src.ensure_path_exists(os.path.dirname(filePath))
37 # Initialize the field that contain the xml in memory
38 self.xmlroot = etree.Element(rootname, attrib = attrib)
40 def write_tree(self, stylesheet=None):
41 '''Write the xml tree in the log file path. Add the stylesheet if asked.
43 :param stylesheet str: The stylesheet to apply to the xml file
45 f = open(self.logFile, 'w')
46 f.write("<?xml version='1.0' encoding='utf-8'?>\n")
48 f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % stylesheet)
49 f.write(etree.tostring(self.xmlroot, encoding='utf-8'))
52 def add_simple_node(self, node_name, text=None, attrib={}):
53 '''Add a node with some attibutes and text to the root node.
55 :param node_name str: the name of the node to add
56 :param text str: the text of the node
57 :param attrib dict: the dictionary containing the attribute of the new node
59 n = etree.Element(node_name, attrib=attrib)
61 self.xmlroot.append(n)
64 def append_node_text(self, node_name, text):
65 '''Append a new text to the node that has node_name as name
67 :param node_name str: The name of the node on which append text
68 :param text str: The text to append
70 # find the corresponding node
71 for field in self.xmlroot:
72 if field.tag == node_name:
76 def append_node_attrib(self, node_name, attrib):
77 '''Append a new attributes to the node that has node_name as name
79 :param node_name str: The name of the node on which append text
80 :param attrib dixt: The attrib to append
82 self.xmlroot.find(node_name).attrib.update(attrib)
84 class readXmlFile(object):
85 '''Class to manage reading of an xml log file
87 def __init__(self, filePath):
90 :param filePath str: The xml file to be read
92 self.filePath = filePath
93 etree_inst = etree.parse(filePath)
94 self.xmlroot = etree_inst.parse(filePath)
96 def getRootAttrib(self):
97 '''Get the attibutes of the self.xmlroot
99 :return: The attributes of the root node
102 return self.xmlroot.attrib
104 def get_attrib(self, node_name):
105 '''Get the attibutes of the node node_name in self.xmlroot
107 :param node_name str: the name of the node
108 :return: the attibutes of the node node_name in self.xmlroot
111 attrib = self.xmlroot.find(node_name).attrib
112 # To be python 3 compatible, convert bytes to str if there are any
114 for k in attrib.keys():
115 if isinstance(k, bytes):
119 if isinstance(attrib[k], bytes):
120 value = attrib[k].decode()
123 fixedAttrib[key] = value
126 def get_node_text(self, node):
127 '''Get the text of the first node that has name that corresponds to the parameter node
129 :param node str: the name of the node from which get the text
130 :return: the text of the first node that has name that corresponds to the parameter node
133 return self.xmlroot.find(node).text