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
33 and value of the root node
35 # Initialize the filePath and ensure that the directory
36 # that contain the file exists (make it if necessary)
37 self.logFile = filePath
38 src.ensure_path_exists(os.path.dirname(filePath))
39 # Initialize the field that contain the xml in memory
40 self.xmlroot = etree.Element(rootname, attrib = attrib)
42 def write_tree(self, stylesheet=None):
43 '''Write the xml tree in the log file path. Add the stylesheet if asked.
45 :param stylesheet str: The stylesheet to apply to the xml file
47 f = open(self.logFile, 'w')
48 f.write("<?xml version='1.0' encoding='utf-8'?>\n")
50 f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" %
52 f.write(etree.tostring(self.xmlroot, encoding='utf-8'))
55 def add_simple_node(self, node_name, text=None, attrib={}):
56 '''Add a node with some attibutes and text to the root node.
58 :param node_name str: the name of the node to add
59 :param text str: the text of the node
60 :param attrib dict: the dictionary containing the
61 attribute of the new node
63 n = etree.Element(node_name, attrib=attrib)
65 self.xmlroot.append(n)
68 def append_node_text(self, node_name, text):
69 '''Append a new text to the node that has node_name as name
71 :param node_name str: The name of the node on which append text
72 :param text str: The text to append
74 # find the corresponding node
75 for field in self.xmlroot:
76 if field.tag == node_name:
80 def append_node_attrib(self, node_name, attrib):
81 '''Append a new attributes to the node that has node_name as name
83 :param node_name str: The name of the node on which append text
84 :param attrib dixt: The attrib to append
86 self.xmlroot.find(node_name).attrib.update(attrib)
88 class ReadXmlFile(object):
89 '''Class to manage reading of an xml log file
91 def __init__(self, filePath):
94 :param filePath str: The xml file to be read
96 self.filePath = filePath
97 etree_inst = etree.parse(filePath)
98 self.xmlroot = etree_inst.parse(filePath)
100 def getRootAttrib(self):
101 '''Get the attibutes of the self.xmlroot
103 :return: The attributes of the root node
106 return self.xmlroot.attrib
108 def get_attrib(self, node_name):
109 '''Get the attibutes of the node node_name in self.xmlroot
111 :param node_name str: the name of the node
112 :return: the attibutes of the node node_name in self.xmlroot
115 attrib = self.xmlroot.find(node_name).attrib
116 # To be python 3 compatible, convert bytes to str if there are any
118 for k in attrib.keys():
119 if isinstance(k, bytes):
123 if isinstance(attrib[k], bytes):
124 value = attrib[k].decode()
127 fixedAttrib[key] = value
130 def get_node_text(self, node):
131 '''Get the text of the first node that has name
132 that corresponds to the parameter node
134 :param node str: the name of the node from which get the text
135 :return: the text of the first node that has name
136 that corresponds to the parameter node
139 return self.xmlroot.find(node).text