38
38
# these two places.
39
39
40
40
BASELINE_IMAGES = [
41
- os . path . join ('lib' , ' matplotlib' , ' tests' , ' baseline_images' ),
42
- os . path . join ('lib' , ' mpl_toolkits' , ' tests' , ' baseline_images' )
43
- ]
41
+ Path ('lib/ matplotlib/ tests/ baseline_images' ),
42
+ Path ('lib/ mpl_toolkits/ tests/ baseline_images' ),
43
+ ]
44
44
45
45
46
46
# Non-png image extensions
@@ -172,7 +172,7 @@ def set_entry(self, index):
172
172
173
173
self .pixmaps = []
174
174
for fname , thumbnail in zip (entry .thumbnails , self .thumbnails ):
175
- pixmap = QtGui .QPixmap (fname )
175
+ pixmap = QtGui .QPixmap (os . fspath ( fname ) )
176
176
scaled_pixmap = pixmap .scaled (
177
177
thumbnail .size (), QtCore .Qt .KeepAspectRatio ,
178
178
QtCore .Qt .SmoothTransformation )
@@ -185,8 +185,9 @@ def set_entry(self, index):
185
185
def set_large_image (self , index ):
186
186
self .thumbnails [self .current_thumbnail ].setFrameShape (0 )
187
187
self .current_thumbnail = index
188
- pixmap = QtGui .QPixmap (self .entries [self .current_entry ]
189
- .thumbnails [self .current_thumbnail ])
188
+ pixmap = QtGui .QPixmap (os .fspath (
189
+ self .entries [self .current_entry ]
190
+ .thumbnails [self .current_thumbnail ]))
190
191
self .image_display .setPixmap (pixmap )
191
192
self .thumbnails [self .current_thumbnail ].setFrameShape (1 )
192
193
@@ -236,11 +237,11 @@ class Entry(object):
236
237
def __init__ (self , path , root , source ):
237
238
self .source = source
238
239
self .root = root
239
- self .dir , fname = os . path .split ( path )
240
- self .reldir = os . path .relpath ( self . dir , self . root )
241
- self .diff = fname
240
+ self .dir = path .parent
241
+ self .diff = path .name
242
+ self .reldir = self . dir . relative_to ( self . root )
242
243
243
- basename = fname [:- len ('-failed-diff.png' )]
244
+ basename = self . diff [:- len ('-failed-diff.png' )]
244
245
for ext in exts :
245
246
if basename .endswith ('_' + ext ):
246
247
display_extension = '_' + ext
@@ -258,46 +259,33 @@ def __init__(self, path, root, source):
258
259
self .expected_display = (basename + '-expected' + display_extension +
259
260
'.png' )
260
261
self .generated_display = basename + display_extension + '.png'
261
- self .name = os . path . join ( self .reldir , self .basename )
262
+ self .name = self .reldir / self .basename
262
263
self .destdir = self .get_dest_dir (self .reldir )
263
264
264
265
self .thumbnails = [
265
266
self .generated_display ,
266
267
self .expected_display ,
267
268
self .diff
268
269
]
269
- self .thumbnails = [os . path . join ( self .dir , x ) for x in self .thumbnails ]
270
+ self .thumbnails = [self .dir / x for x in self .thumbnails ]
270
271
271
272
if not Path (self .destdir , self .generated ).exists ():
272
273
# This case arises from a check_figures_equal test.
273
274
self .status = 'autogen'
274
- elif self . same ( os . path . join (self .dir , self .generated ),
275
- os . path . join (self .destdir , self .generated )):
275
+ elif ( (self .dir / self .generated ). read_bytes ()
276
+ == (self .destdir / self .generated ). read_bytes ( )):
276
277
self .status = 'accept'
277
278
else :
278
279
self .status = 'unknown'
279
280
280
- def same (self , a , b ):
281
- """
282
- Returns True if two files have the same content.
283
- """
284
- return Path (a ).read_bytes () == Path (b ).read_bytes ()
285
-
286
- def copy_file (self , a , b ):
287
- """
288
- Copy file from a to b.
289
- """
290
- print ("copying: {} to {}" .format (a , b ))
291
- shutil .copyfile (a , b )
292
-
293
281
def get_dest_dir (self , reldir ):
294
282
"""
295
283
Find the source tree directory corresponding to the given
296
284
result_images subdirectory.
297
285
"""
298
286
for baseline_dir in BASELINE_IMAGES :
299
- path = os . path . join ( self .source , baseline_dir , reldir )
300
- if os . path .isdir ( path ):
287
+ path = self .source / baseline_dir / reldir
288
+ if path .is_dir ( ):
301
289
return path
302
290
raise ValueError ("Can't find baseline dir for {}" .format (reldir ))
303
291
@@ -320,30 +308,30 @@ def accept(self):
320
308
"""
321
309
Accept this test by copying the generated result to the source tree.
322
310
"""
323
- a = os .path .join (self .dir , self .generated )
324
- b = os .path .join (self .destdir , self .generated )
325
- self .copy_file (a , b )
311
+ copy_file (self .dir / self .generated , self .destdir / self .generated )
326
312
self .status = 'accept'
327
313
328
314
def reject (self ):
329
315
"""
330
316
Reject this test by copying the expected result to the source tree.
331
317
"""
332
- a = os .path .join (self .dir , self .expected )
333
- b = os .path .join (self .destdir , self .generated )
334
- self .copy_file (a , b )
318
+ copy_file (self .dir / self .expected , self .destdir / self .generated )
335
319
self .status = 'reject'
336
320
337
321
322
+ def copy_file (a , b ):
323
+ """Copy file from *a* to *b*."""
324
+ print (f'copying: { a } to { b } ' )
325
+ shutil .copyfile (a , b )
326
+
327
+
338
328
def find_failing_tests (result_images , source ):
339
329
"""
340
330
Find all of the failing tests by looking for files with
341
331
`-failed-diff` at the end of the basename.
342
332
"""
343
- return sorted (
344
- (Entry (path , result_images , source )
345
- for path in Path (result_images ).glob ("**/*-failed-diff.*" )),
346
- key = lambda x : x .name )
333
+ return [Entry (path , result_images , source )
334
+ for path in sorted (Path (result_images ).glob ("**/*-failed-diff.*" ))]
347
335
348
336
349
337
def launch (result_images , source ):
@@ -367,7 +355,7 @@ def launch(result_images, source):
367
355
if __name__ == '__main__' :
368
356
import argparse
369
357
370
- source_dir = os . path . join ( os . path . dirname ( __file__ ), '..' )
358
+ source_dir = Path ( __file__ ). parent . parent
371
359
372
360
parser = argparse .ArgumentParser (
373
361
formatter_class = argparse .RawDescriptionHelpFormatter ,
@@ -383,10 +371,10 @@ def launch(result_images, source):
383
371
A: Accept test. Copy the test result to the source tree.
384
372
R: Reject test. Copy the expected result to the source tree.
385
373
""" )
386
- parser .add_argument ("result_images" , type = str , nargs = '?' ,
387
- default = os . path . join ( source_dir , 'result_images' ) ,
374
+ parser .add_argument ("result_images" , type = Path , nargs = '?' ,
375
+ default = source_dir / 'result_images' ,
388
376
help = "The location of the result_images directory" )
389
- parser .add_argument ("source" , type = str , nargs = '?' , default = source_dir ,
377
+ parser .add_argument ("source" , type = Path , nargs = '?' , default = source_dir ,
390
378
help = "The location of the matplotlib source tree" )
391
379
args = parser .parse_args ()
392
380
0 commit comments