Salome HOME
sat #29570 : set SAT_prodname__IS_NATIVE variables at build time to help compilations...
[tools/sat.git] / src / xmlManager.py
index d619980da281368a9bf182596bca6b5517c17dee..4b4fa61cfc08152cc98c29eb82b9f17756859910 100644 (file)
@@ -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
@@ -45,54 +49,22 @@ class XmlLogFile(object):
         # Initialize the field that contain the xml in memory
         self.xmlroot = etree.Element(rootname, attrib = attrib)
 
-    def escapeSequence(self, 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 '&amp;' and '&lt;' respectively.
-        The right angle bracket(>) may be
-        represented using the string '&gt;', and MUST,
-        for compatibility, be escaped using either '&gt;' 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 &#60; or &lt;
-        > (greater - than) as &#62; or &gt;
-        & (ampersand) as &#38;
-        ' (apostrophe or single quote) as &#39;
-        " (double-quote) as &#34;
-        """
-        replaces = [ ('&', '&amp;'),
-                     ('>', '&gt;'),
-                     ('<', '&lt;'),
-                     ("'", '&#39;'),
-                     ('"', '&#34;'),
-                    ]
-        res = aStr
-        for ini, fin in replaces: # order matters
-          res = res.replace(ini, fin)
-        return res
-
-
-def write_tree(self, stylesheet=None, file_path = None):
+    def write_tree(self, stylesheet=None, file_path = None):
         '''Write the xml tree in the log file path. Add the stylesheet if asked.
         
         :param stylesheet str: The stylesheet to apply to the xml file
         '''
         log_file_path = self.logFile
         if file_path:
-            log_file_path = file_path
+          log_file_path = file_path
         try:
-            f = open(log_file_path, 'w')
+          with open(log_file_path, 'w') as f:
             f.write("<?xml version='1.0' encoding='utf-8'?>\n")
             if stylesheet:
-                f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" % 
-                        stylesheet)    
-            f.write(etree.tostring(self.xmlroot, encoding='utf-8'))
-            f.close()
+                f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\n" %  stylesheet)
+                pass
+            res= etree.tostring(self.xmlroot, encoding='utf-8')
+            f.write(res)
         except IOError:
             pass  
         
@@ -232,13 +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("<?xml version='1.0' encoding='utf-8'?>\n")
+    dirname = os.path.dirname(filename)
+    if not os.path.exists(dirname):
+      os.makedirs(dirname)
     if len(stylesheet) > 0:
-        f.write("<?xml-stylesheet type='text/xsl' href='%s'?>\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("<?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 '&amp;' and '&lt;' respectively.
+    The right angle bracket(>) may be
+    represented using the string '&gt;', and MUST,
+    for compatibility, be escaped using either '&gt;' 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 &#60; or &lt;
+    > (greater - than) as &#62; or &gt;
+    & (ampersand) as &#38;
+    ' (apostrophe or single quote) as &#39;
+    " (double-quote) as &#34;
+    """
+    replaces = [ ('&', '&amp;'),
+                 ('>', '&gt;'),
+                 ('<', '&lt;'),
+                 ("'", '&#39;'),
+                 ('"', '&#34;'),
+                ]
+    res = aStr
+    for ini, fin in replaces: # order matters
+      res = res.replace(ini, fin)
+    return res
+
+