From f3add2dab78a997832db3cc63632ab2c5b2e2666 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sun, 26 Dec 2021 17:18:06 +0700 Subject: Improve compliance --- _libs/formbox/format | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to '_libs') diff --git a/_libs/formbox/format b/_libs/formbox/format index 39e6132..bad40db 100755 --- a/_libs/formbox/format +++ b/_libs/formbox/format @@ -22,7 +22,7 @@ from functools import partial from itertools import starmap from mailbox import mbox from pathlib import Path -from urllib.parse import quote +from urllib.parse import quote, unquote from bleach import clean, linkify from markdown import markdown @@ -34,6 +34,7 @@ sanitise = partial(clean, tags=('a', 'code', 'em', 'strong', 'sub', 'sup', def extract(archive, parent): + """Recursively extract emails in reply to given message ID.""" for message_id, message in archive.copy().items(): # TODO: handle multipart if message['In-Reply-To'] != parent: continue @@ -42,12 +43,14 @@ def extract(archive, parent): def decode(header): + """Return the decoded email header.""" for string, charset in decode_header(header): encoding = 'utf-8' if charset is None else charset yield string.decode(encoding) def render(template, forest, parent): + """Render the thread recursively based on given template.""" for self, children in forest: message_id = self['Message-Id'] date = parsedate_to_datetime(self['Date']).date().isoformat() @@ -60,12 +63,14 @@ def render(template, forest, parent): children='\n'.join(rendered_children)) -parser = ArgumentParser() -parser.add_argument('mbox') -parser.add_argument('id') -parser.add_argument('template', type=Path) -args = parser.parse_args() +if __name__ == '__main__': + parser = ArgumentParser() + parser.add_argument('mbox') + parser.add_argument('id', type=unquote) + parser.add_argument('template', type=Path) + args = parser.parse_args() -archive = {m['Message-Id']: m for m in mbox(args.mbox)} -template = args.template.read_text() -print(*render(template, extract(archive, args.id), args.id), sep='', end='') + archive = {m['Message-Id']: m for m in mbox(args.mbox)} + template = args.template.read_text() + print(*render(template, extract(archive, args.id), args.id), + sep='', end='') -- cgit 1.4.1