Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for passed reader contribution #301

Merged
merged 29 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bbfe154
Add check for passed reader contribution, rearrange and add more erro…
DragaDoncila Jun 27, 2023
34df97e
style: [pre-commit.ci] auto fixes [...]
pre-commit-ci[bot] Jun 28, 2023
9cb6771
Tests passing
DragaDoncila Jun 28, 2023
1cc6122
style: [pre-commit.ci] auto fixes [...]
pre-commit-ci[bot] Jun 28, 2023
a61f1a5
Fix contribution existence check
DragaDoncila Jun 28, 2023
0a9506a
Add more tests
DragaDoncila Jun 28, 2023
91a5f6c
style: [pre-commit.ci] auto fixes [...]
pre-commit-ci[bot] Jun 28, 2023
03407f3
Appease the ruff gods
DragaDoncila Jun 28, 2023
b632100
Merge branch 'check-contribs' of github.com:DragaDoncila/npe2 into ch…
DragaDoncila Jun 28, 2023
4a59c52
Add docs
DragaDoncila Jun 28, 2023
8267446
style: [pre-commit.ci] auto fixes [...]
pre-commit-ci[bot] Jun 28, 2023
1c8524a
Finish docs
DragaDoncila Jun 28, 2023
0a8ee65
Merge branch 'check-contribs' of github.com:DragaDoncila/npe2 into ch…
DragaDoncila Jun 28, 2023
3ca4a98
style: [pre-commit.ci] auto fixes [...]
pre-commit-ci[bot] Jun 28, 2023
7f88cdd
Only show command name if user passed a command
DragaDoncila Jun 28, 2023
31e8b32
Merge branch 'check-contribs' of github.com:DragaDoncila/npe2 into ch…
DragaDoncila Jun 28, 2023
b9405f1
Update src/npe2/io_utils.py
DragaDoncila Jun 29, 2023
6717548
style: [pre-commit.ci] auto fixes [...]
pre-commit-ci[bot] Jun 29, 2023
c32d6f0
Update src/npe2/io_utils.py
DragaDoncila Jun 29, 2023
ddfe5e8
Bring back no-redef
DragaDoncila Jun 29, 2023
5b233af
Add more tests
DragaDoncila Jun 29, 2023
2592a1d
Add one more test
DragaDoncila Jun 29, 2023
931818b
Merge branch 'main' into check-contribs
nclack Jul 5, 2023
e5a2e55
Merge branch 'main' into check-contribs
nclack Jul 5, 2023
6b153a1
Merge branch 'main' into check-contribs
nclack Jul 6, 2023
ab0a105
Update src/npe2/io_utils.py
DragaDoncila Jul 12, 2023
a9a9129
Update src/npe2/io_utils.py
DragaDoncila Jul 12, 2023
59ea554
Update assertion message and add tests
DragaDoncila Jul 12, 2023
659a68e
Merge branch 'main' into check-contribs
jni Jul 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/npe2/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,35 @@ def _read(
if _pm is None:
_pm = PluginManager.instance()

for rdr in _pm.iter_compatible_readers(paths):
if plugin_name and rdr.plugin_name != plugin_name:
continue
rdrs = list(_pm.iter_compatible_readers(paths))
if plugin_name:
# user might have passed 'plugin.reader_contrib' as the command
plugin, contrib = (
tuple(plugin_name.split(".")) if "." in plugin_name else (plugin_name, None)
)
chosen_compatible_readers = list(
filter(lambda rdr: rdr.plugin_name == plugin, rdrs)
)
if contrib:
chosen_compatible_readers = list(
filter(
lambda rdr: rdr.command.split(".")[1] == contrib,
chosen_compatible_readers,
)
)
if not chosen_compatible_readers:
raise ValueError(
f"Given reader '{plugin_name}' is not a compatible reader for {paths}. "
+ (
f"Available readers for {paths} are: {[rdr.command for rdr in rdrs]}"
if rdrs
else "No compatible readers are available."
)
)
else:
chosen_compatible_readers = rdrs

for rdr in chosen_compatible_readers:
read_func = rdr.exec(
kwargs={"path": paths, "stack": stack, "_registry": _pm.commands}
)
Expand Down
28 changes: 27 additions & 1 deletion tests/test__io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ def test_read(uses_sample_plugin):

def test_read_with_unknown_plugin(uses_sample_plugin):
# no such plugin name.... skips over the sample plugin & error is specific
with pytest.raises(ValueError, match="Plugin 'nope' was selected"):
with pytest.raises(
ValueError, match="Given reader 'nope' is not a compatible reader"
):
read(["some.fzzy"], plugin_name="nope", stack=False)


def test_read_with_unknown_plugin_no_readers(uses_sample_plugin):
with pytest.raises(
ValueError, match="Given reader 'nope' is not a compatible reader"
):
read(["some.nope"], plugin_name="nope", stack=False)
# assert 'No compatible readers are available.' in str(e)


def test_read_with_no_plugin():
# no plugin passed and none registered
with pytest.raises(ValueError, match="No readers returned"):
Expand Down Expand Up @@ -62,6 +72,22 @@ def read(paths):
io_utils._read(["some.fzzy"], plugin_name=short_name, stack=False, _pm=pm)


def test_read_with_reader_contribution_plugin(uses_sample_plugin):
assert read(
["some.fzzy"], stack=False, plugin_name=f"{SAMPLE_PLUGIN_NAME}.some_reader"
) == [(None,)]

# if the wrong contribution is passed we get useful error message
with pytest.raises(
ValueError,
match="Given reader 'my-plugin.not_a_reader' is not a compatible reader",
) as e:
read(
["some.fzzy"], stack=False, plugin_name=f"{SAMPLE_PLUGIN_NAME}.not_a_reader"
)
assert "Available readers for" in str(e)


def test_read_return_reader(uses_sample_plugin):
data, reader = read_get_reader("some.fzzy")
assert data == [(None,)]
Expand Down