Salome HOME
jobs report, display error when jobs are missing somewhere in the week
[tools/sat.git] / src / ElementTree.py
index 8714da7ef2dad2de2ebad95c4efe04be3462a683..37ed4674ea05898ebb585af555396b25c8e0b8d9 100644 (file)
@@ -106,7 +106,7 @@ __all__ = [
 # structure, and convert it from and to XML.
 ##
 
-import string, sys, re
+import string, sys, re, platform
 
 class _SimpleElementPath:
     # emulate pre-1.2 find/findtext/findall behaviour
@@ -324,6 +324,8 @@ class _ElementInterface:
     # @defreturn Element or None
 
     def find(self, path):
+        if ElementPath.find(self, path) == None:
+            return ElementPath.find(self, path.encode())
         return ElementPath.find(self, path)
 
     ##
@@ -370,7 +372,13 @@ class _ElementInterface:
     # @defreturn string or None
 
     def get(self, key, default=None):
-        return self.attrib.get(key, default)
+        res = self.attrib.get(key, default)
+        if not res:
+            res = self.attrib.get(key.encode(), default)
+        if isinstance(res, bytes):
+            return res.decode()
+        else:
+            return res
 
     ##
     # Sets an element attribute.
@@ -389,8 +397,14 @@ class _ElementInterface:
     # @defreturn list of strings
 
     def keys(self):
-        return self.attrib.keys()
-
+        res = []
+        for key in self.attrib.keys():
+            if isinstance(key, bytes):
+                res.append(key.decode())
+            else:
+                res.append(key)
+        return res
+                
     ##
     # Gets element attributes, as a sequence.  The attributes are
     # returned in an arbitrary order.
@@ -798,6 +812,13 @@ def _encode_entity(text, pattern=_escape):
 def _escape_cdata(text, encoding=None, replace=str.replace):
     # escape character data
     try:
+        if platform.python_version()[0] == '2': # python 2.x.y
+            if encoding:
+                try:
+                    text = _encode(text, encoding)
+                except UnicodeError:
+                    return _encode_entity(text)
+            
         text = replace(text, "&", "&")
         text = replace(text, "<", "&lt;")
         text = replace(text, ">", "&gt;")
@@ -1050,7 +1071,12 @@ class TreeBuilder:
     def _flush(self):
         if self._data:
             if self._last is not None:
-                text = string.join(self._data, "")
+                text = ""
+                for item in self._data:
+                    try:
+                        text += item
+                    except:
+                        text += item.decode()
                 if self._tail:
                     assert self._last.tail is None, "internal error (tail)"
                     self._last.tail = text
@@ -1143,9 +1169,9 @@ class XMLTreeBuilder:
             parser.StartElementHandler = self._start_list
         except AttributeError:
             pass
-        encoding = None
-        if not parser.returns_unicode:
-            encoding = "utf-8"
+        #encoding = None
+        #if not parser.returns_unicode:
+        #    encoding = "utf-8"
         # target.xml(encoding, None)
         self._doctype = None
         self.entity = {}