about summary refs log tree commit diff homepage
path: root/_libs
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2021-12-26 17:18:06 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-12-26 17:18:06 +0700
commitf3add2dab78a997832db3cc63632ab2c5b2e2666 (patch)
treeef73faba1208c858cba233017b79b8071dac8135 /_libs
parent1d42abf23d59de9684c80c290c4498d81ce9977b (diff)
downloadsite-f3add2dab78a997832db3cc63632ab2c5b2e2666.tar.gz
Improve compliance
Diffstat (limited to '_libs')
-rwxr-xr-x_libs/formbox/format23
1 files changed, 14 insertions, 9 deletions
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='')