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 DynamicPlugin object/context for ease of testing & dynamic plugin creation #128

Merged
merged 14 commits into from
Mar 14, 2022

Conversation

tlambert03
Copy link
Collaborator

@tlambert03 tlambert03 commented Mar 13, 2022

was starting to work on some stuff over in napari/napari#4015 and realized we need a better way to inject temporary functionality into a test plugin manager. This adds a DynamicPlugin context manager that controls a manifest, and allows you to register commands with decorators:

from npe2 import DynamicPlugin

with DynamicPlugin() as plugin:
    @plugin.contribute.sample_data
    def make_image(x):
        ...

it will handle creation of a command, and the corresponding contribution type, and inject it into the plugin manifest and reindex. The decorator takes all the same parameters that the corresponding contribution would take:

    @plugin.contribute.writer(layer_types=["image"])
    def write_path(path, layer_data):
        ...

it cleans up after itself when leaving the context

edit: this also modifies a small amount of logic on the unregistering/deactivation of plugins in general, specifically around unregistering their commands

@tlambert03 tlambert03 requested review from nclack and Carreau March 13, 2022 22:23
@codecov
Copy link

codecov bot commented Mar 13, 2022

Codecov Report

Merging #128 (453cd9e) into main (41c557d) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##              main      #128    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           23        24     +1     
  Lines         1355      1483   +128     
==========================================
+ Hits          1355      1483   +128     
Impacted Files Coverage Δ
npe2/__init__.py 100.00% <100.00%> (ø)
npe2/_command_registry.py 100.00% <100.00%> (ø)
npe2/_dynamic_plugin.py 100.00% <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 41c557d...453cd9e. Read the comment docs.

@tlambert03 tlambert03 requested a review from jni March 13, 2022 22:26
npe2/_command_registry.py Outdated Show resolved Hide resolved
Co-authored-by: Nathan Clack <nclack@gmail.com>
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.

This is pretty interesting as a way of dynamically adding a plugin. I wonder if you think it'll be useful outside of testing?

@tlambert03
Copy link
Collaborator Author

I wonder if you think it'll be useful outside of testing?

I do! This PR started as a _testsupport module with a PluginFixture class... and as I realized the generality of it, I pulled it out into something different. It has a pattern that is not dissimilar to #75 in terms of associating a callable with a command and a contribution. As it currently stands, I think it's a relatively flexible base to use in many scenarios. (If you think a name other than TemporaryPlugin would help indicate that, I'm open :)

@tlambert03 tlambert03 changed the title Add TemporaryPlugin object/context for ease of testing Add TemporaryPlugin object/context for ease of testing & dynamic plugin creation Mar 14, 2022
@tlambert03
Copy link
Collaborator Author

perhaps DynamicPlugin?

@tlambert03
Copy link
Collaborator Author

ok to merge if I change name to DynamicPlugin @nclack ?

@nclack
Copy link
Collaborator

nclack commented Mar 14, 2022

ok to merge

Yes. Meant to approve before anyway.

@tlambert03 tlambert03 changed the title Add TemporaryPlugin object/context for ease of testing & dynamic plugin creation Add DynamicPlugin object/context for ease of testing & dynamic plugin creation Mar 14, 2022
@tlambert03 tlambert03 merged commit 915c810 into napari:main Mar 14, 2022
@tlambert03 tlambert03 deleted the temp-plugin branch March 14, 2022 19:35
@tlambert03 tlambert03 added the enhancement New feature or request label Apr 5, 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