+
+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 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
+ """
+ dirname = os.path.dirname(filename)
+ if not os.path.exists(dirname):
+ os.makedirs(dirname)
+ if len(stylesheet) > 0:
+ styleName = stylesheet
+ else:
+ styleName = None
+
+ with open(filename, "w") as f:
+ f.write("<?xml version='1.0' encoding='utf-8'?>\n")
+ if styleName is not None:
+ f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % styleName)
+ res = etree.tostring(xmlroot, encoding='utf-8')
+ # print("********** etree.tostring %s" % res)
+ f.write(res)
+
+ # create fileStyle in dirname if not existing
+ if styleName is not None:
+ styleFile = os.path.join(dirname, styleName)
+ if not os.path.exists(styleFile):
+ # copy if from "salomeTools/src/xsl"
+ srcdir = os.path.dirname(src.__file__)
+ srcFile = os.path.join(srcdir, "xsl", styleName)
+ if verbose: print("write_report %s style %s" % (srcFile, styleFile))
+ shutil.copy(srcFile, dirname)
+
+def escapeSequence(aStr):
+ """
+ See xml specification:
+ The ampersand character(&) and the left angle bracket(<) MUST NOT appear in their
+ literal form, except when used as markup delimiters, or within a comment, a processing
+ instruction, or a CDATA section.
+ If they are needed elsewhere, they MUST be escaped using either numeric character references
+ or the strings '&' and '<' respectively.
+ The right angle bracket(>) may be
+ represented using the string '>', and MUST,
+ for compatibility, be escaped using either '>' or a character reference
+ when it appears in the string " ]]> " in content,
+ when that string is not marking the end of a CDATA section.
+ You can use these escape sequences:
+ < (less - than) as < or <
+ > (greater - than) as > or >
+ & (ampersand) as &
+ ' (apostrophe or single quote) as '
+ " (double-quote) as "
+ """
+ replaces = [ ('&', '&'),
+ ('>', '>'),
+ ('<', '<'),
+ ("'", '''),
+ ('"', '"'),
+ ]
+ res = aStr
+ for ini, fin in replaces: # order matters
+ res = res.replace(ini, fin)
+ return res
+
+