From 402c30c4b3b360bf7919103580b8d08b84adf7a4 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 12 Oct 2023 09:03:21 -0400 Subject: [PATCH] refactor: easier to let tempfile delete the file I'm not sure why I set delete=False and then handled deletion myself... --- src/scriv/format_rst.py | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/scriv/format_rst.py b/src/scriv/format_rst.py index d52b084..7280690 100644 --- a/src/scriv/format_rst.py +++ b/src/scriv/format_rst.py @@ -1,6 +1,5 @@ """reStructuredText knowledge for scriv.""" -import os import re import tempfile from typing import Optional @@ -145,24 +144,20 @@ def format_sections( def convert_to_markdown( self, text: str ) -> str: # noqa: D102 (inherited docstring) - rst_file = None - try: - with tempfile.NamedTemporaryFile( - mode="w", prefix="scriv_rst_", delete=False - ) as rst_file: - rst_file.write(text) - rst_file.flush() - ok, output = run_command( - "pandoc -frst -tmarkdown_strict " - + "--markdown-headings=atx --wrap=none " - + "--fail-if-warnings " - + rst_file.name + with tempfile.NamedTemporaryFile( + mode="w", + prefix="scriv_rst_", + ) as rst_file: + rst_file.write(text) + rst_file.flush() + ok, output = run_command( + "pandoc -frst -tmarkdown_strict " + + "--markdown-headings=atx --wrap=none " + + "--fail-if-warnings " + + rst_file.name + ) + if not ok: + raise ScrivException( + f"Couldn't convert ReST to Markdown: {output!r}" ) - if not ok: - raise ScrivException( - f"Couldn't convert ReST to Markdown: {output!r}" - ) - return output.replace("\r\n", "\n") - finally: - if rst_file is not None: - os.unlink(rst_file.name) + return output.replace("\r\n", "\n")