From 5828c92c6693414301639ab5b84dc75529fe27ab Mon Sep 17 00:00:00 2001 From: Vadim Yanitskiy Date: Tue, 31 Mar 2026 05:15:09 +0700 Subject: [PATCH] filesystem: JsonEditor: use NamedTemporaryFile A plain NamedTemporaryFile is sufficient here: we only need a single file, not a directory to hold it. Using NamedTemporaryFile is simpler (no subdirectory to manage) and gives us a .json suffix for free, which editors use for syntax highlighting. Change-Id: If3b0bd0fcc90732407dbd03b9cc883f7abeb948e --- pySim/filesystem.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pySim/filesystem.py b/pySim/filesystem.py index 9cd2f232..e273361f 100644 --- a/pySim/filesystem.py +++ b/pySim/filesystem.py @@ -30,6 +30,7 @@ import tempfile import json import abc import inspect +import os import cmd2 from cmd2 import CommandSet, with_default_category @@ -567,7 +568,7 @@ class JsonEditor: self._cmd = cmd self._orig_json = orig_json self._ef = ef - self._tmpdir = None + self._file = None @staticmethod def _strip_comments(text: str) -> str: @@ -606,18 +607,21 @@ class JsonEditor: text_file.write(f'// {line}\n') def __enter__(self) -> object: - """Write JSON + examples to a temp file, run the editor, return parsed result.""" - self._tmpdir = tempfile.TemporaryDirectory(prefix='pysim_') - filename = '%s/file' % self._tmpdir.name - with open(filename, 'w') as text_file: - json.dump(self._orig_json, text_file, indent=4, cls=JsonEncoder) - self._append_examples_as_comments(text_file) - self._cmd.run_editor(filename) - with open(filename, 'r') as text_file: + """Write JSON + examples to a temp file, run the editor, return parsed result. + + The temp file is kept on JSONDecodeError so the user can correct and + re-open it manually. It is removed by __exit__() on success.""" + self._file = tempfile.NamedTemporaryFile(prefix='pysim_', suffix='.json', + mode='w', delete=False) + json.dump(self._orig_json, self._file, indent=4, cls=JsonEncoder) + self._append_examples_as_comments(self._file) + self._file.close() + self._cmd.run_editor(self._file.name) + with open(self._file.name, 'r') as text_file: return json.loads(self._strip_comments(text_file.read())) def __exit__(self, *args): - self._tmpdir.cleanup() + os.unlink(self._file.name) class CardEF(CardFile):