From f21ddfb5762b9e6389be489f123ae7807bf645b4 Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Tue, 8 Mar 2022 18:22:29 -0500 Subject: [PATCH 1/3] No need to warn when falling back to other URL --- pytest_mpl/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_mpl/plugin.py b/pytest_mpl/plugin.py index 3891dbc1..11591806 100644 --- a/pytest_mpl/plugin.py +++ b/pytest_mpl/plugin.py @@ -62,7 +62,7 @@ def _download_file(baseline, filename): u = urlopen(base_url + filename) content = u.read() except Exception as e: - warnings.warn('Downloading {0} failed: {1}'.format(base_url + filename, e)) + pass else: break else: From adf58193144b2038d1474bd3cfdb144d4b3e4cc8 Mon Sep 17 00:00:00 2001 From: "Pey Lian Lim (Github)" <2090236+pllim@users.noreply.github.com> Date: Mon, 4 Apr 2022 16:37:15 -0400 Subject: [PATCH 2/3] Use logger --- pytest_mpl/plugin.py | 50 +++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/pytest_mpl/plugin.py b/pytest_mpl/plugin.py index 11591806..84861414 100644 --- a/pytest_mpl/plugin.py +++ b/pytest_mpl/plugin.py @@ -34,6 +34,7 @@ import shutil import hashlib import inspect +import logging import tempfile import warnings import contextlib @@ -54,27 +55,6 @@ {actual_path}""" -def _download_file(baseline, filename): - # Note that baseline can be a comma-separated list of URLs that we can - # then treat as mirrors - for base_url in baseline.split(','): - try: - u = urlopen(base_url + filename) - content = u.read() - except Exception as e: - pass - else: - break - else: - raise Exception("Could not download baseline image from any of the " - "available URLs") - result_dir = Path(tempfile.mkdtemp()) - filename = result_dir / 'downloaded' - with open(str(filename), 'wb') as tmpfile: - tmpfile.write(content) - return Path(filename) - - def _hash_file(in_stream): """ Hashes an already opened file. @@ -292,6 +272,12 @@ def __init__(self, self._test_results = {} self._test_stats = None + # https://stackoverflow.com/questions/51737378/how-should-i-log-in-my-pytest-plugin + # turn debug prints on only if "-vv" or more passed + level = logging.DEBUG if config.option.verbose > 1 else logging.INFO + logging.basicConfig(level=level) + self.logger = logging.getLogger('pytest-mpl') + def get_compare(self, item): """ Return the mpl_image_compare marker for the given item. @@ -364,6 +350,26 @@ def get_baseline_directory(self, item): return baseline_dir + def _download_file(self, baseline, filename): + # Note that baseline can be a comma-separated list of URLs that we can + # then treat as mirrors + for base_url in baseline.split(','): + try: + u = urlopen(base_url + filename) + content = u.read() + except Exception as e: + self.logger.debug(f'Downloading {base_url + filename} failed: {repr(e)}') + else: + break + else: + raise Exception("Could not download baseline image from any of the " + "available URLs") + result_dir = Path(tempfile.mkdtemp()) + filename = result_dir / 'downloaded' + with open(str(filename), 'wb') as tmpfile: + tmpfile.write(content) + return Path(filename) + def obtain_baseline_image(self, item, target_dir): """ Copy the baseline image to our working directory. @@ -378,7 +384,7 @@ def obtain_baseline_image(self, item, target_dir): if baseline_remote: # baseline_dir can be a list of URLs when remote, so we have to # pass base and filename to download - baseline_image = _download_file(baseline_dir, filename) + baseline_image = self._download_file(baseline_dir, filename) else: baseline_image = (baseline_dir / filename).absolute() From 146089b2162b296680a6520b15fdc28d234d1327 Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Wed, 20 Apr 2022 12:16:25 -0400 Subject: [PATCH 3/3] Update pytest_mpl/plugin.py Co-authored-by: Stuart Mumford --- pytest_mpl/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_mpl/plugin.py b/pytest_mpl/plugin.py index 84861414..b23f50a4 100644 --- a/pytest_mpl/plugin.py +++ b/pytest_mpl/plugin.py @@ -358,7 +358,7 @@ def _download_file(self, baseline, filename): u = urlopen(base_url + filename) content = u.read() except Exception as e: - self.logger.debug(f'Downloading {base_url + filename} failed: {repr(e)}') + self.logger.info(f'Downloading {base_url + filename} failed: {repr(e)}') else: break else: