Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 51576d4

Browse filesBrowse files
committed
Merge pull request #4221 from umairidris/default_filename
ENH : Suggest non-existing default filename in gui save window closes #3608
2 parents 481d820 + 85f472a commit 51576d4
Copy full SHA for 51576d4

File tree

Expand file treeCollapse file tree

2 files changed

+60
-3
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+60
-3
lines changed

‎lib/matplotlib/backend_bases.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backend_bases.py
+15-3Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,9 +2253,21 @@ def get_default_filename(self):
22532253
Return a string, which includes extension, suitable for use as
22542254
a default filename.
22552255
"""
2256-
default_filename = self.get_window_title() or 'image'
2257-
default_filename = default_filename.lower().replace(' ', '_')
2258-
return default_filename + '.' + self.get_default_filetype()
2256+
default_basename = self.get_window_title() or 'image'
2257+
default_basename = default_basename.lower().replace(' ', '_')
2258+
default_filetype = self.get_default_filetype()
2259+
default_filename = default_basename + '.' + default_filetype
2260+
2261+
save_dir = os.path.expanduser(rcParams.get('savefig.directory', ''))
2262+
2263+
# ensure non-existing filename in save dir
2264+
i = 1
2265+
while os.path.isfile(os.path.join(save_dir, default_filename)):
2266+
# attach numerical count to basename
2267+
default_filename = '{0}-{1}.{2}'.format(default_basename, i, default_filetype)
2268+
i += 1
2269+
2270+
return default_filename
22592271

22602272
def switch_backends(self, FigureCanvasClass):
22612273
"""

‎lib/matplotlib/tests/test_backend_bases.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_backend_bases.py
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
from matplotlib.backend_bases import FigureCanvasBase
12
from matplotlib.backend_bases import RendererBase
3+
from matplotlib.testing.decorators import image_comparison, cleanup
4+
5+
import matplotlib.pyplot as plt
26
import matplotlib.transforms as transforms
37
import matplotlib.path as path
8+
9+
from nose.tools import assert_equal
10+
411
import numpy as np
12+
import os
13+
import shutil
14+
import tempfile
515

616

717
def test_uses_per_path():
@@ -43,3 +53,38 @@ def check(master_transform, paths, all_transforms,
4353
check(id, paths, tforms, offsets, facecolors, [])
4454
check(id, paths, tforms, offsets, [], [])
4555
check(id, paths, tforms, offsets, facecolors[0:1], edgecolors)
56+
57+
58+
@cleanup
59+
def test_get_default_filename():
60+
try:
61+
test_dir = tempfile.mkdtemp()
62+
plt.rcParams['savefig.directory'] = test_dir
63+
fig = plt.figure()
64+
canvas = FigureCanvasBase(fig)
65+
filename = canvas.get_default_filename()
66+
assert_equal(filename, 'image.png')
67+
finally:
68+
shutil.rmtree(test_dir)
69+
70+
71+
@cleanup
72+
def test_get_default_filename_already_exists():
73+
# From #3068: Suggest non-existing default filename
74+
try:
75+
test_dir = tempfile.mkdtemp()
76+
plt.rcParams['savefig.directory'] = test_dir
77+
fig = plt.figure()
78+
canvas = FigureCanvasBase(fig)
79+
80+
# create 'image.png' in figure's save dir
81+
open(os.path.join(test_dir, 'image.png'), 'w').close()
82+
83+
filename = canvas.get_default_filename()
84+
assert_equal(filename, 'image-1.png')
85+
finally:
86+
shutil.rmtree(test_dir)
87+
88+
if __name__ == "__main__":
89+
import nose
90+
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.