2 # Copyright (C) 2007-2017 EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 """Module rassemblant des fonctions utilitaires de manipulations
21 de chaines de caractères
23 # module identique à Execution/strfunc.py pour usage dans Eficas
25 from __future__ import absolute_import
27 from builtins import str
37 """Return local encoding
42 _encoding = locale.getpreferredencoding() or 'ascii'
48 def toUnicode(string):
49 """Try to convert string into a unicode string."""
50 if type(string) is six.text_type:
52 elif type(string) is dict:
54 for k, v in list(string.items()):
57 elif type(string) is list:
58 return [toUnicode(elt) for elt in string]
59 elif type(string) is tuple:
60 return tuple(toUnicode(list(string)))
61 elif type(string) is not str:
63 assert type(string) is str, u"unsupported object: %s" % string
64 for encoding in ('utf-8', 'iso-8859-15', 'cp1252'):
66 s = six.text_type(string, encoding)
68 except UnicodeDecodeError:
70 return six.text_type(string, 'utf-8', 'replace')
73 #def fromUnicode(ustring, encoding, errors='replace'):
74 # """Try to encode a unicode string using encoding."""
76 # return ustring.encode(encoding)
77 # except UnicodeError:
79 # return ustring.encode(encoding, errors)
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 = toUnicode(content)
88 # return fromUnicode(content, encoding or getEncoding(), errors)
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 = toUnicode(uformat)
95 # if len(args) == 1 and type(args[0]) is dict:
96 # arguments = toUnicode(args[0])
100 # if type(arg) in (str, six.text_type, list, tuple, dict):
101 # nargs.append(toUnicode(arg))
102 # elif type(arg) not in (int, int, float):
103 # nargs.append(toUnicode(str(arg)))
106 # arguments = tuple(nargs)
107 # formatted_string=""
109 # formatted_string = uformat % arguments
110 # #except UnicodeDecodeError:
111 # # print type(uformat), uformat
112 # # print type(arguments), arguments
116 # return formatted_string