]> git.madduck.net Git - etc/neomutt.git/blobdiff - .config/neomutt/buildmimetree.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

buildmimetree.py: file IO outside do_massage
[etc/neomutt.git] / .config / neomutt / buildmimetree.py
index 83cbe4d204beb515ae6018716b093c0e408ca794..d033bda2f9a1e7f2ae7ede1e4406514de62f5771 100755 (executable)
@@ -72,6 +72,12 @@ def parse_cli_args(*args, **kwargs):
         help="Markdown extension to add to the list of extensions use",
     )
 
+    parser_setup.add_argument(
+        "--send-message",
+        action="store_true",
+        help="Generate command(s) to send the message after processing",
+    )
+
     parser_massage.add_argument(
         "--debug-commands",
         action="store_true",
@@ -134,26 +140,31 @@ class Multipart(
         return f"<multipart/{self.subtype}> children={len(self.children)}"
 
 
-def convert_markdown_to_html(maildraft, *, extensions=None):
-    draftpath = pathlib.Path(maildraft)
+
+def convert_markdown_to_html(
+    origtext, draftpath, *, filewriter_fn=None, extensions=None
+):
+    mdwn = markdown.Markdown(extensions=extensions)
+
+    if not filewriter_fn:
+
+        def filewriter_fn(path, content, mode="w", **kwargs):
+            with open(path, mode, **kwargs) as out_f:
+                out_f.write(content)
+
+    filewriter_fn(draftpath, origtext, encoding="utf-8")
     textpart = Part(
         "text", "plain", draftpath, "Plain-text version", orig=True
     )
 
-    with open(draftpath, "r", encoding="utf-8") as textmarkdown:
-        text = textmarkdown.read()
-
-    mdwn = markdown.Markdown(extensions=extensions)
-    html = mdwn.convert(text)
+    htmltext = mdwn.convert(origtext)
 
     htmlpath = draftpath.with_suffix(".html")
+    filewriter_fn(
+        htmlpath, htmltext, encoding="utf-8", errors="xmlcharrefreplace"
+    )
     htmlpart = Part("text", "html", htmlpath, "HTML version")
 
-    with open(
-        htmlpath, "w", encoding="utf-8", errors="xmlcharrefreplace"
-    ) as texthtml:
-        texthtml.write(html)
-
     logopart = Part(
         "image",
         "png",
@@ -293,6 +304,8 @@ def do_setup(
     editor = f"{sys.argv[0]} massage --write-commands-to {temppath}"
     if extensions:
         editor = f'{editor} --extensions {",".join(extensions)}'
+    if debug_commands:
+        editor = f'{editor} --debug-commands'
 
     cmds.cmd('set my_editor="$editor"')
     cmds.cmd('set my_edit_headers="$edit_headers"')
@@ -304,7 +317,8 @@ def do_setup(
 
 
 def do_massage(
-    maildraft,
+    draft_f,
+    draftpath,
     cmd_f,
     *,
     extensions=None,
@@ -331,7 +345,7 @@ def do_massage(
     cmds.flush()
 
     extensions = extensions.split(",") if extensions else []
-    tree = converter(maildraft, extensions=extensions)
+    tree = converter(draft_f.read(), draftpath, extensions=extensions)
 
     mimetree = MIMETreeDFWalker(debug=debug_walk)
 
@@ -345,19 +359,23 @@ def do_massage(
             # We've hit a leaf-node, i.e. an alternative or a related part
             # with actual content.
 
-            # If the part is not an original part, i.e. doesn't already
-            # exist, we must first add it.
-            if not item.orig:
+            # Let's add the part
+            if item.orig:
+                # The original source already exists in the NeoMutt tree, but
+                # the underlying file may have been modified, so we need to
+                # update the encoding, but that's it:
+                cmds.push("<update-encoding>")
+            else:
+                # … whereas all other parts need to be added, and they're all
+                # considered to be temporary and inline:
                 cmds.push(f"<attach-file>{item.path}<enter>")
                 cmds.push("<toggle-unlink><toggle-disposition>")
-                if item.cid:
-                    cmds.push(f"<edit-content-id>\\Ca\\Ck{item.cid}<enter>")
 
-            # If the item (including the original) comes with a
-            # description, then we might just as well update the NeoMutt
+            # If the item (including the original) comes with additional
+            # information, then we might just as well update the NeoMutt
             # tree now:
-            if item.desc:
-                cmds.push(f"<edit-description>\\Ca\\Ck{item.desc}<enter>")
+            if item.cid:
+                cmds.push(f"<edit-content-id>\\Ca\\Ck{item.cid}<enter>")
 
         elif isinstance(item, Multipart):
             # This node has children, but we already visited them (see
@@ -371,14 +389,14 @@ def do_massage(
             elif item.subtype == "multilingual":
                 cmds.push("<group-multilingual>")
 
-            # Again, if there is a description, we might just as well:
-            if item.desc:
-                cmds.push(f"<edit-description>\\Ca\\Ck{item.desc}<enter>")
-
         else:
             # We should never get here
             assert not "is valid part"
 
+        # If the item has a description, we might just as well add it
+        if item.desc:
+            cmds.push(f"<edit-description>\\Ca\\Ck{item.desc}<enter>")
+
         # Finally, if we're at non-root level, tag the new container,
         # as it might itself be part of a container, to be processed
         # one level up:
@@ -409,12 +427,18 @@ if __name__ == "__main__":
     args = parse_cli_args()
 
     if args.mode == "setup":
+        if args.send_message:
+            raise NotImplementedError()
+
         do_setup(args.extensions, debug_commands=args.debug_commands)
 
     elif args.mode == "massage":
-        with open(args.cmdpath, "w") as cmd_f:
+        with open(args.MAILDRAFT, "r") as draft_f, open(
+            args.cmdpath, "w"
+        ) as cmd_f:
             do_massage(
-                args.MAILDRAFT,
+                draft_f,
+                pathlib.Path(args.MAILDRAFT),
                 cmd_f,
                 extensions=args.extensions,
                 debug_commands=args.debug_commands,
@@ -426,6 +450,7 @@ if __name__ == "__main__":
 
 try:
     import pytest
+    from io import StringIO
 
     class Tests:
         @pytest.fixture
@@ -490,7 +515,13 @@ try:
                     Multipart(
                         "alternative",
                         children=[
-                            Part("text", "plain", "part.txt", desc="Plain"),
+                            Part(
+                                "text",
+                                "plain",
+                                "part.txt",
+                                desc="Plain",
+                                orig=True,
+                            ),
                             Part("text", "html", "part.html", desc="HTML"),
                         ],
                         desc="Alternative",
@@ -525,7 +556,6 @@ try:
 
         def test_MIMETreeDFWalker_list_to_mixed(self, basic_mime_tree):
             mimetree = MIMETreeDFWalker()
-
             items = []
 
             def visitor_fn(item, stack, debugprint):
@@ -567,5 +597,62 @@ try:
             assert lines[2].endswith(f'{const2},{const1}"')
             assert lines[4].endswith(const1)
 
+        @pytest.fixture
+        def string_io(self, const1, text=None):
+            return StringIO(text or const1)
+
+        def test_do_massage_basic(self, const1, string_io, capsys):
+            def converter(drafttext, draftpath, extensions):
+                return Part("text", "plain", draftpath, orig=True)
+
+            do_massage(
+                draft_f=string_io,
+                draftpath=const1,
+                cmd_f=sys.stdout,
+                converter=converter,
+            )
+
+            captured = capsys.readouterr()
+            lines = captured.out.splitlines()
+            assert '="$my_editor"' in lines.pop(0)
+            assert '="$my_edit_headers"' in lines.pop(0)
+            assert "unset my_editor" == lines.pop(0)
+            assert "unset my_edit_headers" == lines.pop(0)
+            assert "update-encoding" in lines.pop(0)
+            assert "source 'rm -f " in lines.pop(0)
+            assert "unset my_mdwn_postprocess_cmd_file" == lines.pop(0)
+
+        def test_do_massage_fulltree(
+            self, string_io, const1, basic_mime_tree, capsys
+        ):
+            def converter(drafttext, draftpath, extensions):
+                return basic_mime_tree
+
+            do_massage(
+                draft_f=string_io,
+                draftpath=const1,
+                cmd_f=sys.stdout,
+                converter=converter,
+            )
+
+            captured = capsys.readouterr()
+            lines = captured.out.splitlines()[4:][::-1]
+            assert "Related" in lines.pop()
+            assert "group-related" in lines.pop()
+            assert "tag-entry" in lines.pop()
+            assert "Logo" in lines.pop()
+            assert "content-id" in lines.pop()
+            assert "toggle-unlink" in lines.pop()
+            assert "logo.png" in lines.pop()
+            assert "tag-entry" in lines.pop()
+            assert "Alternative" in lines.pop()
+            assert "group-alternatives" in lines.pop()
+            assert "tag-entry" in lines.pop()
+            assert "HTML" in lines.pop()
+            assert "toggle-unlink" in lines.pop()
+            assert "part.html" in lines.pop()
+            assert "tag-entry" in lines.pop()
+            assert "Plain" in lines.pop()
+
 except ImportError:
     pass