X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FxmlManager.py;h=4b4fa61cfc08152cc98c29eb82b9f17756859910;hb=a9ed5d52d2534d2f4d1e741656a8854a2601c30a;hp=c125c5124999487eb06cf2eba62fb16a012c9c7b;hpb=41b996848dfb04c3394902459518be9114e20bb6;p=tools%2Fsat.git diff --git a/src/xmlManager.py b/src/xmlManager.py index c125c51..4b4fa61 100644 --- a/src/xmlManager.py +++ b/src/xmlManager.py @@ -17,6 +17,8 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os +import shutil + try: # For python2 import sys reload(sys) @@ -25,7 +27,9 @@ except: pass import src -from . import ElementTree as etree +import src.ElementTree as etree + +verbose = False class XmlLogFile(object): '''Class to manage writing in salomeTools xml log file @@ -44,7 +48,7 @@ class XmlLogFile(object): src.ensure_path_exists(os.path.dirname(filePath)) # Initialize the field that contain the xml in memory self.xmlroot = etree.Element(rootname, attrib = attrib) - + def write_tree(self, stylesheet=None, file_path = None): '''Write the xml tree in the log file path. Add the stylesheet if asked. @@ -52,14 +56,17 @@ class XmlLogFile(object): ''' 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" % - stylesheet) - f.write(etree.tostring(self.xmlroot, encoding='utf-8')) - f.close() + log_file_path = file_path + try: + with open(log_file_path, 'w') as f: + f.write("\n") + if stylesheet: + f.write("\n" % stylesheet) + pass + res= etree.tostring(self.xmlroot, encoding='utf-8') + f.write(res) + except IOError: + pass def add_simple_node(self, node_name, text=None, attrib={}): '''Add a node with some attibutes and text to the root node. @@ -146,7 +153,7 @@ 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. @@ -170,6 +177,26 @@ def append_node_attrib(root_node, attrib): ''' 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. @@ -177,12 +204,61 @@ def write_report(filename, xmlroot, stylesheet): :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") + dirname = os.path.dirname(filename) + if not os.path.exists(dirname): + os.makedirs(dirname) if len(stylesheet) > 0: - f.write("\n" % stylesheet) - f.write(etree.tostring(xmlroot, encoding='utf-8')) - f.close() \ No newline at end of file + styleName = stylesheet + else: + styleName = None + + with open(filename, "w") as f: + f.write("\n") + if styleName is not None: + f.write("\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 + +