Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001,2002 Python Software Foundation |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 2 | # Author: barry@zope.com (Barry Warsaw) |
| 3 | |
| 4 | """Module containing encoding functions for Image.Image and Text.Text. |
| 5 | """ |
| 6 | |
| 7 | import base64 |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 8 | |
| 9 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 10 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 11 | # Helpers |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 12 | try: |
| 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') |
| 19 | except 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 Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 36 | |
Barry Warsaw | 6f70c41 | 2001-09-26 05:26:22 +0000 | [diff] [blame] | 37 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 38 | def _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 Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 50 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 51 | def encode_base64(msg): |
| 52 | """Encode the message's payload in Base64. |
| 53 | |
| 54 | Also, add an appropriate Content-Transfer-Encoding: header. |
| 55 | """ |
| 56 | orig = msg.get_payload() |
| 57 | encdata = _bencode(orig) |
| 58 | msg.set_payload(encdata) |
| 59 | msg['Content-Transfer-Encoding'] = 'base64' |
| 60 | |
| 61 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 62 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 63 | def encode_quopri(msg): |
| 64 | """Encode the message's payload in Quoted-Printable. |
| 65 | |
| 66 | Also, add an appropriate Content-Transfer-Encoding: header. |
| 67 | """ |
| 68 | orig = msg.get_payload() |
| 69 | encdata = _qencode(orig) |
| 70 | msg.set_payload(encdata) |
| 71 | msg['Content-Transfer-Encoding'] = 'quoted-printable' |
| 72 | |
| 73 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 74 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 75 | def encode_7or8bit(msg): |
| 76 | """Set the Content-Transfer-Encoding: header to 7bit or 8bit.""" |
| 77 | orig = msg.get_payload() |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 78 | if orig is None: |
| 79 | # There's no payload. For backwards compatibility we use 7bit |
| 80 | msg['Content-Transfer-Encoding'] = '7bit' |
| 81 | return |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 82 | # 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 Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 92 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 93 | def encode_noop(msg): |
| 94 | """Do nothing.""" |