]> git.madduck.net Git - etc/neomutt.git/commitdiff

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: remove quickndirty file io hack
authormartin f. krafft <madduck@madduck.net>
Sun, 3 Sep 2023 20:36:43 +0000 (22:36 +0200)
committermartin f. krafft <madduck@madduck.net>
Sun, 3 Sep 2023 20:36:43 +0000 (22:36 +0200)
.config/neomutt/buildmimetree.py

index f5e3c17ae74512fada4032db862cb72cf29317f1..c541ed12094e9ff1c22ab147e5eb9824ff9b6f73 100755 (executable)
@@ -231,10 +231,6 @@ class File:
         if cache and self._cache[File.Op.R]:
             return self._get_cache(File.Op.R)
 
         if cache and self._cache[File.Op.R]:
             return self._get_cache(File.Op.R)
 
-        if not self._file:
-            with self as f:
-                return f.read(cache=cache)
-
         if self._lastop == File.Op.W:
             try:
                 self._file.seek(0)
         if self._lastop == File.Op.W:
             try:
                 self._file.seek(0)
@@ -251,10 +247,6 @@ class File:
 
     def write(self, s, *, cache=True):
 
 
     def write(self, s, *, cache=True):
 
-        if not self._file:
-            with self as f:
-                return f.write(s, cache=cache)
-
         if self._lastop == File.Op.R:
             try:
                 self._file.seek(0)
         if self._lastop == File.Op.R:
             try:
                 self._file.seek(0)
@@ -644,7 +636,9 @@ def extract_signature(text, *, filefactory=FileFactory()):
         path = pathlib.Path(re.split(r" +", lines.pop(0), maxsplit=1)[1])
         textsig = "\n".join(lines)
 
         path = pathlib.Path(re.split(r" +", lines.pop(0), maxsplit=1)[1])
         textsig = "\n".join(lines)
 
-        sig_input = filefactory(path.expanduser()).read()
+        with filefactory(path.expanduser()) as sig_f:
+            sig_input = sig_f.read()
+
         soup = bs4.BeautifulSoup(sig_input, "html.parser")
 
         style = str(soup.style.extract()) if soup.style else ""
         soup = bs4.BeautifulSoup(sig_input, "html.parser")
 
         style = str(soup.style.extract()) if soup.style else ""
@@ -1537,10 +1531,10 @@ try:
 
         @pytest.mark.converter
         def test_converter_tree_basic(self, fakepath, const1, fakefilefactory):
 
         @pytest.mark.converter
         def test_converter_tree_basic(self, fakepath, const1, fakefilefactory):
-            draft_f = fakefilefactory(fakepath, content=const1)
-            tree = convert_markdown_to_html(
-                draft_f, filefactory=fakefilefactory
-            )
+            with fakefilefactory(fakepath, content=const1) as draft_f:
+                tree = convert_markdown_to_html(
+                    draft_f, filefactory=fakefilefactory
+                )
 
             assert tree.subtype == "alternative"
             assert len(tree.children) == 2
 
             assert tree.subtype == "alternative"
             assert len(tree.children) == 2
@@ -1554,8 +1548,8 @@ try:
         def test_converter_writes(
             self, fakepath, fakefilefactory, const1, monkeypatch
         ):
         def test_converter_writes(
             self, fakepath, fakefilefactory, const1, monkeypatch
         ):
-            draft_f = fakefilefactory(fakepath, content=const1)
-            convert_markdown_to_html(draft_f, filefactory=fakefilefactory)
+            with fakefilefactory(fakepath, content=const1) as draft_f:
+                convert_markdown_to_html(draft_f, filefactory=fakefilefactory)
 
             html = fakefilefactory.pop()
             assert fakepath.with_suffix(".html") == html[0]
 
             html = fakefilefactory.pop()
             assert fakepath.with_suffix(".html") == html[0]
@@ -1749,7 +1743,6 @@ try:
             @pytest.mark.styling
             def test_massage_styling_to_converter(self):
                 css = "p { color:red }"
             @pytest.mark.styling
             def test_massage_styling_to_converter(self):
                 css = "p { color:red }"
-                css_f = File(content=css)
                 css_applied = []
 
                 def converter(draft_f, css_f, **kwargs):
                 css_applied = []
 
                 def converter(draft_f, css_f, **kwargs):
@@ -1757,12 +1750,17 @@ try:
                     css_applied.append(css)
                     return Part("text", "plain", draft_f.path, orig=True)
 
                     css_applied.append(css)
                     return Part("text", "plain", draft_f.path, orig=True)
 
-                do_massage(
-                    draft_f=File(),
-                    cmd_f=File(),
-                    css_f=css_f,
-                    converter=converter,
-                )
+                with (
+                    File() as draft_f,
+                    File(mode="w") as cmd_f,
+                    File(content=css) as css_f
+                ):
+                    do_massage(
+                        draft_f=draft_f,
+                        cmd_f=cmd_f,
+                        css_f=css_f,
+                        converter=converter,
+                    )
                 assert css_applied[0] == css
 
             @pytest.mark.converter
                 assert css_applied[0] == css
 
             @pytest.mark.converter
@@ -1831,11 +1829,10 @@ try:
             assert htmlsig == sigconst.format(path=fakepath)
 
         @pytest.mark.sig
             assert htmlsig == sigconst.format(path=fakepath)
 
         @pytest.mark.sig
-        def test_signature_extraction_file_not_found(self, const1):
-            path = pathlib.Path("/does/not/exist")
+        def test_signature_extraction_file_not_found(self, fakepath, const1):
             with pytest.raises(FileNotFoundError):
                 origtext, textsig, htmlsig = extract_signature(
             with pytest.raises(FileNotFoundError):
                 origtext, textsig, htmlsig = extract_signature(
-                    f"{const1}{EMAIL_SIG_SEP}{HTML_SIG_MARKER}{path}\n{const1}"
+                    f"{const1}{EMAIL_SIG_SEP}{HTML_SIG_MARKER}{fakepath}\n{const1}"
                 )
 
         @pytest.mark.imgproc
                 )
 
         @pytest.mark.imgproc
@@ -2109,6 +2106,11 @@ try:
                 f.write(const1, cache=False)
                 assert f.read(cache=False) == const1
 
                 f.write(const1, cache=False)
                 assert f.read(cache=False) == const1
 
+        @pytest.mark.fileio
+        def test_file_class_path_no_exists(self, fakepath):
+            with pytest.raises(FileNotFoundError):
+                File(fakepath, mode="r").open()
+
         @pytest.mark.fileio
         def test_file_class_cache(self, tmp_path, const1, const2):
             path = tmp_path / "file"
         @pytest.mark.fileio
         def test_file_class_cache(self, tmp_path, const1, const2):
             path = tmp_path / "file"