blob: 5460fdb956bb2470de6e0051e75652381340ebb3 [file] [log] [blame]
Barry Warsaw409a4c02002-04-10 21:01:31 +00001# Copyright (C) 2001,2002 Python Software Foundation
Barry Warsawba925802001-09-23 03:17:28 +00002# Author: barry@zope.com (Barry Warsaw)
3
4"""Module containing encoding functions for Image.Image and Text.Text.
5"""
6
7import base64
Barry Warsawba925802001-09-23 03:17:28 +00008
9
Barry Warsawe968ead2001-10-04 17:05:11 +000010
Barry Warsawba925802001-09-23 03:17:28 +000011# Helpers
Barry Warsaw8c1aac22002-05-19 23:44:19 +000012try:
13 from quopri import encodestring as _encodestring
14
15 def _qencode(s):
16 enc = _encodestring(s, quotetabs=1)
17 # Must encode spaces, which quopri.encodestring() doesn't do
18 return enc.replace(' ', '=20')
19except ImportError:
20 # Python 2.1 doesn't have quopri.encodestring()
21 from cStringIO import StringIO
22 import quopri as _quopri
23
24 def _qencode(s):
25 if not s:
26 return s
27 hasnewline = (s[-1] == '\n')
28 infp = StringIO(s)
29 outfp = StringIO()
30 _quopri.encode(infp, outfp, quotetabs=1)
31 # Python 2.x's encode() doesn't encode spaces even when quotetabs==1
32 value = outfp.getvalue().replace(' ', '=20')
33 if not hasnewline and value[-1] == '\n':
34 return value[:-1]
35 return value
Barry Warsawba925802001-09-23 03:17:28 +000036
Barry Warsaw6f70c412001-09-26 05:26:22 +000037
Barry Warsawba925802001-09-23 03:17:28 +000038def _bencode(s):
39 # We can't quite use base64.encodestring() since it tacks on a "courtesy
40 # newline". Blech!
41 if not s:
42 return s
43 hasnewline = (s[-1] == '\n')
44 value = base64.encodestring(s)
45 if not hasnewline and value[-1] == '\n':
46 return value[:-1]
47 return value
48
49
Barry Warsawe968ead2001-10-04 17:05:11 +000050
Barry Warsawba925802001-09-23 03:17:28 +000051def encode_base64(msg):
52 """Encode the message's payload in Base64.
53
Barry Warsaw12272a22002-10-01 00:05:24 +000054 Also, add an appropriate Content-Transfer-Encoding header.
Barry Warsawba925802001-09-23 03:17:28 +000055 """
56 orig = msg.get_payload()
57 encdata = _bencode(orig)
58 msg.set_payload(encdata)
59 msg['Content-Transfer-Encoding'] = 'base64'
60
61
Barry Warsawe968ead2001-10-04 17:05:11 +000062
Barry Warsawba925802001-09-23 03:17:28 +000063def encode_quopri(msg):
Barry Warsaw12272a22002-10-01 00:05:24 +000064 """Encode the message's payload in quoted-printable.
Barry Warsawba925802001-09-23 03:17:28 +000065
Barry Warsaw12272a22002-10-01 00:05:24 +000066 Also, add an appropriate Content-Transfer-Encoding header.
Barry Warsawba925802001-09-23 03:17:28 +000067 """
68 orig = msg.get_payload()
69 encdata = _qencode(orig)
70 msg.set_payload(encdata)
71 msg['Content-Transfer-Encoding'] = 'quoted-printable'
72
73
Barry Warsawe968ead2001-10-04 17:05:11 +000074
Barry Warsawba925802001-09-23 03:17:28 +000075def encode_7or8bit(msg):
Barry Warsaw12272a22002-10-01 00:05:24 +000076 """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
Barry Warsawba925802001-09-23 03:17:28 +000077 orig = msg.get_payload()
Barry Warsaw409a4c02002-04-10 21:01:31 +000078 if orig is None:
79 # There's no payload. For backwards compatibility we use 7bit
80 msg['Content-Transfer-Encoding'] = '7bit'
81 return
Barry Warsawba925802001-09-23 03:17:28 +000082 # We play a trick to make this go fast. If encoding to ASCII succeeds, we
83 # know the data must be 7bit, otherwise treat it as 8bit.
84 try:
85 orig.encode('ascii')
86 except UnicodeError:
87 msg['Content-Transfer-Encoding'] = '8bit'
88 else:
89 msg['Content-Transfer-Encoding'] = '7bit'
90
91
Barry Warsawe968ead2001-10-04 17:05:11 +000092
Barry Warsawba925802001-09-23 03:17:28 +000093def encode_noop(msg):
94 """Do nothing."""