annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/email/contentmanager.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 import binascii
jpayne@69 2 import email.charset
jpayne@69 3 import email.message
jpayne@69 4 import email.errors
jpayne@69 5 from email import quoprimime
jpayne@69 6
jpayne@69 7 class ContentManager:
jpayne@69 8
jpayne@69 9 def __init__(self):
jpayne@69 10 self.get_handlers = {}
jpayne@69 11 self.set_handlers = {}
jpayne@69 12
jpayne@69 13 def add_get_handler(self, key, handler):
jpayne@69 14 self.get_handlers[key] = handler
jpayne@69 15
jpayne@69 16 def get_content(self, msg, *args, **kw):
jpayne@69 17 content_type = msg.get_content_type()
jpayne@69 18 if content_type in self.get_handlers:
jpayne@69 19 return self.get_handlers[content_type](msg, *args, **kw)
jpayne@69 20 maintype = msg.get_content_maintype()
jpayne@69 21 if maintype in self.get_handlers:
jpayne@69 22 return self.get_handlers[maintype](msg, *args, **kw)
jpayne@69 23 if '' in self.get_handlers:
jpayne@69 24 return self.get_handlers[''](msg, *args, **kw)
jpayne@69 25 raise KeyError(content_type)
jpayne@69 26
jpayne@69 27 def add_set_handler(self, typekey, handler):
jpayne@69 28 self.set_handlers[typekey] = handler
jpayne@69 29
jpayne@69 30 def set_content(self, msg, obj, *args, **kw):
jpayne@69 31 if msg.get_content_maintype() == 'multipart':
jpayne@69 32 # XXX: is this error a good idea or not? We can remove it later,
jpayne@69 33 # but we can't add it later, so do it for now.
jpayne@69 34 raise TypeError("set_content not valid on multipart")
jpayne@69 35 handler = self._find_set_handler(msg, obj)
jpayne@69 36 msg.clear_content()
jpayne@69 37 handler(msg, obj, *args, **kw)
jpayne@69 38
jpayne@69 39 def _find_set_handler(self, msg, obj):
jpayne@69 40 full_path_for_error = None
jpayne@69 41 for typ in type(obj).__mro__:
jpayne@69 42 if typ in self.set_handlers:
jpayne@69 43 return self.set_handlers[typ]
jpayne@69 44 qname = typ.__qualname__
jpayne@69 45 modname = getattr(typ, '__module__', '')
jpayne@69 46 full_path = '.'.join((modname, qname)) if modname else qname
jpayne@69 47 if full_path_for_error is None:
jpayne@69 48 full_path_for_error = full_path
jpayne@69 49 if full_path in self.set_handlers:
jpayne@69 50 return self.set_handlers[full_path]
jpayne@69 51 if qname in self.set_handlers:
jpayne@69 52 return self.set_handlers[qname]
jpayne@69 53 name = typ.__name__
jpayne@69 54 if name in self.set_handlers:
jpayne@69 55 return self.set_handlers[name]
jpayne@69 56 if None in self.set_handlers:
jpayne@69 57 return self.set_handlers[None]
jpayne@69 58 raise KeyError(full_path_for_error)
jpayne@69 59
jpayne@69 60
jpayne@69 61 raw_data_manager = ContentManager()
jpayne@69 62
jpayne@69 63
jpayne@69 64 def get_text_content(msg, errors='replace'):
jpayne@69 65 content = msg.get_payload(decode=True)
jpayne@69 66 charset = msg.get_param('charset', 'ASCII')
jpayne@69 67 return content.decode(charset, errors=errors)
jpayne@69 68 raw_data_manager.add_get_handler('text', get_text_content)
jpayne@69 69
jpayne@69 70
jpayne@69 71 def get_non_text_content(msg):
jpayne@69 72 return msg.get_payload(decode=True)
jpayne@69 73 for maintype in 'audio image video application'.split():
jpayne@69 74 raw_data_manager.add_get_handler(maintype, get_non_text_content)
jpayne@69 75
jpayne@69 76
jpayne@69 77 def get_message_content(msg):
jpayne@69 78 return msg.get_payload(0)
jpayne@69 79 for subtype in 'rfc822 external-body'.split():
jpayne@69 80 raw_data_manager.add_get_handler('message/'+subtype, get_message_content)
jpayne@69 81
jpayne@69 82
jpayne@69 83 def get_and_fixup_unknown_message_content(msg):
jpayne@69 84 # If we don't understand a message subtype, we are supposed to treat it as
jpayne@69 85 # if it were application/octet-stream, per
jpayne@69 86 # tools.ietf.org/html/rfc2046#section-5.2.4. Feedparser doesn't do that,
jpayne@69 87 # so do our best to fix things up. Note that it is *not* appropriate to
jpayne@69 88 # model message/partial content as Message objects, so they are handled
jpayne@69 89 # here as well. (How to reassemble them is out of scope for this comment :)
jpayne@69 90 return bytes(msg.get_payload(0))
jpayne@69 91 raw_data_manager.add_get_handler('message',
jpayne@69 92 get_and_fixup_unknown_message_content)
jpayne@69 93
jpayne@69 94
jpayne@69 95 def _prepare_set(msg, maintype, subtype, headers):
jpayne@69 96 msg['Content-Type'] = '/'.join((maintype, subtype))
jpayne@69 97 if headers:
jpayne@69 98 if not hasattr(headers[0], 'name'):
jpayne@69 99 mp = msg.policy
jpayne@69 100 headers = [mp.header_factory(*mp.header_source_parse([header]))
jpayne@69 101 for header in headers]
jpayne@69 102 try:
jpayne@69 103 for header in headers:
jpayne@69 104 if header.defects:
jpayne@69 105 raise header.defects[0]
jpayne@69 106 msg[header.name] = header
jpayne@69 107 except email.errors.HeaderDefect as exc:
jpayne@69 108 raise ValueError("Invalid header: {}".format(
jpayne@69 109 header.fold(policy=msg.policy))) from exc
jpayne@69 110
jpayne@69 111
jpayne@69 112 def _finalize_set(msg, disposition, filename, cid, params):
jpayne@69 113 if disposition is None and filename is not None:
jpayne@69 114 disposition = 'attachment'
jpayne@69 115 if disposition is not None:
jpayne@69 116 msg['Content-Disposition'] = disposition
jpayne@69 117 if filename is not None:
jpayne@69 118 msg.set_param('filename',
jpayne@69 119 filename,
jpayne@69 120 header='Content-Disposition',
jpayne@69 121 replace=True)
jpayne@69 122 if cid is not None:
jpayne@69 123 msg['Content-ID'] = cid
jpayne@69 124 if params is not None:
jpayne@69 125 for key, value in params.items():
jpayne@69 126 msg.set_param(key, value)
jpayne@69 127
jpayne@69 128
jpayne@69 129 # XXX: This is a cleaned-up version of base64mime.body_encode (including a bug
jpayne@69 130 # fix in the calculation of unencoded_bytes_per_line). It would be nice to
jpayne@69 131 # drop both this and quoprimime.body_encode in favor of enhanced binascii
jpayne@69 132 # routines that accepted a max_line_length parameter.
jpayne@69 133 def _encode_base64(data, max_line_length):
jpayne@69 134 encoded_lines = []
jpayne@69 135 unencoded_bytes_per_line = max_line_length // 4 * 3
jpayne@69 136 for i in range(0, len(data), unencoded_bytes_per_line):
jpayne@69 137 thisline = data[i:i+unencoded_bytes_per_line]
jpayne@69 138 encoded_lines.append(binascii.b2a_base64(thisline).decode('ascii'))
jpayne@69 139 return ''.join(encoded_lines)
jpayne@69 140
jpayne@69 141
jpayne@69 142 def _encode_text(string, charset, cte, policy):
jpayne@69 143 lines = string.encode(charset).splitlines()
jpayne@69 144 linesep = policy.linesep.encode('ascii')
jpayne@69 145 def embedded_body(lines): return linesep.join(lines) + linesep
jpayne@69 146 def normal_body(lines): return b'\n'.join(lines) + b'\n'
jpayne@69 147 if cte==None:
jpayne@69 148 # Use heuristics to decide on the "best" encoding.
jpayne@69 149 try:
jpayne@69 150 return '7bit', normal_body(lines).decode('ascii')
jpayne@69 151 except UnicodeDecodeError:
jpayne@69 152 pass
jpayne@69 153 if (policy.cte_type == '8bit' and
jpayne@69 154 max(len(x) for x in lines) <= policy.max_line_length):
jpayne@69 155 return '8bit', normal_body(lines).decode('ascii', 'surrogateescape')
jpayne@69 156 sniff = embedded_body(lines[:10])
jpayne@69 157 sniff_qp = quoprimime.body_encode(sniff.decode('latin-1'),
jpayne@69 158 policy.max_line_length)
jpayne@69 159 sniff_base64 = binascii.b2a_base64(sniff)
jpayne@69 160 # This is a little unfair to qp; it includes lineseps, base64 doesn't.
jpayne@69 161 if len(sniff_qp) > len(sniff_base64):
jpayne@69 162 cte = 'base64'
jpayne@69 163 else:
jpayne@69 164 cte = 'quoted-printable'
jpayne@69 165 if len(lines) <= 10:
jpayne@69 166 return cte, sniff_qp
jpayne@69 167 if cte == '7bit':
jpayne@69 168 data = normal_body(lines).decode('ascii')
jpayne@69 169 elif cte == '8bit':
jpayne@69 170 data = normal_body(lines).decode('ascii', 'surrogateescape')
jpayne@69 171 elif cte == 'quoted-printable':
jpayne@69 172 data = quoprimime.body_encode(normal_body(lines).decode('latin-1'),
jpayne@69 173 policy.max_line_length)
jpayne@69 174 elif cte == 'base64':
jpayne@69 175 data = _encode_base64(embedded_body(lines), policy.max_line_length)
jpayne@69 176 else:
jpayne@69 177 raise ValueError("Unknown content transfer encoding {}".format(cte))
jpayne@69 178 return cte, data
jpayne@69 179
jpayne@69 180
jpayne@69 181 def set_text_content(msg, string, subtype="plain", charset='utf-8', cte=None,
jpayne@69 182 disposition=None, filename=None, cid=None,
jpayne@69 183 params=None, headers=None):
jpayne@69 184 _prepare_set(msg, 'text', subtype, headers)
jpayne@69 185 cte, payload = _encode_text(string, charset, cte, msg.policy)
jpayne@69 186 msg.set_payload(payload)
jpayne@69 187 msg.set_param('charset',
jpayne@69 188 email.charset.ALIASES.get(charset, charset),
jpayne@69 189 replace=True)
jpayne@69 190 msg['Content-Transfer-Encoding'] = cte
jpayne@69 191 _finalize_set(msg, disposition, filename, cid, params)
jpayne@69 192 raw_data_manager.add_set_handler(str, set_text_content)
jpayne@69 193
jpayne@69 194
jpayne@69 195 def set_message_content(msg, message, subtype="rfc822", cte=None,
jpayne@69 196 disposition=None, filename=None, cid=None,
jpayne@69 197 params=None, headers=None):
jpayne@69 198 if subtype == 'partial':
jpayne@69 199 raise ValueError("message/partial is not supported for Message objects")
jpayne@69 200 if subtype == 'rfc822':
jpayne@69 201 if cte not in (None, '7bit', '8bit', 'binary'):
jpayne@69 202 # http://tools.ietf.org/html/rfc2046#section-5.2.1 mandate.
jpayne@69 203 raise ValueError(
jpayne@69 204 "message/rfc822 parts do not support cte={}".format(cte))
jpayne@69 205 # 8bit will get coerced on serialization if policy.cte_type='7bit'. We
jpayne@69 206 # may end up claiming 8bit when it isn't needed, but the only negative
jpayne@69 207 # result of that should be a gateway that needs to coerce to 7bit
jpayne@69 208 # having to look through the whole embedded message to discover whether
jpayne@69 209 # or not it actually has to do anything.
jpayne@69 210 cte = '8bit' if cte is None else cte
jpayne@69 211 elif subtype == 'external-body':
jpayne@69 212 if cte not in (None, '7bit'):
jpayne@69 213 # http://tools.ietf.org/html/rfc2046#section-5.2.3 mandate.
jpayne@69 214 raise ValueError(
jpayne@69 215 "message/external-body parts do not support cte={}".format(cte))
jpayne@69 216 cte = '7bit'
jpayne@69 217 elif cte is None:
jpayne@69 218 # http://tools.ietf.org/html/rfc2046#section-5.2.4 says all future
jpayne@69 219 # subtypes should be restricted to 7bit, so assume that.
jpayne@69 220 cte = '7bit'
jpayne@69 221 _prepare_set(msg, 'message', subtype, headers)
jpayne@69 222 msg.set_payload([message])
jpayne@69 223 msg['Content-Transfer-Encoding'] = cte
jpayne@69 224 _finalize_set(msg, disposition, filename, cid, params)
jpayne@69 225 raw_data_manager.add_set_handler(email.message.Message, set_message_content)
jpayne@69 226
jpayne@69 227
jpayne@69 228 def set_bytes_content(msg, data, maintype, subtype, cte='base64',
jpayne@69 229 disposition=None, filename=None, cid=None,
jpayne@69 230 params=None, headers=None):
jpayne@69 231 _prepare_set(msg, maintype, subtype, headers)
jpayne@69 232 if cte == 'base64':
jpayne@69 233 data = _encode_base64(data, max_line_length=msg.policy.max_line_length)
jpayne@69 234 elif cte == 'quoted-printable':
jpayne@69 235 # XXX: quoprimime.body_encode won't encode newline characters in data,
jpayne@69 236 # so we can't use it. This means max_line_length is ignored. Another
jpayne@69 237 # bug to fix later. (Note: encoders.quopri is broken on line ends.)
jpayne@69 238 data = binascii.b2a_qp(data, istext=False, header=False, quotetabs=True)
jpayne@69 239 data = data.decode('ascii')
jpayne@69 240 elif cte == '7bit':
jpayne@69 241 # Make sure it really is only ASCII. The early warning here seems
jpayne@69 242 # worth the overhead...if you care write your own content manager :).
jpayne@69 243 data.encode('ascii')
jpayne@69 244 elif cte in ('8bit', 'binary'):
jpayne@69 245 data = data.decode('ascii', 'surrogateescape')
jpayne@69 246 msg.set_payload(data)
jpayne@69 247 msg['Content-Transfer-Encoding'] = cte
jpayne@69 248 _finalize_set(msg, disposition, filename, cid, params)
jpayne@69 249 for typ in (bytes, bytearray, memoryview):
jpayne@69 250 raw_data_manager.add_set_handler(typ, set_bytes_content)