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