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
25 sys.setdefaultencoding('utf8')
30 from . import ElementTree as etree
34 class XmlLogFile(object):
35 '''Class to manage writing in salomeTools xml log file
37 def __init__(self, filePath, rootname, attrib = {}):
40 :param filePath str: The path to the file where to write the log file
41 :param rootname str: The name of the root node of the xml file
42 :param attrib dict: the dictionary that contains the attributes
43 and value of the root node
45 # Initialize the filePath and ensure that the directory
46 # that contain the file exists (make it if necessary)
47 self.logFile = filePath
48 src.ensure_path_exists(os.path.dirname(filePath))
49 # Initialize the field that contain the xml in memory
50 self.xmlroot = etree.Element(rootname, attrib = attrib)
52 def write_tree(self, stylesheet=None, file_path = None):
53 '''Write the xml tree in the log file path. Add the stylesheet if asked.
55 :param stylesheet str: The stylesheet to apply to the xml file
57 log_file_path = self.logFile
59 log_file_path = file_path
61 f = open(log_file_path, 'w')
62 f.write("<?xml version='1.0' encoding='utf-8'?>\n")
64 f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % stylesheet)
65 f.write(etree.tostring(self.xmlroot, encoding='utf-8'))
70 def add_simple_node(self, node_name, text=None, attrib={}):
71 '''Add a node with some attibutes and text to the root node.
73 :param node_name str: the name of the node to add
74 :param text str: the text of the node
75 :param attrib dict: the dictionary containing the
76 attribute of the new node
78 n = etree.Element(node_name, attrib=attrib)
80 self.xmlroot.append(n)
83 def append_node_text(self, node_name, text):
84 '''Append a new text to the node that has node_name as name
86 :param node_name str: The name of the node on which append text
87 :param text str: The text to append
89 # find the corresponding node
90 for field in self.xmlroot:
91 if field.tag == node_name:
95 def append_node_attrib(self, node_name, attrib):
96 '''Append a new attributes to the node that has node_name as name
98 :param node_name str: The name of the node on which append text
99 :param attrib dixt: The attrib to append
101 self.xmlroot.find(node_name).attrib.update(attrib)
103 class ReadXmlFile(object):
104 '''Class to manage reading of an xml log file
106 def __init__(self, filePath):
109 :param filePath str: The xml file to be read
111 self.filePath = filePath
112 etree_inst = etree.parse(filePath)
113 self.xmlroot = etree_inst.parse(filePath)
115 def getRootAttrib(self):
116 '''Get the attibutes of the self.xmlroot
118 :return: The attributes of the root node
121 return self.xmlroot.attrib
123 def get_attrib(self, node_name):
124 '''Get the attibutes of the node node_name in self.xmlroot
126 :param node_name str: the name of the node
127 :return: the attibutes of the node node_name in self.xmlroot
130 attrib = self.xmlroot.find(node_name).attrib
131 # To be python 3 compatible, convert bytes to str if there are any
133 for k in attrib.keys():
134 if isinstance(k, bytes):
138 if isinstance(attrib[k], bytes):
139 value = attrib[k].decode()
142 fixedAttrib[key] = value
145 def get_node_text(self, node):
146 '''Get the text of the first node that has name
147 that corresponds to the parameter node
149 :param node str: the name of the node from which get the text
150 :return: the text of the first node that has name
151 that corresponds to the parameter node
154 return self.xmlroot.find(node).text
156 def add_simple_node(root_node, node_name, text=None, attrib={}):
157 '''Add a node with some attibutes and text to the root node.
159 :param root_node etree.Element: the Etree element where to add the new node
160 :param node_name str: the name of the node to add
161 :param text str: the text of the node
162 :param attrib dict: the dictionary containing the
163 attribute of the new node
165 n = etree.Element(node_name, attrib=attrib)
170 def append_node_attrib(root_node, attrib):
171 '''Append a new attributes to the node that has node_name as name
173 :param root_node etree.Element: the Etree element
174 where to append the new attibutes
175 :param attrib dixt: The attrib to append
177 root_node.attrib.update(attrib)
179 def find_node_by_attrib(xmlroot, name_node, key, value):
180 '''Find the nfirst ode from xmlroot that has name name_node and that has in
181 its attributes {key : value}. Return the node
183 :param xmlroot etree.Element: the Etree element where to search
184 :param name_node str: the name of node to search
185 :param key str: the key to search
186 :param value str: the value to search
187 :return: the found node
188 :rtype: xmlroot etree.Element
190 l_nodes = xmlroot.findall(name_node)
192 if key not in node.attrib.keys():
194 if node.attrib[key] == value:
199 def write_report(filename, xmlroot, stylesheet):
200 """Writes a report file from a XML tree.
202 :param filename str: The path to the file to create
203 :param xmlroot etree.Element: the Etree element to write to the file
204 :param stylesheet str: The stylesheet to add to the begin of the file
206 dirname = os.path.dirname(filename)
207 if not os.path.exists(dirname):
209 if len(stylesheet) > 0:
210 styleName = stylesheet
214 with open(filename, "w") as f:
215 f.write("<?xml version='1.0' encoding='utf-8'?>\n")
216 if styleName is not None:
217 f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % styleName)
218 f.write(etree.tostring(xmlroot, encoding='utf-8'))
220 # create fileStyle in dirname if not existing
221 if styleName is not None:
222 styleFile = os.path.join(dirname, styleName)
223 if not os.path.exists(styleFile):
224 # copy if from "salomeTools/src/xsl"
225 srcdir = os.path.dirname(src.__file__)
226 srcFile = os.path.join(srcdir, "xsl", styleName)
227 if verbose: print("write_report %s style %s" % (srcFile, styleFile))
228 shutil.copy(srcFile, dirname)
230 def escapeSequence(aStr):
232 See xml specification:
233 The ampersand character(&) and the left angle bracket(<) MUST NOT appear in their
234 literal form, except when used as markup delimiters, or within a comment, a processing
235 instruction, or a CDATA section.
236 If they are needed elsewhere, they MUST be escaped using either numeric character references
237 or the strings '&' and '<' respectively.
238 The right angle bracket(>) may be
239 represented using the string '>', and MUST,
240 for compatibility, be escaped using either '>' or a character reference
241 when it appears in the string " ]]> " in content,
242 when that string is not marking the end of a CDATA section.
243 You can use these escape sequences:
244 < (less - than) as < or <
245 > (greater - than) as > or >
246 & (ampersand) as &
247 ' (apostrophe or single quote) as '
248 " (double-quote) as "
250 replaces = [ ('&', '&'),
257 for ini, fin in replaces: # order matters
258 res = res.replace(ini, fin)