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

Class Generator

Lib/email/generator.py:28–404  ·  view source on GitHub ↗

Generates output from a Message object tree. This basic generator writes the message to the given file object as plain text.

Source from the content-addressed store, hash-verified

26
27
28class Generator:
29 """Generates output from a Message object tree.
30
31 This basic generator writes the message to the given file object as plain
32 text.
33 """
34 #
35 # Public interface
36 #
37
38 def __init__(self, outfp, mangle_from_=None, maxheaderlen=None, *,
39 policy=None):
40 """Create the generator for message flattening.
41
42 outfp is the output file-like object for writing the message to. It
43 must have a write() method.
44
45 Optional mangle_from_ is a flag that, when True (the default if policy
46 is not set), escapes From_ lines in the body of the message by putting
47 a '>' in front of them.
48
49 Optional maxheaderlen specifies the longest length for a non-continued
50 header. When a header line is longer (in characters, with tabs
51 expanded to 8 spaces) than maxheaderlen, the header will split as
52 defined in the Header class. Set maxheaderlen to zero to disable
53 header wrapping. The default is 78, as recommended (but not required)
54 by RFC 5322 section 2.1.1.
55
56 The policy keyword specifies a policy object that controls a number of
57 aspects of the generator's operation. If no policy is specified,
58 the policy associated with the Message object passed to the
59 flatten method is used.
60
61 """
62
63 if mangle_from_ is None:
64 mangle_from_ = True if policy is None else policy.mangle_from_
65 self._fp = outfp
66 self._mangle_from_ = mangle_from_
67 self.maxheaderlen = maxheaderlen
68 self.policy = policy
69
70 def write(self, s):
71 # Just delegate to the file object
72 self._fp.write(s)
73
74 def flatten(self, msg, unixfrom=False, linesep=None):
75 r"""Print the message object tree rooted at msg to the output file
76 specified when the Generator instance was created.
77
78 unixfrom is a flag that forces the printing of a Unix From_ delimiter
79 before the first object in the message tree. If the original message
80 has no From_ delimiter, a 'standard' one is crafted. By default, this
81 is False to inhibit the printing of any From_ delimiter.
82
83 Note that for subobjects, no From_ line is printed.
84
85 linesep specifies the characters used to indicate a new line in

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…