From afb47983ed77b7fac519e797170db7a781c73d1b Mon Sep 17 00:00:00 2001
From: Remi  PLANEL <rplanel@pasteur.fr>
Date: Thu, 28 Mar 2024 17:59:35 +0100
Subject: [PATCH] write in tmp file instead

---
 .../df-wiki-cli/df_wiki_cli/content/main.py   | 25 ++++++++-----------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/packages/df-wiki-cli/df_wiki_cli/content/main.py b/packages/df-wiki-cli/df_wiki_cli/content/main.py
index 751e7d48..319089d3 100644
--- a/packages/df-wiki-cli/df_wiki_cli/content/main.py
+++ b/packages/df-wiki-cli/df_wiki_cli/content/main.py
@@ -4,6 +4,7 @@ import json
 import pandas as pd
 import shutil
 import csv
+import tempfile
 import matplotlib.pyplot as plt
 from pandas.errors import ParserError
 from typing_extensions import Annotated
@@ -426,30 +427,26 @@ def markdown(
             help="Dir where all systems article are",
         ),
     ],
-    # output: Annotated[
-    #     Path,
-    #     typer.Option(
-    #         file_okay=True,
-    #         dir_okay=False,
-    #         writable=True,
-    #         resolve_path=True,
-    #     ),
-    # ],
 ):
-
     for file in dir.iterdir():
         if file.suffix == ".md":
 
             console.rule(f"[bold blue]{file.name}", style="blue")
-            with open(file, "r+") as f:
+            # make a copy of file
+            _, tmp_path = tempfile.mkstemp()
+            # with open(dst, "w") as tmp_f:
+            dst = Path(tmp_path)
+            dst.write_bytes(file.read_bytes())
+
+            with open(dst, "r+") as f:
                 new_f = re.sub(
-                    r"##\s+Structure\n.+##\s+Experimental\s+validation",
+                    r"##\s+Structure\n.*##\s+Experimental\s+validation",
                     "## Structure\n\n::article-structure\n::\n\n## Experimental validation",
                     f.read(),
                     flags=re.DOTALL,
                 )
-                f.seek(0, 0)
-                f.write(new_f)
+                with open(file, "w") as f_out:
+                    f_out.write(new_f)
 
 
 def remove_version(assembly):
-- 
GitLab