X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=Noyau%2Fstrfunc.py;h=3339a852fa7f3d973783e4b5747753cb79862707;hb=9f4ff7ed9afd87c96e11d61e442e39e5511f60d1;hp=9127b8c53c630446ab380a6fa50815e17bf9c889;hpb=221da48db3beb709c3674e1f455690faa9cf0195;p=tools%2Feficas.git diff --git a/Noyau/strfunc.py b/Noyau/strfunc.py index 9127b8c5..3339a852 100644 --- a/Noyau/strfunc.py +++ b/Noyau/strfunc.py @@ -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,54 +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: - # print type(uformat), uformat - # print type(arguments), arguments - #raise - except : - pass - 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