From 0e9efa35ae52c78d194662087da2355807a1ce96 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 24 Jan 2022 08:40:00 -0600 Subject: [PATCH] clean up plot dir handling --- oct2py/ipython/octavemagic.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/oct2py/ipython/octavemagic.py b/oct2py/ipython/octavemagic.py index 5fab7f6a..c074421b 100644 --- a/oct2py/ipython/octavemagic.py +++ b/oct2py/ipython/octavemagic.py @@ -40,7 +40,7 @@ #----------------------------------------------------------------------------- import os -import tempfile +import shutil import oct2py @@ -246,14 +246,21 @@ def octave(self, line, cell=None, local_ns=None): if args.size is not None: width, height = [int(s) for s in args.size.split(',')] - plot_dir_obj = tempfile.TemporaryDirectory() - plot_dir = plot_dir_obj.name - + # Handle the temporary directory, defaulting to the Oct2Py instance's + # temp dir. temp_dir = args.temp_dir - if temp_dir is not None and not os.path.isdir(temp_dir): + if temp_dir and not os.path.isdir(temp_dir): temp_dir = None + temp_dir = temp_dir or self._oct.temp_dir + + # Put the plots in the temp directory so we don't have to make another + # temporary directory. + plot_dir = os.path.join(temp_dir, 'plots') + if os.path.exists(plot_dir): + shutil.rmtree(plot_dir) + os.makedirs(plot_dir) - # match current working directory + # Match current working directory. self._oct.cd(os.getcwd().replace(os.path.sep, '/')) value = self._oct.eval(code, stream_handler=self._publish, plot_dir=plot_dir, plot_width=width, plot_height=height,