annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/email/encoders.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 # Copyright (C) 2001-2006 Python Software Foundation
jpayne@68 2 # Author: Barry Warsaw
jpayne@68 3 # Contact: email-sig@python.org
jpayne@68 4
jpayne@68 5 """Encodings and related functions."""
jpayne@68 6
jpayne@68 7 __all__ = [
jpayne@68 8 'encode_7or8bit',
jpayne@68 9 'encode_base64',
jpayne@68 10 'encode_noop',
jpayne@68 11 'encode_quopri',
jpayne@68 12 ]
jpayne@68 13
jpayne@68 14
jpayne@68 15 from base64 import encodebytes as _bencode
jpayne@68 16 from quopri import encodestring as _encodestring
jpayne@68 17
jpayne@68 18
jpayne@68 19
jpayne@68 20 def _qencode(s):
jpayne@68 21 enc = _encodestring(s, quotetabs=True)
jpayne@68 22 # Must encode spaces, which quopri.encodestring() doesn't do
jpayne@68 23 return enc.replace(b' ', b'=20')
jpayne@68 24
jpayne@68 25
jpayne@68 26 def encode_base64(msg):
jpayne@68 27 """Encode the message's payload in Base64.
jpayne@68 28
jpayne@68 29 Also, add an appropriate Content-Transfer-Encoding header.
jpayne@68 30 """
jpayne@68 31 orig = msg.get_payload(decode=True)
jpayne@68 32 encdata = str(_bencode(orig), 'ascii')
jpayne@68 33 msg.set_payload(encdata)
jpayne@68 34 msg['Content-Transfer-Encoding'] = 'base64'
jpayne@68 35
jpayne@68 36
jpayne@68 37
jpayne@68 38 def encode_quopri(msg):
jpayne@68 39 """Encode the message's payload in quoted-printable.
jpayne@68 40
jpayne@68 41 Also, add an appropriate Content-Transfer-Encoding header.
jpayne@68 42 """
jpayne@68 43 orig = msg.get_payload(decode=True)
jpayne@68 44 encdata = _qencode(orig)
jpayne@68 45 msg.set_payload(encdata)
jpayne@68 46 msg['Content-Transfer-Encoding'] = 'quoted-printable'
jpayne@68 47
jpayne@68 48
jpayne@68 49
jpayne@68 50 def encode_7or8bit(msg):
jpayne@68 51 """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
jpayne@68 52 orig = msg.get_payload(decode=True)
jpayne@68 53 if orig is None:
jpayne@68 54 # There's no payload. For backwards compatibility we use 7bit
jpayne@68 55 msg['Content-Transfer-Encoding'] = '7bit'
jpayne@68 56 return
jpayne@68 57 # We play a trick to make this go fast. If decoding from ASCII succeeds,
jpayne@68 58 # we know the data must be 7bit, otherwise treat it as 8bit.
jpayne@68 59 try:
jpayne@68 60 orig.decode('ascii')
jpayne@68 61 except UnicodeError:
jpayne@68 62 msg['Content-Transfer-Encoding'] = '8bit'
jpayne@68 63 else:
jpayne@68 64 msg['Content-Transfer-Encoding'] = '7bit'
jpayne@68 65
jpayne@68 66
jpayne@68 67
jpayne@68 68 def encode_noop(msg):
jpayne@68 69 """Do nothing."""