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 |
| 8 | from quopri import encodestring as _encodestring |
| 9 | |
| 10 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 11 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 12 | # Helpers |
| 13 | def _qencode(s): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 14 | enc = _encodestring(s, quotetabs=1) |
| 15 | # Must encode spaces, which quopri.encodestring() doesn't do |
| 16 | return enc.replace(' ', '=20') |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 17 | |
Barry Warsaw | 6f70c41 | 2001-09-26 05:26:22 +0000 | [diff] [blame] | 18 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 19 | def _bencode(s): |
| 20 | # We can't quite use base64.encodestring() since it tacks on a "courtesy |
| 21 | # newline". Blech! |
| 22 | if not s: |
| 23 | return s |
| 24 | hasnewline = (s[-1] == '\n') |
| 25 | value = base64.encodestring(s) |
| 26 | if not hasnewline and value[-1] == '\n': |
| 27 | return value[:-1] |
| 28 | return value |
| 29 | |
| 30 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 31 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 32 | def encode_base64(msg): |
| 33 | """Encode the message's payload in Base64. |
| 34 | |
| 35 | Also, add an appropriate Content-Transfer-Encoding: header. |
| 36 | """ |
| 37 | orig = msg.get_payload() |
| 38 | encdata = _bencode(orig) |
| 39 | msg.set_payload(encdata) |
| 40 | msg['Content-Transfer-Encoding'] = 'base64' |
| 41 | |
| 42 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 43 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 44 | def encode_quopri(msg): |
| 45 | """Encode the message's payload in Quoted-Printable. |
| 46 | |
| 47 | Also, add an appropriate Content-Transfer-Encoding: header. |
| 48 | """ |
| 49 | orig = msg.get_payload() |
| 50 | encdata = _qencode(orig) |
| 51 | msg.set_payload(encdata) |
| 52 | msg['Content-Transfer-Encoding'] = 'quoted-printable' |
| 53 | |
| 54 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 55 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 56 | def encode_7or8bit(msg): |
| 57 | """Set the Content-Transfer-Encoding: header to 7bit or 8bit.""" |
| 58 | orig = msg.get_payload() |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 59 | if orig is None: |
| 60 | # There's no payload. For backwards compatibility we use 7bit |
| 61 | msg['Content-Transfer-Encoding'] = '7bit' |
| 62 | return |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 63 | # We play a trick to make this go fast. If encoding to ASCII succeeds, we |
| 64 | # know the data must be 7bit, otherwise treat it as 8bit. |
| 65 | try: |
| 66 | orig.encode('ascii') |
| 67 | except UnicodeError: |
| 68 | msg['Content-Transfer-Encoding'] = '8bit' |
| 69 | else: |
| 70 | msg['Content-Transfer-Encoding'] = '7bit' |
| 71 | |
| 72 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 73 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 74 | def encode_noop(msg): |
| 75 | """Do nothing.""" |