Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f95b0f

Browse files
committed
Cleanup testing.compare.
- In get_cache_dir(), we can assume that get_cachedir always returns a writable directory (we make the same assumption e.g. in texmanager, for example), basically since 244e8d8. - pathlibify convert().
1 parent 9a24fb7 commit 2f95b0f

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,9 @@ def make_test_filename(fname, purpose):
3131

3232

3333
def get_cache_dir():
34-
cachedir = mpl.get_cachedir()
35-
if cachedir is None:
36-
raise RuntimeError('Could not find a suitable configuration directory')
37-
cache_dir = os.path.join(cachedir, 'test_cache')
38-
try:
39-
Path(cache_dir).mkdir(parents=True, exist_ok=True)
40-
except IOError:
41-
return None
42-
if not os.access(cache_dir, os.W_OK):
43-
return None
44-
return cache_dir
34+
cache_dir = Path(mpl.get_cachedir(), 'test_cache')
35+
cache_dir.mkdir(parents=True, exist_ok=True)
36+
return str(cache_dir)
4537

4638

4739
def get_file_hash(path, block_size=2 ** 20):
@@ -53,10 +45,10 @@ def get_file_hash(path, block_size=2 ** 20):
5345
break
5446
md5.update(data)
5547

56-
if path.endswith('.pdf'):
48+
if Path(path).suffix == '.pdf':
5749
md5.update(str(mpl._get_executable_info("gs").version)
5850
.encode('utf-8'))
59-
elif path.endswith('.svg'):
51+
elif Path(path).suffix == '.svg':
6052
md5.update(str(mpl._get_executable_info("inkscape").version)
6153
.encode('utf-8'))
6254

@@ -263,37 +255,32 @@ def convert(filename, cache):
263255
hash of the exact contents of the input file. There is no limit on the
264256
size of the cache, so it may need to be manually cleared periodically.
265257
"""
266-
base, extension = os.fspath(filename).rsplit('.', 1)
267-
if extension not in converter:
258+
path = Path(filename)
259+
if not path.exists():
260+
raise IOError(f"{path} does not exist")
261+
if path.suffix[1:] not in converter:
268262
import pytest
269-
pytest.skip(f"Don't know how to convert {extension} files to png")
270-
newname = base + '_' + extension + '.png'
271-
if not os.path.exists(filename):
272-
raise IOError("'%s' does not exist" % filename)
263+
pytest.skip(f"Don't know how to convert {path.suffix} files to png")
264+
newpath = path.parent / f"{path.stem}_{path.suffix}.png"
273265

274266
# Only convert the file if the destination doesn't already exist or
275267
# is out of date.
276-
if (not os.path.exists(newname) or
277-
os.stat(newname).st_mtime < os.stat(filename).st_mtime):
278-
if cache:
279-
cache_dir = get_cache_dir()
280-
else:
281-
cache_dir = None
268+
if not newpath.exists() or newpath.stat().st_mtime < path.stat().st_mtime:
269+
cache_dir = Path(get_cache_dir()) if cache else None
282270

283271
if cache_dir is not None:
284-
hash_value = get_file_hash(filename)
285-
new_ext = os.path.splitext(newname)[1]
286-
cached_file = os.path.join(cache_dir, hash_value + new_ext)
287-
if os.path.exists(cached_file):
288-
shutil.copyfile(cached_file, newname)
289-
return newname
272+
hash_value = get_file_hash(path)
273+
cached_path = cache_dir / (hash_value + newpath.suffix)
274+
if cached_path.exists():
275+
shutil.copyfile(cached_path, newpath)
276+
return newpath
290277

291-
converter[extension](filename, newname)
278+
converter[path.suffix[1:]](path, newpath)
292279

293280
if cache_dir is not None:
294-
shutil.copyfile(newname, cached_file)
281+
shutil.copyfile(newpath, cached_path)
295282

296-
return newname
283+
return str(newpath)
297284

298285

299286
def crop_to_same(actual_path, actual_image, expected_path, expected_image):

0 commit comments

Comments
 (0)