Skip to content

Commit

Permalink
Use utf8 in the policy, allowing non-ascii to be rendered naturally.
Browse files Browse the repository at this point in the history
This approach required overriding EmailPolicy.header_store_parse to avoid crashing when saving the description. Closes #23
  • Loading branch information
jaraco committed Aug 31, 2024
1 parent e302161 commit d52d03b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import contextlib
import email.message
import email.policy
import functools
import importlib.metadata
import pathlib
Expand Down Expand Up @@ -41,6 +42,11 @@ def _(values: email.message.Message) -> Iterable[Tuple[str, str]]:
return values._headers


class Policy(email.policy.EmailPolicy):
def header_store_parse(self, name, value):
return (name, self.header_factory(name, value))


class Message(email.message.Message):
"""
>>> md = Message.discover()
Expand All @@ -49,11 +55,11 @@ class Message(email.message.Message):
>>> msg = Message({'Material': 'Kokuyōseki'})
>>> print(msg.render().strip())
Material: =?utf-8?q?Kokuy=C5=8Dseki?=
Material: Kokuyōseki
"""

def __init__(self, values):
super().__init__()
super().__init__(policy=Policy(utf8=True))
for item in always_items(values):
self.add_header(*item)

Expand Down

0 comments on commit d52d03b

Please sign in to comment.