]> SALOME platform Git repositories - tools/eficas.git/blob - Noyau/strfunc.py
Salome HOME
3339a852fa7f3d973783e4b5747753cb79862707
[tools/eficas.git] / Noyau / strfunc.py
1 # coding=utf-8
2 # Copyright (C) 2007-2013   EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19
20 """Module rassemblant des fonctions utilitaires de manipulations
21 de chaines de caractères
22 """
23 # module identique à Execution/strfunc.py pour usage dans Eficas
24
25 from __future__ import absolute_import
26 try :
27    from builtins import str
28 except : pass
29
30 import locale
31 import six
32
33 _encoding = None
34
35
36 def get_encoding():
37     """Return local encoding
38     """
39     global _encoding
40     if _encoding is None:
41         try:
42             _encoding = locale.getpreferredencoding() or 'ascii'
43         except locale.Error:
44             _encoding = 'ascii'
45     return _encoding
46
47
48 def to_unicode(string):
49     """Try to convert string into a unicode string."""
50     if type(string) is six.text_type:
51         return string
52     elif type(string) is dict:
53         new = {}
54         for k, v in list(string.items()):
55             new[k] = to_unicode(v)
56         return new
57     elif type(string) is list:
58         return [to_unicode(elt) for elt in string]
59     elif type(string) is tuple:
60         return tuple(to_unicode(list(string)))
61     elif type(string) is not str:
62         return string
63     assert type(string) is str, u"unsupported object: %s" % string
64     for encoding in ('utf-8', 'iso-8859-15', 'cp1252'):
65         try:
66             s = six.text_type(string, encoding)
67             return s
68         except UnicodeDecodeError:
69             pass
70     return six.text_type(string, 'utf-8', 'replace')
71
72
73 #def from_unicode(ustring, encoding, errors='replace'):
74 #    """Try to encode a unicode string using encoding."""
75 #    try:
76 #        return ustring.encode(encoding)
77 #    except UnicodeError:
78 #        pass
79 #    return ustring.encode(encoding, errors)
80 #
81 #
82 #def convert(content, encoding=None, errors='replace'):
83 #    """Convert content using encoding or default encoding if None."""
84 #    if type(content) not in (str, six.text_type):
85 #        content = six.text_type(content)
86 #    if type(content) == str:
87 #        content = to_unicode(content)
88 #    return from_unicode(content, encoding or get_encoding(), errors)
89 #
90 #
91 #def ufmt(uformat, *args):
92 #    """Helper function to format a string by converting all its arguments to unicode"""
93 #    if type(uformat) is not six.text_type:
94 #        uformat = to_unicode(uformat)
95 #    if len(args) == 1 and type(args[0]) is dict:
96 #        arguments = to_unicode(args[0])
97 #    else:
98 #        nargs = []
99 #        for arg in args:
100 #            if type(arg) in (str, six.text_type, list, tuple, dict):
101 #                nargs.append(to_unicode(arg))
102 #            elif type(arg) not in (int, int, float):
103 #                nargs.append(to_unicode(str(arg)))
104 #            else:
105 #                nargs.append(arg)
106 #        arguments = tuple(nargs)
107 #    formatted_string=""
108 #    try:
109 #        formatted_string = uformat % arguments
110 #    #except UnicodeDecodeError:
111 #    #    print type(uformat), uformat
112 #    #    print type(arguments), arguments
113 #        #raise
114 #    except :
115 #        pass
116 #    return formatted_string