diff options
| author | robot-piglet <[email protected]> | 2025-02-04 18:15:06 +0300 |
|---|---|---|
| committer | robot-piglet <[email protected]> | 2025-02-04 21:07:27 +0300 |
| commit | b70e82eebea77757f33e6b77b6f1512cc326bfc6 (patch) | |
| tree | 237352cd06b6a51e8b5dd410ad4ed45a0c2ef414 /contrib/python/importlib-metadata/py3/importlib_metadata/_adapters.py | |
| parent | 8741caac7b0e574bd23431ba43e68ec1c9fc2a31 (diff) | |
Intermediate changes
commit_hash:7515000bb2a1a163da5e4501067f274b33ca619d
Diffstat (limited to 'contrib/python/importlib-metadata/py3/importlib_metadata/_adapters.py')
| -rw-r--r-- | contrib/python/importlib-metadata/py3/importlib_metadata/_adapters.py | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/contrib/python/importlib-metadata/py3/importlib_metadata/_adapters.py b/contrib/python/importlib-metadata/py3/importlib_metadata/_adapters.py index 3b516a2d066..f5b30dd92cd 100644 --- a/contrib/python/importlib-metadata/py3/importlib_metadata/_adapters.py +++ b/contrib/python/importlib-metadata/py3/importlib_metadata/_adapters.py @@ -1,11 +1,58 @@ import email.message +import email.policy import re import textwrap from ._text import FoldedCase +class RawPolicy(email.policy.EmailPolicy): + def fold(self, name, value): + folded = self.linesep.join( + textwrap.indent(value, prefix=' ' * 8, predicate=lambda line: True) + .lstrip() + .splitlines() + ) + return f'{name}: {folded}{self.linesep}' + + class Message(email.message.Message): + r""" + Specialized Message subclass to handle metadata naturally. + + Reads values that may have newlines in them and converts the + payload to the Description. + + >>> msg_text = textwrap.dedent(''' + ... Name: Foo + ... Version: 3.0 + ... License: blah + ... de-blah + ... <BLANKLINE> + ... First line of description. + ... Second line of description. + ... <BLANKLINE> + ... Fourth line! + ... ''').lstrip().replace('<BLANKLINE>', '') + >>> msg = Message(email.message_from_string(msg_text)) + >>> msg['Description'] + 'First line of description.\nSecond line of description.\n\nFourth line!\n' + + Message should render even if values contain newlines. + + >>> print(msg) + Name: Foo + Version: 3.0 + License: blah + de-blah + Description: First line of description. + Second line of description. + <BLANKLINE> + Fourth line! + <BLANKLINE> + <BLANKLINE> + """ + multiple_use_keys = set( map( FoldedCase, @@ -57,15 +104,20 @@ class Message(email.message.Message): def _repair_headers(self): def redent(value): "Correct for RFC822 indentation" - if not value or '\n' not in value: + indent = ' ' * 8 + if not value or '\n' + indent not in value: return value - return textwrap.dedent(' ' * 8 + value) + return textwrap.dedent(indent + value) headers = [(key, redent(value)) for key, value in vars(self)['_headers']] if self._payload: headers.append(('Description', self.get_payload())) + self.set_payload('') return headers + def as_string(self): + return super().as_string(policy=RawPolicy()) + @property def json(self): """ |
