From d52d03bd3b4e33403dc7c10509efe51c50224ef9 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 31 Aug 2024 17:38:40 -0400 Subject: [PATCH] Use utf8 in the policy, allowing non-ascii to be rendered naturally. This approach required overriding EmailPolicy.header_store_parse to avoid crashing when saving the description. Closes #23 --- metadata.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/metadata.py b/metadata.py index a06e753..0617453 100644 --- a/metadata.py +++ b/metadata.py @@ -2,6 +2,7 @@ import contextlib import email.message +import email.policy import functools import importlib.metadata import pathlib @@ -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() @@ -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)