annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/email/encoders.py @ 69:33d812a61356

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