Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / lib / python2.4 / email / Encoders.py
CommitLineData
920dae64
AT
1# Copyright (C) 2001-2004 Python Software Foundation
2# Author: Barry Warsaw
3# Contact: email-sig@python.org
4
5"""Encodings and related functions."""
6
7import base64
8from quopri import encodestring as _encodestring
9
10def _qencode(s):
11 enc = _encodestring(s, quotetabs=True)
12 # Must encode spaces, which quopri.encodestring() doesn't do
13 return enc.replace(' ', '=20')
14
15
16def _bencode(s):
17 # We can't quite use base64.encodestring() since it tacks on a "courtesy
18 # newline". Blech!
19 if not s:
20 return s
21 hasnewline = (s[-1] == '\n')
22 value = base64.encodestring(s)
23 if not hasnewline and value[-1] == '\n':
24 return value[:-1]
25 return value
26
27
28\f
29def encode_base64(msg):
30 """Encode the message's payload in Base64.
31
32 Also, add an appropriate Content-Transfer-Encoding header.
33 """
34 orig = msg.get_payload()
35 encdata = _bencode(orig)
36 msg.set_payload(encdata)
37 msg['Content-Transfer-Encoding'] = 'base64'
38
39
40\f
41def encode_quopri(msg):
42 """Encode the message's payload in quoted-printable.
43
44 Also, add an appropriate Content-Transfer-Encoding header.
45 """
46 orig = msg.get_payload()
47 encdata = _qencode(orig)
48 msg.set_payload(encdata)
49 msg['Content-Transfer-Encoding'] = 'quoted-printable'
50
51
52\f
53def encode_7or8bit(msg):
54 """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
55 orig = msg.get_payload()
56 if orig is None:
57 # There's no payload. For backwards compatibility we use 7bit
58 msg['Content-Transfer-Encoding'] = '7bit'
59 return
60 # We play a trick to make this go fast. If encoding to ASCII succeeds, we
61 # know the data must be 7bit, otherwise treat it as 8bit.
62 try:
63 orig.encode('ascii')
64 except UnicodeError:
65 # iso-2022-* is non-ASCII but still 7-bit
66 charset = msg.get_charset()
67 output_cset = charset and charset.output_charset
68 if output_cset and output_cset.lower().startswith('iso-2202-'):
69 msg['Content-Transfer-Encoding'] = '7bit'
70 else:
71 msg['Content-Transfer-Encoding'] = '8bit'
72 else:
73 msg['Content-Transfer-Encoding'] = '7bit'
74
75
76\f
77def encode_noop(msg):
78 """Do nothing."""