source: python/vendor/Python-2.6.5/Lib/email/encoders.py

Last change on this file was 2, checked in by Yuri Dario, 15 years ago

Initial import for vendor code.

  • Property svn:eol-style set to native
File size: 2.2 KB
Line 
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
7__all__ = [
8 'encode_7or8bit',
9 'encode_base64',
10 'encode_noop',
11 'encode_quopri',
12 ]
13
14import base64
15
16from quopri import encodestring as _encodestring
17
18
19
20
21def _qencode(s):
22 enc = _encodestring(s, quotetabs=True)
23 # Must encode spaces, which quopri.encodestring() doesn't do
24 return enc.replace(' ', '=20')
25
26
27def _bencode(s):
28 # We can't quite use base64.encodestring() since it tacks on a "courtesy
29 # newline". Blech!
30 if not s:
31 return s
32 hasnewline = (s[-1] == '\n')
33 value = base64.encodestring(s)
34 if not hasnewline and value[-1] == '\n':
35 return value[:-1]
36 return value
37
38
39
40
41def encode_base64(msg):
42 """Encode the message's payload in Base64.
43
44 Also, add an appropriate Content-Transfer-Encoding header.
45 """
46 orig = msg.get_payload()
47 encdata = _bencode(orig)
48 msg.set_payload(encdata)
49 msg['Content-Transfer-Encoding'] = 'base64'
50
51
52
53
54def encode_quopri(msg):
55 """Encode the message's payload in quoted-printable.
56
57 Also, add an appropriate Content-Transfer-Encoding header.
58 """
59 orig = msg.get_payload()
60 encdata = _qencode(orig)
61 msg.set_payload(encdata)
62 msg['Content-Transfer-Encoding'] = 'quoted-printable'
63
64
65
66
67def encode_7or8bit(msg):
68 """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
69 orig = msg.get_payload()
70 if orig is None:
71 # There's no payload. For backwards compatibility we use 7bit
72 msg['Content-Transfer-Encoding'] = '7bit'
73 return
74 # We play a trick to make this go fast. If encoding to ASCII succeeds, we
75 # know the data must be 7bit, otherwise treat it as 8bit.
76 try:
77 orig.encode('ascii')
78 except UnicodeError:
79 # iso-2022-* is non-ASCII but still 7-bit
80 charset = msg.get_charset()
81 output_cset = charset and charset.output_charset
82 if output_cset and output_cset.lower().startswith('iso-2202-'):
83 msg['Content-Transfer-Encoding'] = '7bit'
84 else:
85 msg['Content-Transfer-Encoding'] = '8bit'
86 else:
87 msg['Content-Transfer-Encoding'] = '7bit'
88
89
90
91
92def encode_noop(msg):
93 """Do nothing."""
Note: See TracBrowser for help on using the repository browser.