@@ -31,17 +31,9 @@ def make_test_filename(fname, purpose):
31
31
32
32
33
33
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 )
45
37
46
38
47
39
def get_file_hash (path , block_size = 2 ** 20 ):
@@ -53,10 +45,10 @@ def get_file_hash(path, block_size=2 ** 20):
53
45
break
54
46
md5 .update (data )
55
47
56
- if path . endswith ( '.pdf' ) :
48
+ if Path ( path ). suffix == '.pdf' :
57
49
md5 .update (str (mpl ._get_executable_info ("gs" ).version )
58
50
.encode ('utf-8' ))
59
- elif path . endswith ( '.svg' ) :
51
+ elif Path ( path ). suffix == '.svg' :
60
52
md5 .update (str (mpl ._get_executable_info ("inkscape" ).version )
61
53
.encode ('utf-8' ))
62
54
@@ -263,37 +255,32 @@ def convert(filename, cache):
263
255
hash of the exact contents of the input file. There is no limit on the
264
256
size of the cache, so it may need to be manually cleared periodically.
265
257
"""
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 :
268
262
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"
273
265
274
266
# Only convert the file if the destination doesn't already exist or
275
267
# 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
282
270
283
271
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
290
277
291
- converter [extension ]( filename , newname )
278
+ converter [path . suffix [ 1 :]]( path , newpath )
292
279
293
280
if cache_dir is not None :
294
- shutil .copyfile (newname , cached_file )
281
+ shutil .copyfile (newpath , cached_path )
295
282
296
- return newname
283
+ return str ( newpath )
297
284
298
285
299
286
def crop_to_same (actual_path , actual_image , expected_path , expected_image ):
0 commit comments