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

Allow plugins sharing the same name to be overwritten #158

Merged
merged 3 commits into from
Mar 21, 2023

Conversation

kwilcox
Copy link
Member

@kwilcox kwilcox commented Mar 8, 2023

When configuring an xpublish server, I found it useful to be able to load the default entrypoint plugins and override a subset of them. For example, I didn't want the zarr plugin in the root namespace, I'd like it at /zarr. This allows plugins to be overwritten by name while still allowing xpublish to do its thing and discover entrypoint plugins.

rest = xpublish.Rest(ds)
zp = ZarrPlugin(dataset_router_prefix='/zarr')
rest.register_plugin(zp, overwrite=True)

While testing this I realized that DatasetInfoPlugin.name = 'dataset_info' but it was being added in entrypoints with the name info. There is no problem with this, except when a user like me wants to override the settings of a plugin by name 😆 and depending on how it was loaded, the default name changes. I changed the entrypoint to dataset_info... not going to die on this hill but it would be nice if the entrypoint name and the default plugin name were equal!

kwilcox added 3 commits March 8, 2023 12:07
This allows someone to load the default plugins and overwrite those that
they wish to by specifying an `overwrite=True` parameter to
`reguster_plugin`.
The DatasetInfoPlugin plugin will end up with two different names when
it is registered with its two "default" methods: by entrypoint and by
creating a DatasetInfoPlugin() object with no arguments. Having these
two fields equal helps downstream users who want to override the default
plugins. Without this, the users will have to know what the
entrypoint name is for discovered plugins so they can override the
correct plugin. By standardizing on the name of the plugin, it can be
inferred.
Copy link
Member

@abkfenris abkfenris left a comment

Choose a reason for hiding this comment

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

That looks pretty good to me. There are a few documentation things, but I've spun those out into their own issues.

Let me know when you are happy with it and I'll land it.

'info = xpublish.plugins.included.dataset_info:DatasetInfoPlugin',
'dataset_info = xpublish.plugins.included.dataset_info:DatasetInfoPlugin',
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for catching and fixing that mismatch.

Comment on lines +46 to +49
# Registering a duplicate plugin name will fail
same_name = DatasetInfoPlugin(name='dataset_info', dataset_router_prefix='/newinfo')
with pytest.raises(ValueError):
rest.register_plugin(same_name)
Copy link
Member

Choose a reason for hiding this comment

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

Should we have a separate test to catch the error with the existing functionality?

Copy link
Member Author

Choose a reason for hiding this comment

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

Up to you, I can break this test down into smaller tests if you would like!

Copy link
Member

Choose a reason for hiding this comment

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

I think this works for now.

Comment on lines +130 to +144
"""
Register a plugin with the xpublish system

Args:
plugin (Plugin): Instantiated Plugin object
plugin_name (str, optional): Plugin name
overwrite (bool, optional): If a plugin of the same name exist,
setting this to True will remove the existing plugin before
registering the new plugin. Defaults to False.

Raises:
AttributeError: Plugin can not be registered
ValueError: Plugin already registered, try setting overwrite to True
"""
plugin_name = plugin_name or plugin.name
Copy link
Member

Choose a reason for hiding this comment

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

Raising the bar on documentation! Right now it doesn't render into the docs, so I've made a new issue to add other methods to the docs #160. It also made me think we should use the type hints directly so we don't need to re-specify them in the docstrings #161.

@abkfenris abkfenris merged commit cef6cd2 into xpublish-community:main Mar 21, 2023
@abkfenris
Copy link
Member

Thanks Kyle!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants