3434import shutil
3535import hashlib
3636import inspect
37+ import logging
3738import tempfile
3839import warnings
3940import contextlib
5455 {actual_path}"""
5556
5657
57- def _download_file (baseline , filename ):
58- # Note that baseline can be a comma-separated list of URLs that we can
59- # then treat as mirrors
60- for base_url in baseline .split (',' ):
61- try :
62- u = urlopen (base_url + filename )
63- content = u .read ()
64- except Exception as e :
65- warnings .warn ('Downloading {0} failed: {1}' .format (base_url + filename , e ))
66- else :
67- break
68- else :
69- raise Exception ("Could not download baseline image from any of the "
70- "available URLs" )
71- result_dir = Path (tempfile .mkdtemp ())
72- filename = result_dir / 'downloaded'
73- with open (str (filename ), 'wb' ) as tmpfile :
74- tmpfile .write (content )
75- return Path (filename )
76-
77-
7858def _hash_file (in_stream ):
7959 """
8060 Hashes an already opened file.
@@ -292,6 +272,12 @@ def __init__(self,
292272 self ._test_results = {}
293273 self ._test_stats = None
294274
275+ # https://stackoverflow.com/questions/51737378/how-should-i-log-in-my-pytest-plugin
276+ # turn debug prints on only if "-vv" or more passed
277+ level = logging .DEBUG if config .option .verbose > 1 else logging .INFO
278+ logging .basicConfig (level = level )
279+ self .logger = logging .getLogger ('pytest-mpl' )
280+
295281 def get_compare (self , item ):
296282 """
297283 Return the mpl_image_compare marker for the given item.
@@ -364,6 +350,26 @@ def get_baseline_directory(self, item):
364350
365351 return baseline_dir
366352
353+ def _download_file (self , baseline , filename ):
354+ # Note that baseline can be a comma-separated list of URLs that we can
355+ # then treat as mirrors
356+ for base_url in baseline .split (',' ):
357+ try :
358+ u = urlopen (base_url + filename )
359+ content = u .read ()
360+ except Exception as e :
361+ self .logger .info (f'Downloading { base_url + filename } failed: { repr (e )} ' )
362+ else :
363+ break
364+ else :
365+ raise Exception ("Could not download baseline image from any of the "
366+ "available URLs" )
367+ result_dir = Path (tempfile .mkdtemp ())
368+ filename = result_dir / 'downloaded'
369+ with open (str (filename ), 'wb' ) as tmpfile :
370+ tmpfile .write (content )
371+ return Path (filename )
372+
367373 def obtain_baseline_image (self , item , target_dir ):
368374 """
369375 Copy the baseline image to our working directory.
@@ -378,7 +384,7 @@ def obtain_baseline_image(self, item, target_dir):
378384 if baseline_remote :
379385 # baseline_dir can be a list of URLs when remote, so we have to
380386 # pass base and filename to download
381- baseline_image = _download_file (baseline_dir , filename )
387+ baseline_image = self . _download_file (baseline_dir , filename )
382388 else :
383389 baseline_image = (baseline_dir / filename ).absolute ()
384390
0 commit comments