1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
29 LOG_CRITICAL = 'critical'
31 # TODO get_encodings, ustr and exception_to_unicode were originally from tools.misc.
32 # There are here until we refactor tools so that this module doesn't depends on tools.
34 def get_encodings(hint_encoding='utf-8'):
37 'iso-8859-1': 'iso8859-15',
42 if hint_encoding.lower() in fallbacks:
43 yield fallbacks[hint_encoding.lower()]
45 # some defaults (also taking care of pure ASCII)
46 for charset in ['utf8','latin1']:
47 if not hint_encoding or (charset.lower() != hint_encoding.lower()):
50 from locale import getpreferredencoding
51 prefenc = getpreferredencoding()
52 if prefenc and prefenc.lower() != 'utf-8':
54 prefenc = fallbacks.get(prefenc.lower())
58 def ustr(value, hint_encoding='utf-8', errors='strict'):
59 """This method is similar to the builtin `unicode`, except
60 that it may try multiple encodings to find one that works
61 for decoding `value`, and defaults to 'utf-8' first.
63 :param: value: the value to convert
64 :param: hint_encoding: an optional encoding that was detecte
65 upstream and should be tried first to decode ``value``.
66 :param str errors: optional `errors` flag to pass to the unicode
67 built-in to indicate how illegal character values should be
68 treated when converting a string: 'strict', 'ignore' or 'replace'
69 (see ``unicode()`` constructor).
70 Passing anything other than 'strict' means that the first
71 encoding tried will be used, even if it's not the correct
72 one to use, so be careful! Ignored if value is not a string/unicode.
73 :raise: UnicodeError if value cannot be coerced to unicode
74 :return: unicode string representing the given value
76 if isinstance(value, Exception):
77 return exception_to_unicode(value)
79 if isinstance(value, unicode):
82 if not isinstance(value, basestring):
86 raise UnicodeError('unable to convert %r' % (value,))
88 for ln in get_encodings(hint_encoding):
90 return unicode(value, ln, errors=errors)
93 raise UnicodeError('unable to convert %r' % (value,))
96 def exception_to_unicode(e):
97 if (sys.version_info[:2] < (2,6)) and hasattr(e, 'message'):
98 return ustr(e.message)
99 if hasattr(e, 'args'):
100 return "\n".join((ustr(a) for a in e.args))
104 return u"Unknown message"
106 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: