43
43
44
44
import pytest
45
45
46
- from .kernels import DEFAULT_HAMMING_TOLERANCE , DEFAULT_HASH_SIZE , KERNEL_SHA256 , kernel_factory
46
+ from .kernels import (DEFAULT_HAMMING_TOLERANCE , DEFAULT_HASH_SIZE ,
47
+ DEFAULT_HIGH_FREQUENCY_FACTOR , KERNEL_SHA256 , kernel_factory )
47
48
from .summary .html import generate_summary_basic_html , generate_summary_html
48
49
49
50
#: The default matplotlib backend.
67
68
#: Valid formats for generate summary.
68
69
SUPPORTED_FORMATS = {'html' , 'json' , 'basic-html' }
69
70
71
+ #: Supported INET protocols.
72
+ PROTOCOLS = ('http://' , 'https://' )
73
+
70
74
#: Template error message for image shape conformance.
71
75
TEMPLATE_SHAPE_MISMATCH = """Error! Image dimensions did not match.
72
76
@@ -150,22 +154,31 @@ def pytest_addoption(parser):
150
154
151
155
msg = ('Directory for test results, relative to location where py.test '
152
156
'is run.' )
153
- group .addoption ('--mpl-results-path' , help = msg , action = 'store' )
154
- parser .addini ('mpl-results-path' , help = msg )
157
+ option = 'mpl-results-path'
158
+ group .addoption (f'--{ option } ' , help = msg , action = 'store' )
159
+ parser .addini (option , help = msg )
155
160
156
161
msg = ('Always compare to baseline images and save result images, even '
157
162
'for passing tests. This option is automatically applied when '
158
163
'generating a HTML summary.' )
159
- group .addoption ('--mpl-results-always' , help = msg , action = 'store_true' )
160
- parser .addini ('mpl-results-always' , help = msg )
164
+ option = 'mpl-results-always'
165
+ group .addoption (f'--{ option } ' , help = msg , action = 'store_true' )
166
+ parser .addini (option , help = msg )
161
167
162
168
msg = 'Use fully qualified test name as the filename.'
163
169
parser .addini ('mpl-use-full-test-name' , help = msg , type = 'bool' )
164
170
165
171
msg = ('Algorithm to be used for hashing images. Supported kernels are '
166
172
'`sha256` (default) and `phash`.' )
167
- group .addoption ('--mpl-kernel' , help = msg , action = 'store' )
168
- parser .addini ('mpl-kernel' , help = msg )
173
+ option = 'mpl-kernel'
174
+ group .addoption (f'--{ option } ' , help = msg , action = 'store' )
175
+ parser .addini (option , help = msg )
176
+
177
+ msg = ('Determine the level of image detail (high) or structure (low)'
178
+ 'represented in the perceptual hash.' )
179
+ option = 'mpl-high-freq-factor'
180
+ group .addoption (f'--{ option } ' , help = msg , action = 'store' )
181
+ parser .addini (option , help = msg )
169
182
170
183
msg = 'The hash size (N) used to generate a N**2 bit image hash.'
171
184
group .addoption ('--mpl-hash-size' , help = msg , action = 'store' )
@@ -211,8 +224,7 @@ def pytest_configure(config):
211
224
"--mpl-generate-path is set" )
212
225
warnings .warn (wmsg )
213
226
214
- protocols = ("https" , "http" )
215
- if baseline_dir is not None and not baseline_dir .startswith (protocols ):
227
+ if baseline_dir is not None and not baseline_dir .startswith (PROTOCOLS ):
216
228
baseline_dir = os .path .abspath (baseline_dir )
217
229
if generate_dir is not None :
218
230
baseline_dir = os .path .abspath (generate_dir )
@@ -249,8 +261,9 @@ def switch_backend(backend):
249
261
250
262
251
263
def close_mpl_figure (fig ):
252
- "Close a given matplotlib Figure. Any other type of figure is ignored"
253
-
264
+ """
265
+ Close a given matplotlib Figure. Any other type of figure is ignored
266
+ """
254
267
import matplotlib .pyplot as plt
255
268
from matplotlib .figure import Figure
256
269
@@ -264,11 +277,12 @@ def close_mpl_figure(fig):
264
277
265
278
def get_marker (item , marker_name ):
266
279
if hasattr (item , 'get_closest_marker' ):
267
- return item .get_closest_marker (marker_name )
280
+ result = item .get_closest_marker (marker_name )
268
281
else :
269
282
# "item.keywords.get" was deprecated in pytest 3.6
270
283
# See https://docs.pytest.org/en/latest/mark.html#updating-code
271
- return item .keywords .get (marker_name )
284
+ result = item .keywords .get (marker_name )
285
+ return result
272
286
273
287
274
288
def path_is_not_none (apath ):
@@ -308,24 +322,30 @@ def __init__(self,
308
322
self .results_always = results_always
309
323
310
324
# Configure hashing kernel options.
311
- option = " mpl-hash-size"
312
- hash_size = int (config .getoption (f" --{ option } " ) or
325
+ option = ' mpl-hash-size'
326
+ hash_size = int (config .getoption (f' --{ option } ' ) or
313
327
config .getini (option ) or DEFAULT_HASH_SIZE )
314
328
self .hash_size = hash_size
315
329
316
- option = " mpl-hamming-tolerance"
317
- hamming_tolerance = int (config .getoption (f" --{ option } " ) or
330
+ option = ' mpl-hamming-tolerance'
331
+ hamming_tolerance = int (config .getoption (f' --{ option } ' ) or
318
332
config .getini (option ) or
319
333
DEFAULT_HAMMING_TOLERANCE )
320
334
self .hamming_tolerance = hamming_tolerance
321
335
336
+ option = 'mpl-high-freq-factor'
337
+ high_freq_factor = int (config .getoption (f'--{ option } ' ) or
338
+ config .getini (option ) or
339
+ DEFAULT_HIGH_FREQUENCY_FACTOR )
340
+ self .high_freq_factor = high_freq_factor
341
+
322
342
# Configure the hashing kernel - must be done *after* kernel options.
323
- option = " mpl-kernel"
324
- kernel = config .getoption (f" --{ option } " ) or config .getini (option )
343
+ option = ' mpl-kernel'
344
+ kernel = config .getoption (f' --{ option } ' ) or config .getini (option )
325
345
if kernel :
326
346
requested = str (kernel ).lower ()
327
347
if requested not in kernel_factory :
328
- emsg = f" Unrecognised hashing kernel { kernel !r} not supported."
348
+ emsg = f' Unrecognised hashing kernel { kernel !r} not supported.'
329
349
raise ValueError (emsg )
330
350
kernel = requested
331
351
else :
@@ -421,7 +441,7 @@ def get_baseline_directory(self, item):
421
441
baseline_dir = self .baseline_dir
422
442
423
443
baseline_remote = (isinstance (baseline_dir , str ) and # noqa
424
- baseline_dir .startswith (( 'http://' , 'https://' ) ))
444
+ baseline_dir .startswith (PROTOCOLS ))
425
445
if not baseline_remote :
426
446
return Path (item .fspath ).parent / baseline_dir
427
447
@@ -457,7 +477,7 @@ def obtain_baseline_image(self, item, target_dir):
457
477
filename = self .generate_filename (item )
458
478
baseline_dir = self .get_baseline_directory (item )
459
479
baseline_remote = (isinstance (baseline_dir , str ) and # noqa
460
- baseline_dir .startswith (( 'http://' , 'https://' ) ))
480
+ baseline_dir .startswith (PROTOCOLS ))
461
481
if baseline_remote :
462
482
# baseline_dir can be a list of URLs when remote, so we have to
463
483
# pass base and filename to download
0 commit comments