Skip to content

Commit

Permalink
Merge pull request #2931 from pnuu/clarify-missing-reader-dependency-…
Browse files Browse the repository at this point in the history
…logging

Enhance visibility of missing dependencies
  • Loading branch information
mraspaud authored Oct 17, 2024
2 parents 07d2c00 + 3db6547 commit 5ee0729
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
49 changes: 34 additions & 15 deletions satpy/readers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,31 +557,24 @@ def load_readers(filenames=None, reader=None, reader_kwargs=None):
reader, filenames, remaining_filenames = _get_reader_and_filenames(reader, filenames)
(reader_kwargs, reader_kwargs_without_filter) = _get_reader_kwargs(reader, reader_kwargs)

for idx, reader_configs in enumerate(configs_for_reader(reader)):
if isinstance(filenames, dict):
readers_files = set(filenames[reader[idx]])
else:
readers_files = remaining_filenames
if reader_kwargs is None:
reader_kwargs = {}

try:
reader_instance = load_reader(
reader_configs,
**reader_kwargs[None if reader is None else reader[idx]])
except (KeyError, IOError, yaml.YAMLError) as err:
LOG.info("Cannot use %s", str(reader_configs))
LOG.debug(str(err))
for idx, reader_configs in enumerate(configs_for_reader(reader)):
readers_files = _get_readers_files(filenames, reader, idx, remaining_filenames)
reader_instance = _get_reader_instance(reader, reader_configs, idx, reader_kwargs)
if reader_instance is None or not readers_files:
# Reader initiliasation failed or no files were given
continue

if not readers_files:
# we weren't given any files for this reader
continue
loadables = reader_instance.select_files_from_pathnames(readers_files)
if loadables:
reader_instance.create_storage_items(
loadables,
fh_kwargs=reader_kwargs_without_filter[None if reader is None else reader[idx]])
reader_instances[reader_instance.name] = reader_instance
remaining_filenames -= set(loadables)

if not remaining_filenames:
break

Expand All @@ -590,6 +583,32 @@ def load_readers(filenames=None, reader=None, reader_kwargs=None):
return reader_instances


def _get_readers_files(filenames, reader, idx, remaining_filenames):
if isinstance(filenames, dict):
return set(filenames[reader[idx]])
return remaining_filenames


def _get_reader_instance(reader, reader_configs, idx, reader_kwargs):
reader_instance = None
try:
reader_instance = load_reader(
reader_configs,
**reader_kwargs[None if reader is None else reader[idx]])
except (KeyError, IOError) as err:
LOG.info("Cannot use %s", str(reader_configs))
LOG.debug(str(err))
except yaml.constructor.ConstructorError as err:
_log_yaml_error(reader_configs, err)

return reader_instance


def _log_yaml_error(reader_configs, err):
LOG.error("Problem with %s", str(reader_configs))
LOG.error(str(err))


def _early_exit(filenames, reader):
if not filenames and not reader:
# used for an empty Scene
Expand Down
31 changes: 31 additions & 0 deletions satpy/tests/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ class TestReaderLoader(unittest.TestCase):
Assumes that the VIIRS SDR reader exists and works.
"""

@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog, tmp_path): # noqa: PT004
"""Inject caplog to the test class."""
self._caplog = caplog
self._tmp_path = tmp_path

def setUp(self):
"""Wrap HDF5 file handler with our own fake handler."""
from satpy.readers.viirs_sdr import VIIRSSDRFileHandler
Expand Down Expand Up @@ -439,6 +445,31 @@ def test_almost_all_filtered(self):
assert "abi_l1b" in readers
assert len(list(readers["abi_l1b"].available_dataset_ids)) == 0

def test_yaml_error_message(self):
"""Test that YAML errors are logged properly."""
import logging

import satpy
from satpy.readers import load_readers

reader_config = "reader:\n"
reader_config += " name: nonreader\n"
reader_config += " reader: !!python/name:notapackage.notareader.BadClass\n"

os.mkdir(self._tmp_path / "readers")
reader_fname = self._tmp_path / "readers" / "nonreader.yaml"
with open(reader_fname, "w") as fid:
fid.write(reader_config)

filenames = ["foo.bar"]
error_message = "No module named 'notapackage'"

with self._caplog.at_level(logging.ERROR):
with satpy.config.set({"config_path": [str(self._tmp_path)]}):
with pytest.raises(ValueError, match="No supported files found"):
_ = load_readers(filenames=filenames, reader="nonreader")
assert error_message in self._caplog.text


class TestFindFilesAndReaders:
"""Test the find_files_and_readers utility function."""
Expand Down

0 comments on commit 5ee0729

Please sign in to comment.