comparison planemo/lib/python3.7/site-packages/future/backports/email/encoders.py @ 0:d30785e31577 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:18:57 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d30785e31577
1 # Copyright (C) 2001-2006 Python Software Foundation
2 # Author: Barry Warsaw
3 # Contact: email-sig@python.org
4
5 """Encodings and related functions."""
6 from __future__ import unicode_literals
7 from __future__ import division
8 from __future__ import absolute_import
9 from future.builtins import str
10
11 __all__ = [
12 'encode_7or8bit',
13 'encode_base64',
14 'encode_noop',
15 'encode_quopri',
16 ]
17
18
19 try:
20 from base64 import encodebytes as _bencode
21 except ImportError:
22 # Py2 compatibility. TODO: test this!
23 from base64 import encodestring as _bencode
24 from quopri import encodestring as _encodestring
25
26
27 def _qencode(s):
28 enc = _encodestring(s, quotetabs=True)
29 # Must encode spaces, which quopri.encodestring() doesn't do
30 return enc.replace(' ', '=20')
31
32
33 def encode_base64(msg):
34 """Encode the message's payload in Base64.
35
36 Also, add an appropriate Content-Transfer-Encoding header.
37 """
38 orig = msg.get_payload()
39 encdata = str(_bencode(orig), 'ascii')
40 msg.set_payload(encdata)
41 msg['Content-Transfer-Encoding'] = 'base64'
42
43
44 def encode_quopri(msg):
45 """Encode the message's payload in quoted-printable.
46
47 Also, add an appropriate Content-Transfer-Encoding header.
48 """
49 orig = msg.get_payload()
50 encdata = _qencode(orig)
51 msg.set_payload(encdata)
52 msg['Content-Transfer-Encoding'] = 'quoted-printable'
53
54
55 def encode_7or8bit(msg):
56 """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
57 orig = msg.get_payload()
58 if orig is None:
59 # There's no payload. For backwards compatibility we use 7bit
60 msg['Content-Transfer-Encoding'] = '7bit'
61 return
62 # We play a trick to make this go fast. If encoding/decode to ASCII
63 # succeeds, we know the data must be 7bit, otherwise treat it as 8bit.
64 try:
65 if isinstance(orig, str):
66 orig.encode('ascii')
67 else:
68 orig.decode('ascii')
69 except UnicodeError:
70 charset = msg.get_charset()
71 output_cset = charset and charset.output_charset
72 # iso-2022-* is non-ASCII but encodes to a 7-bit representation
73 if output_cset and output_cset.lower().startswith('iso-2022-'):
74 msg['Content-Transfer-Encoding'] = '7bit'
75 else:
76 msg['Content-Transfer-Encoding'] = '8bit'
77 else:
78 msg['Content-Transfer-Encoding'] = '7bit'
79 if not isinstance(orig, str):
80 msg.set_payload(orig.decode('ascii', 'surrogateescape'))
81
82
83 def encode_noop(msg):
84 """Do nothing."""
85 # Well, not quite *nothing*: in Python3 we have to turn bytes into a string
86 # in our internal surrogateescaped form in order to keep the model
87 # consistent.
88 orig = msg.get_payload()
89 if not isinstance(orig, str):
90 msg.set_payload(orig.decode('ascii', 'surrogateescape'))