Salome HOME
commentaire
[tools/eficas.git] / Noyau / strfunc.py
index 3e9089a054fb0c8a96135dd7521d82534c98cff7..3339a852fa7f3d973783e4b5747753cb79862707 100644 (file)
@@ -22,7 +22,13 @@ de chaines de caractères
 """
 # module identique à Execution/strfunc.py pour usage dans Eficas
 
+from __future__ import absolute_import
+try :
+   from builtins import str
+except : pass
+
 import locale
+import six
 
 _encoding = None
 
@@ -41,11 +47,11 @@ def get_encoding():
 
 def to_unicode(string):
     """Try to convert string into a unicode string."""
-    if type(string) is unicode:
+    if type(string) is six.text_type:
         return string
     elif type(string) is dict:
         new = {}
-        for k, v in string.items():
+        for k, v in list(string.items()):
             new[k] = to_unicode(v)
         return new
     elif type(string) is list:
@@ -57,52 +63,54 @@ def to_unicode(string):
     assert type(string) is str, u"unsupported object: %s" % string
     for encoding in ('utf-8', 'iso-8859-15', 'cp1252'):
         try:
-            s = unicode(string, encoding)
+            s = six.text_type(string, encoding)
             return s
         except UnicodeDecodeError:
             pass
-    return unicode(string, 'utf-8', 'replace')
-
-
-def from_unicode(ustring, encoding, errors='replace'):
-    """Try to encode a unicode string using encoding."""
-    try:
-        return ustring.encode(encoding)
-    except UnicodeError:
-        pass
-    return ustring.encode(encoding, errors)
-
-
-def convert(content, encoding=None, errors='replace'):
-    """Convert content using encoding or default encoding if None."""
-    if type(content) not in (str, unicode):
-        content = unicode(content)
-    if type(content) == str:
-        content = to_unicode(content)
-    return from_unicode(content, encoding or get_encoding(), errors)
+    return six.text_type(string, 'utf-8', 'replace')
 
 
-def ufmt(uformat, *args):
-    """Helper function to format a string by converting all its arguments to unicode"""
-    if type(uformat) is not unicode:
-        uformat = to_unicode(uformat)
-    if len(args) == 1 and type(args[0]) is dict:
-        arguments = to_unicode(args[0])
-    else:
-        nargs = []
-        for arg in args:
-            if type(arg) in (str, unicode, list, tuple, dict):
-                nargs.append(to_unicode(arg))
-            elif type(arg) not in (int, long, float):
-                nargs.append(to_unicode(str(arg)))
-            else:
-                nargs.append(arg)
-        arguments = tuple(nargs)
-    formatted_string=""
-    try:
-        formatted_string = uformat % arguments
-    except UnicodeDecodeError, err:
-        print type(uformat), uformat
-        print type(arguments), arguments
-        raise
-    return formatted_string
+#def from_unicode(ustring, encoding, errors='replace'):
+#    """Try to encode a unicode string using encoding."""
+#    try:
+#        return ustring.encode(encoding)
+#    except UnicodeError:
+#        pass
+#    return ustring.encode(encoding, errors)
+#
+#
+#def convert(content, encoding=None, errors='replace'):
+#    """Convert content using encoding or default encoding if None."""
+#    if type(content) not in (str, six.text_type):
+#        content = six.text_type(content)
+#    if type(content) == str:
+#        content = to_unicode(content)
+#    return from_unicode(content, encoding or get_encoding(), errors)
+#
+#
+#def ufmt(uformat, *args):
+#    """Helper function to format a string by converting all its arguments to unicode"""
+#    if type(uformat) is not six.text_type:
+#        uformat = to_unicode(uformat)
+#    if len(args) == 1 and type(args[0]) is dict:
+#        arguments = to_unicode(args[0])
+#    else:
+#        nargs = []
+#        for arg in args:
+#            if type(arg) in (str, six.text_type, list, tuple, dict):
+#                nargs.append(to_unicode(arg))
+#            elif type(arg) not in (int, int, float):
+#                nargs.append(to_unicode(str(arg)))
+#            else:
+#                nargs.append(arg)
+#        arguments = tuple(nargs)
+#    formatted_string=""
+#    try:
+#        formatted_string = uformat % arguments
+#    #except UnicodeDecodeError:
+#    #    print type(uformat), uformat
+#    #    print type(arguments), arguments
+#        #raise
+#    except :
+#        pass
+#    return formatted_string