Hi, I'm trying to implement pluggy into my app. I went through the docs, read "toy example" and "complete example", tried my own example implementation and implemented pluggy into my app. I have a question if I understand PluginManager correctly. The "toy example" shows that in order to register plugins to host you have to have direct access to the PM instance. The "complete example" shows that plugin doesn't need to have direct access to PM instance, the PM is just in the host and will scan available python packages and load plugins from them. Is that right?
My use case is somewhere in the middle - I'd like to register hooks from the plugin (not from host) via PM that I can instantiate using just the project_name. I was wondering - is there a way to use PM as a singleton / module variable / global variable so I can instantiate it just with the project_name anywhere in the code without having direct access to the PM instance that was created in the host app? When I read the docs, I thought it could be possible but I found out it's probably not.
What I tried to achieve in the plugin code:
import pluggy; pm = pluggy.PluginManager('my_app') # class MyPlugin1...; class MyPlugin2 pm.register(MyPlugin1()); pm.register(MyPlugin2()) # Now app (host) that uses "my_app" as a name for pluggy plugins, knows about my hooks.
@RonnyPfannschmidt I would like to have a host app and extend it using plugins. In order to extend a host, plugins need an access to plugin manager instance and hookimpl from host and both (PM and hookimpl) need to be imported into plugin. My idea is - get PM instance and hookimpl in a similar way how python logging gets logger instance:
mylogger = logging.getLogger('myapp')
I wrote an example Gist here:
Thank you for explanation. If I understand it correctly, there are two ways how a plugin can be registered - 1) using direct access to PM or 2) using setuptools entrypoint.
If I use the setuptools entrypoint, I need to name the python package as a plugin so the host can recognise it. I'm not developing primarily a plugin for host app so I don't want to name that python package as a plugin for that host.
The only way left is to import and directly use PM instance which is what I would like to avoid in a long run, therefore I'm looking for similar way that python logging uses:
pluggy= pluggy.get_pm('my_app'). If pluggy doesn't have this at the moment, I'll try to wrap it somehow. Thanks anyway, pluggy is useful package for me! :)
import host_project pm = host_project.get_pm('my_app') pm.register(plugin)
I was told once that I cannot get a specific hook from a specific plugin.
But today, I just found out the function
get_plugin, so I get curious... It seems that I can indeed get a specific hook from a specific plugin by doing:
pm.get_plugin('plugin_name').my_hook() I'm able to call a hook from a specific plugin.
I was just wondering (and asking here): Is there any issue on doing this? Is it possible that I'm missing something, and this is not a good thing to do?
plugins_that_should_run_specific_hook =  for plugin_name in available_plugin_names: if pm.get_plugin(plugin_name).hook_should_run(): # User defined hook that may return True or False, depending on the runtime configuration plugins_that_should_run_specific_hook.append(plugin_name) # (...) for plugin_name in plugins_that_should_run_specific_hook: pm.get_plugin(plugin_name).hook_run()
get_plugin(plugin_name).my_hook(), is that it?)
get_plugin(plugin_name)works to me