First public revision
[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
32 # pylint: disable=deprecated-module
33 # see https://www.logilab.org/ticket/2481
34 import string
35
36 class VarFormatter(string.Formatter):
37     """
38     Simple formatter that does not throw exception if the
39     variable does not exist. In this case, it is replaced by an
40     empty string.
41     """
42     def __init__(self):
43         string.Formatter.__init__(self)
44
45     def get_field(self, field_name, args, kwargs):
46         try:
47             return string.Formatter.get_field(self, field_name, args, kwargs)
48         except (KeyError, AttributeError):
49             return None, field_name
50
51     def format_field(self, value, spec):
52         if value is None:
53             return ''
54         return string.Formatter.format_field(self, value, spec)
55
56 def headers_to_unicode(headers):
57     """
58     Convert mail headers into a unicode dictionary
59
60     :arg email.message.Message headers:
61       The email headers
62     """
63     unicode_headers = {}
64     for key, hdr in headers.items():
65         value, encoding = email.header.decode_header(hdr)[0]
66         if encoding is None:
67             value = unicode(value)
68         else:
69             value = value.decode(encoding)
70         unicode_headers[key] = value
71     return unicode_headers
72
73 def highest_fetch_level(fetch_list):
74     """
75     Return the highest fetch level for a mail.
76     """
77     if "all" in fetch_list:
78         return "all"
79     if "part1" in fetch_list:
80         return "part1"
81     if "headers" in fetch_list:
82         return "headers"
83     return "no"