From 20a46a30a2fb8b4df50792607c43e502bee82ad6 Mon Sep 17 00:00:00 2001 From: Martin Spacek Date: Thu, 22 Mar 2018 18:15:51 +0100 Subject: [PATCH 1/4] ENH: Stop mangling default figure file name if file exists If the file exists, the user is still prompted by the usual dialog box to decide whether or not to overwrite. The appended `-1`, `-2` are very annoying and not at all typical behaviour of a save dialog box. --- lib/matplotlib/backend_bases.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index fc92f6dd1869..0000f3416a01 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2275,17 +2275,6 @@ def get_default_filename(self): default_basename = default_basename.replace(' ', '_') default_filetype = self.get_default_filetype() default_filename = default_basename + '.' + default_filetype - - save_dir = os.path.expanduser(rcParams['savefig.directory']) - - # ensure non-existing filename in save dir - i = 1 - while os.path.isfile(os.path.join(save_dir, default_filename)): - # attach numerical count to basename - default_filename = ( - '{}-{}.{}'.format(default_basename, i, default_filetype)) - i += 1 - return default_filename def switch_backends(self, FigureCanvasClass): From c0132cc655bce8dc9bbc198b801f03fbf71b4e2d Mon Sep 17 00:00:00 2001 From: Martin Spacek Date: Tue, 27 Mar 2018 14:26:25 +0200 Subject: [PATCH 2/4] Remove test_get_default_filename_already_exists() --- lib/matplotlib/tests/test_backend_bases.py | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/lib/matplotlib/tests/test_backend_bases.py b/lib/matplotlib/tests/test_backend_bases.py index 0ab7e67a7666..3d0142f8996e 100644 --- a/lib/matplotlib/tests/test_backend_bases.py +++ b/lib/matplotlib/tests/test_backend_bases.py @@ -60,20 +60,3 @@ def test_get_default_filename(): assert filename == 'image.png' finally: shutil.rmtree(test_dir) - - -def test_get_default_filename_already_exists(): - # From #3068: Suggest non-existing default filename - try: - test_dir = tempfile.mkdtemp() - plt.rcParams['savefig.directory'] = test_dir - fig = plt.figure() - canvas = FigureCanvasBase(fig) - - # create 'image.png' in figure's save dir - open(os.path.join(test_dir, 'image.png'), 'w').close() - - filename = canvas.get_default_filename() - assert filename == 'image-1.png' - finally: - shutil.rmtree(test_dir) From 09af685f17ff0923966ceabb8ee9d72ac1609711 Mon Sep 17 00:00:00 2001 From: Martin Spacek Date: Tue, 27 Mar 2018 14:26:55 +0200 Subject: [PATCH 3/4] Enable overwrite confirmation in Gtk3 --- lib/matplotlib/backends/backend_gtk3.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/backends/backend_gtk3.py b/lib/matplotlib/backends/backend_gtk3.py index 0bbab45c0a8b..e5dcb4cbb3aa 100644 --- a/lib/matplotlib/backends/backend_gtk3.py +++ b/lib/matplotlib/backends/backend_gtk3.py @@ -592,6 +592,7 @@ def __init__(self, ): super().__init__(title, parent, action, buttons) self.set_default_response(Gtk.ResponseType.OK) + self.set_do_overwrite_confirmation(True) if not path: path = os.getcwd() From 84b9ca24fbef2cae1f08a562683c883c9135c936 Mon Sep 17 00:00:00 2001 From: Martin Spacek Date: Tue, 27 Mar 2018 20:13:05 +0200 Subject: [PATCH 4/4] Add entry to /next_whats_new --- .../next_whats_new/default_filename_suffix.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 doc/users/next_whats_new/default_filename_suffix.rst diff --git a/doc/users/next_whats_new/default_filename_suffix.rst b/doc/users/next_whats_new/default_filename_suffix.rst new file mode 100644 index 000000000000..0f8730b421e0 --- /dev/null +++ b/doc/users/next_whats_new/default_filename_suffix.rst @@ -0,0 +1,13 @@ +Stop adding a suffix to suggest unique file name +------------------------------------------------ + +Previously, when saving a figure to a file using the GUI's +save dialog box, if the default filename (based on the +figure window title) already existed on disk, Matplotlib +would append a suffix (e.g. `Figure_1-1.png`), preventing +the dialog from prompting to overwrite the file. This +behaviour has been removed. Now if the file name exists on +disk, the user is prompted whether or not to overwrite it. +This eliminates guesswork, and allows intentional +overwriting, especially when the figure name has been +manually set using `fig.canvas.set_window_title()`.