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 plugin_manager module for global singleton convenience #164

Merged
merged 8 commits into from
Jun 12, 2022

Conversation

tlambert03
Copy link
Collaborator

Inspired by some conversations with @kne42 at the hackathon, this PR adds a new module that serves as a convenience for accessing public methods on the global PluginManager.instance() singleton.

Everything is lazy, so importing the module does nothing to instantiate the singleton. I avoided fully autogenerating the module so as to retain type hinting and basic docs (though the full docstrings with parameters will remain in the actual PluginManager methods).

This would make a lot of the lines in napari/plugins/_npe2.py and elsewhere simpler. for example:

pm = npe2.PluginManager.instance()
for plugin_name, contribs in pm.iter_sample_data():
   ...

# becomes
for plugin_name, contribs in npe2.pm.iter_sample_data():
    ...

The only downside is that the pm.py module needs to be kept manually updated (but there's a test to let us know when we need to).

thoughts @kne42, @nclack?

@codecov
Copy link

codecov bot commented May 22, 2022

Codecov Report

Merging #164 (a26e45a) into main (a483aea) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main      #164   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           26        27    +1     
  Lines         1730      1770   +40     
=========================================
+ Hits          1730      1770   +40     
Impacted Files Coverage Δ
npe2/_plugin_manager.py 100.00% <ø> (ø)
npe2/plugin_manager.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a483aea...a26e45a. Read the comment docs.

@nclack
Copy link
Collaborator

nclack commented May 25, 2022

I'm not a big fan of using pm as a name - I tend to want to spell things out rather than abbreviate. What do you think about making the singleton transparent so things look like: npe2.iter_sample_data()?

@tlambert03
Copy link
Collaborator Author

I'm not married to pm. Doing it in the root namespace is definitely aesthetically appealing, but it scares me to use that namespace 😂! I guess I could live with from npe2 import plugin_manager as pm or something like that?

@nclack
Copy link
Collaborator

nclack commented May 30, 2022

root namespace scares me too 👻 plugin_manager looks great.

@tlambert03
Copy link
Collaborator Author

Will merge this tommorrow with new module name. Unless @nclack you think we shouldn’t have it at all? (You never commented on that)

Copy link
Collaborator

@nclack nclack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like the idea of the global plugin_manager instance.

@@ -471,9 +484,17 @@ def iter_compatible_readers(
def iter_compatible_writers(
self, layer_types: Sequence[str]
) -> Iterator[WriterContribution]:
"""Iterate over compatible WriterContributions given a sequence of layer_types.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love all the improved docstrings!

@tlambert03 tlambert03 changed the title Add pm module for global singleton convenience Add plugin_manager module for global singleton convenience Jun 12, 2022
@tlambert03 tlambert03 merged commit 6b1cd1e into napari:main Jun 12, 2022
@tlambert03 tlambert03 deleted the convenience-pm branch June 12, 2022 12:57
@tlambert03 tlambert03 added the enhancement New feature or request label Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants