Home | History | Annotate | Download | only in email
      1 # Copyright (C) 2001-2006 Python Software Foundation
      2 # Author: Barry Warsaw
      3 # Contact: email-sig (at] 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 
     14 import base64
     15 
     16 from quopri import encodestring as _encodestring
     17 
     18 
     19 
     21 def _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 
     27 def _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 
     41 def 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 
     54 def 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 
     67 def 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         msg['Content-Transfer-Encoding'] = '8bit'
     80     else:
     81         msg['Content-Transfer-Encoding'] = '7bit'
     82 
     83 
     84 
     86 def encode_noop(msg):
     87     """Do nothing."""
     88