|
1 | 1 | import email.message
|
| 2 | +import email.policy |
2 | 3 | import re
|
3 | 4 | import textwrap
|
4 | 5 |
|
5 | 6 | from ._text import FoldedCase
|
6 | 7 |
|
7 | 8 |
|
| 9 | +class RawPolicy(email.policy.EmailPolicy): |
| 10 | + def fold(self, name, value): |
| 11 | + folded = self.linesep.join( |
| 12 | + textwrap.indent(value, prefix=' ' * 8).lstrip().splitlines() |
| 13 | + ) |
| 14 | + return f'{name}: {folded}{self.linesep}' |
| 15 | + |
| 16 | + |
8 | 17 | class Message(email.message.Message):
|
| 18 | + r""" |
| 19 | + Specialized Message subclass to handle metadata naturally. |
| 20 | +
|
| 21 | + Reads values that may have newlines in them and converts the |
| 22 | + payload to the Description. |
| 23 | +
|
| 24 | + >>> msg_text = textwrap.dedent(''' |
| 25 | + ... Name: Foo |
| 26 | + ... Version: 3.0 |
| 27 | + ... License: blah |
| 28 | + ... de-blah |
| 29 | + ... <BLANKLINE> |
| 30 | + ... First line of description. |
| 31 | + ... Second line of description. |
| 32 | + ... ''').lstrip().replace('<BLANKLINE>', '') |
| 33 | + >>> msg = Message(email.message_from_string(msg_text)) |
| 34 | + >>> msg['Description'] |
| 35 | + 'First line of description.\nSecond line of description.\n' |
| 36 | +
|
| 37 | + Message should render even if values contain newlines. |
| 38 | +
|
| 39 | + >>> print(msg) |
| 40 | + Name: Foo |
| 41 | + Version: 3.0 |
| 42 | + License: blah |
| 43 | + de-blah |
| 44 | + Description: First line of description. |
| 45 | + Second line of description. |
| 46 | + <BLANKLINE> |
| 47 | + First line of description. |
| 48 | + Second line of description. |
| 49 | + <BLANKLINE> |
| 50 | + """ |
| 51 | + |
9 | 52 | multiple_use_keys = set(
|
10 | 53 | map(
|
11 | 54 | FoldedCase,
|
@@ -67,6 +110,9 @@ def redent(value):
|
67 | 110 | headers.append(('Description', self.get_payload()))
|
68 | 111 | return headers
|
69 | 112 |
|
| 113 | + def as_string(self): |
| 114 | + return super().as_string(policy=RawPolicy()) |
| 115 | + |
70 | 116 | @property
|
71 | 117 | def json(self):
|
72 | 118 | """
|
|
0 commit comments