4 # Copyright 2015, Olivier MATZ <zer0@droids-corp.org>
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
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.
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.
32 # pylint: disable=deprecated-module
33 # see https://www.logilab.org/ticket/2481
36 class VarFormatter(string.Formatter):
38 Simple formatter that does not throw exception if the
39 variable does not exist. In this case, it is replaced by an
43 string.Formatter.__init__(self)
45 def get_field(self, field_name, args, kwargs):
47 return string.Formatter.get_field(self, field_name, args, kwargs)
48 except (KeyError, AttributeError):
49 return None, field_name
51 def format_field(self, value, spec):
54 return string.Formatter.format_field(self, value, spec)
56 def headers_to_unicode(headers):
58 Convert mail headers into a unicode dictionary
60 :arg email.message.Message headers:
64 for key, hdr in headers.items():
65 value, encoding = email.header.decode_header(hdr)[0]
67 value = unicode(value)
69 value = value.decode(encoding)
70 unicode_headers[key] = value
71 return unicode_headers
73 def highest_fetch_level(fetch_list):
75 Return the highest fetch level for a mail.
77 if "all" in fetch_list:
79 if "part1" in fetch_list:
81 if "headers" in fetch_list: