blob: f9657f0a255db2597e670ebbf0cba4c9e279b729 [file] [log] [blame]
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001# Copyright (C) 2001-2006 Python Software Foundation
2# Author: Barry Warsaw
3# Contact: email-sig@python.org
4
5"""Encodings and related functions."""
6
7__all__ = [
8 'encode_7or8bit',
9 'encode_base64',
10 'encode_noop',
11 'encode_quopri',
12 ]
13
Guido van Rossum8b3febe2007-08-30 01:15:14 +000014
R David Murray6d94bd42011-03-16 15:52:22 -040015from base64 import encodebytes as _bencode
Guido van Rossum8b3febe2007-08-30 01:15:14 +000016from quopri import encodestring as _encodestring
17
18
19
20def _qencode(s):
21 enc = _encodestring(s, quotetabs=True)
22 # Must encode spaces, which quopri.encodestring() doesn't do
R David Murrayf6069f92013-06-27 18:37:00 -040023 return enc.replace(b' ', b'=20')
Guido van Rossum8b3febe2007-08-30 01:15:14 +000024
25
Guido van Rossum8b3febe2007-08-30 01:15:14 +000026def encode_base64(msg):
27 """Encode the message's payload in Base64.
28
29 Also, add an appropriate Content-Transfer-Encoding header.
30 """
R David Murray00ae4352013-08-21 21:10:31 -040031 orig = msg.get_payload(decode=True)
R. David Murray7da8f062010-06-04 16:11:08 +000032 encdata = str(_bencode(orig), 'ascii')
Guido van Rossum8b3febe2007-08-30 01:15:14 +000033 msg.set_payload(encdata)
34 msg['Content-Transfer-Encoding'] = 'base64'
35
36
37
38def encode_quopri(msg):
39 """Encode the message's payload in quoted-printable.
40
41 Also, add an appropriate Content-Transfer-Encoding header.
42 """
R David Murray00ae4352013-08-21 21:10:31 -040043 orig = msg.get_payload(decode=True)
Guido van Rossum8b3febe2007-08-30 01:15:14 +000044 encdata = _qencode(orig)
R David Murray00ae4352013-08-21 21:10:31 -040045 msg.set_payload(encdata)
Guido van Rossum8b3febe2007-08-30 01:15:14 +000046 msg['Content-Transfer-Encoding'] = 'quoted-printable'
47
48
49
50def encode_7or8bit(msg):
51 """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
R David Murray00ae4352013-08-21 21:10:31 -040052 orig = msg.get_payload(decode=True)
Guido van Rossum8b3febe2007-08-30 01:15:14 +000053 if orig is None:
54 # There's no payload. For backwards compatibility we use 7bit
55 msg['Content-Transfer-Encoding'] = '7bit'
56 return
R David Murray56a9d7e2011-03-15 12:20:02 -040057 # We play a trick to make this go fast. If encoding/decode to ASCII
58 # succeeds, we know the data must be 7bit, otherwise treat it as 8bit.
Guido van Rossum8b3febe2007-08-30 01:15:14 +000059 try:
R David Murray56a9d7e2011-03-15 12:20:02 -040060 if isinstance(orig, str):
61 orig.encode('ascii')
62 else:
63 orig.decode('ascii')
Guido van Rossum8b3febe2007-08-30 01:15:14 +000064 except UnicodeError:
Guido van Rossum8b3febe2007-08-30 01:15:14 +000065 charset = msg.get_charset()
66 output_cset = charset and charset.output_charset
R David Murrayec317a82013-02-11 10:51:28 -050067 # iso-2022-* is non-ASCII but encodes to a 7-bit representation
R. David Murrayef3d6bd2010-04-12 14:48:58 +000068 if output_cset and output_cset.lower().startswith('iso-2022-'):
Guido van Rossum8b3febe2007-08-30 01:15:14 +000069 msg['Content-Transfer-Encoding'] = '7bit'
70 else:
71 msg['Content-Transfer-Encoding'] = '8bit'
72 else:
73 msg['Content-Transfer-Encoding'] = '7bit'
74
75
76
77def encode_noop(msg):
78 """Do nothing."""