[IMP] translations: parse views iteratively instead of recursively
[odoo/odoo.git] / openerp / loglevels.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import sys
23
24 LOG_NOTSET = 'notset'
25 LOG_DEBUG = 'debug'
26 LOG_INFO = 'info'
27 LOG_WARNING = 'warn'
28 LOG_ERROR = 'error'
29 LOG_CRITICAL = 'critical'
30
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.
33
34 def get_encodings(hint_encoding='utf-8'):
35     fallbacks = {
36         'latin1': 'latin9',
37         'iso-8859-1': 'iso8859-15',
38         'cp1252': '1252',
39     }
40     if hint_encoding:
41         yield hint_encoding
42         if hint_encoding.lower() in fallbacks:
43             yield fallbacks[hint_encoding.lower()]
44
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()):
48             yield charset
49
50     from locale import getpreferredencoding
51     prefenc = getpreferredencoding()
52     if prefenc and prefenc.lower() != 'utf-8':
53         yield prefenc
54         prefenc = fallbacks.get(prefenc.lower())
55         if prefenc:
56             yield prefenc
57
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.
62
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
75     """
76     if isinstance(value, Exception):
77         return exception_to_unicode(value)
78
79     if isinstance(value, unicode):
80         return value
81
82     if not isinstance(value, basestring):
83         try:
84             return unicode(value)
85         except Exception:
86             raise UnicodeError('unable to convert %r' % (value,))
87
88     for ln in get_encodings(hint_encoding):
89         try:
90             return unicode(value, ln, errors=errors)
91         except Exception:
92             pass
93     raise UnicodeError('unable to convert %r' % (value,))
94
95
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))
101     try:
102         return unicode(e)
103     except Exception:
104         return u"Unknown message"
105
106 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: