utils: relax conversion to unicode
[imapami.git] / imapami / utils.py
1 #!/usr/bin/env python
2
3 #
4 # Copyright 2015, Olivier MATZ <zer0@droids-corp.org>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 #     * Redistributions of source code must retain the above copyright
10 #       notice, this list of conditions and the following disclaimer.
11 #     * Redistributions in binary form must reproduce the above copyright
12 #       notice, this list of conditions and the following disclaimer in the
13 #       documentation and/or other materials provided with the distribution.
14 #     * Neither the name of the University of California, Berkeley nor the
15 #       names of its contributors may be used to endorse or promote products
16 #       derived from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
19 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #
29
30 import email.header
31 import re
32
33 # pylint: disable=deprecated-module
34 # see https://www.logilab.org/ticket/2481
35 import string
36
37 class VarFormatter(string.Formatter):
38     """
39     Simple formatter that does not throw exception if the
40     variable does not exist. In this case, it is replaced by an
41     empty string.
42     """
43     def __init__(self):
44         string.Formatter.__init__(self)
45
46     def get_field(self, field_name, args, kwargs):
47         try:
48             return string.Formatter.get_field(self, field_name, args, kwargs)
49         except (KeyError, AttributeError):
50             return None, field_name
51
52     def format_field(self, value, spec):
53         if value is None:
54             return ''
55         return string.Formatter.format_field(self, value, spec)
56
57 def headers_to_unicode(headers):
58     """
59     Convert mail headers into a unicode dictionary
60
61     :arg email.message.Message headers:
62       The email headers
63     """
64     unicode_headers = {}
65     for key, hdr in headers.items():
66         try:
67             value, encoding = email.header.decode_header(hdr)[0]
68         except email.header.HeaderParseError:
69             try:
70                 # try to workaround badly formatted RFC2047 tokens
71                 hdr = re.sub(r"(==)(?!$)", u"= =", hdr)
72                 value, encoding = email.header.decode_header(hdr)[0]
73             except email.header.HeaderParseError:
74                 # fallback to wrong decoding
75                 value, encoding = hdr, 'utf-8'
76         if encoding is None:
77             value = unicode(value)
78         else:
79             value = value.decode(encoding, errors="replace")
80         unicode_headers[key] = value
81     return unicode_headers
82
83 def highest_fetch_level(fetch_list):
84     """
85     Return the highest fetch level for a mail.
86     """
87     if "all" in fetch_list:
88         return "all"
89     if "part1" in fetch_list:
90         return "part1"
91     if "headers" in fetch_list:
92         return "headers"
93     return "no"