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.
33 # pylint: disable=deprecated-module
34 # see https://www.logilab.org/ticket/2481
37 class VarFormatter(string.Formatter):
39 Simple formatter that does not throw exception if the
40 variable does not exist. In this case, it is replaced by an
44 string.Formatter.__init__(self)
46 def get_field(self, field_name, args, kwargs):
48 return string.Formatter.get_field(self, field_name, args, kwargs)
49 except (KeyError, AttributeError):
50 return None, field_name
52 def format_field(self, value, spec):
55 return string.Formatter.format_field(self, value, spec)
57 def headers_to_unicode(headers):
59 Convert mail headers into a unicode dictionary
61 :arg email.message.Message headers:
65 for key, hdr in headers.items():
67 value, encoding = email.header.decode_header(hdr)[0]
68 except email.header.HeaderParseError:
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'
77 value = unicode(value)
79 value = value.decode(encoding, errors="replace")
80 unicode_headers[key] = value
81 return unicode_headers
83 def highest_fetch_level(fetch_list):
85 Return the highest fetch level for a mail.
87 if "all" in fetch_list:
89 if "part1" in fetch_list:
91 if "headers" in fetch_list: