about summary refs log tree commit diff homepage
path: root/_libs/formbox
diff options
context:
space:
mode:
Diffstat (limited to '_libs/formbox')
-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='')