MCPcopy Index your code
hub / github.com/python/cpython / DecodedGenerator

Class DecodedGenerator

Lib/email/generator.py:468–522  ·  view source on GitHub ↗

Generates a text representation of a message. Like the Generator base class, except that non-text parts are substituted with a format string representing the part.

Source from the content-addressed store, hash-verified

466_FMT = '[Non-text (%(type)s) part of message omitted, filename %(filename)s]'
467
468class DecodedGenerator(Generator):
469 """Generates a text representation of a message.
470
471 Like the Generator base class, except that non-text parts are substituted
472 with a format string representing the part.
473 """
474 def __init__(self, outfp, mangle_from_=None, maxheaderlen=None, fmt=None, *,
475 policy=None):
476 """Like Generator.__init__() except that an additional optional
477 argument is allowed.
478
479 Walks through all subparts of a message. If the subpart is of main
480 type 'text', then it prints the decoded payload of the subpart.
481
482 Otherwise, fmt is a format string that is used instead of the message
483 payload. fmt is expanded with the following keywords (in
484 %(keyword)s format):
485
486 type : Full MIME type of the non-text part
487 maintype : Main MIME type of the non-text part
488 subtype : Sub-MIME type of the non-text part
489 filename : Filename of the non-text part
490 description: Description associated with the non-text part
491 encoding : Content transfer encoding of the non-text part
492
493 The default value for fmt is None, meaning
494
495 [Non-text (%(type)s) part of message omitted, filename %(filename)s]
496 """
497 Generator.__init__(self, outfp, mangle_from_, maxheaderlen,
498 policy=policy)
499 if fmt is None:
500 self._fmt = _FMT
501 else:
502 self._fmt = fmt
503
504 def _dispatch(self, msg):
505 for part in msg.walk():
506 maintype = part.get_content_maintype()
507 if maintype == 'text':
508 print(part.get_payload(decode=False), file=self)
509 elif maintype == 'multipart':
510 # Just skip this
511 pass
512 else:
513 print(self._fmt % {
514 'type' : part.get_content_type(),
515 'maintype' : part.get_content_maintype(),
516 'subtype' : part.get_content_subtype(),
517 'filename' : part.get_filename('[no filename]'),
518 'description': part.get('Content-Description',
519 '[no description]'),
520 'encoding' : part.get('Content-Transfer-Encoding',
521 '[no encoding]'),
522 }, file=self)
523
524
525# Helper used by Generator._make_boundary

Callers 1

Calls

no outgoing calls

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…