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

Feature/get plugins for hook #177

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
5 changes: 5 additions & 0 deletions pluggy/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ def get_hookcallers(self, plugin):
""" get all hook callers for the specified plugin. """
return self._plugin2hookcallers.get(plugin)

def get_hookimpl(self, name):
""" Return a list of all hook implementations (HookImpl) for a given hook. """
hc = getattr(self.hook, name)
return hc._wrappers + hc._nonwrappers
Copy link
Member

Choose a reason for hiding this comment

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

the part dealing with private attributes should be delegated to the hookcaller

Copy link
Member Author

Choose a reason for hiding this comment

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

I initially did like this, but I saw other functions using also the _wrappers+_nonwrappers pattern and decided to follow it.

Is it ok to refactor them as well to keep coherence?

Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt Sep 16, 2018

Choose a reason for hiding this comment

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

seems like i missed the introduction of the pattern, lets do this as a follow-up issue so this one can be self-contained an in line with the current coding standard, we can resolve the pattern as a separate pr after its merit is taken into account

so lets keep yours as is 👍

Copy link
Member Author

@Sup3rGeo Sup3rGeo Sep 16, 2018

Choose a reason for hiding this comment

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

Ok!

Another thing - if hookcaller can give you the hookimpl, then maybe it is more general if we can just get the caller from pluginmanager and then call get_hookimpl() on the returned caller.

For getting the caller, we have two options:

  • the plugin manager to have an additional method hook_caller(name). Reads nice and simple.
  • or actually just use subset_hook_caller with remove_plugins as None or []. It actually just returns the original caller. No need to introduce another function, but does not read as nice (running this subset method to actually get the whole thing, not the subset)

I think I prefer the first option.

Copy link
Member

Choose a reason for hiding this comment

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

for now i wont explore the domain in detail as i lack personal time

Copy link
Member Author

Choose a reason for hiding this comment

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

I just realized you can get the hookcaller just by pluginmanager.hook.<name>. Even better, then I will just add the get_hookimpl to the hook caller, it should be enough.


def add_hookcall_monitoring(self, before, after):
""" add before/after tracing functions for all hooks
and return an undo function which, when called,
Expand Down
34 changes: 34 additions & 0 deletions testing/test_pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,40 @@ class PluginNo(object):
assert out == [10]


def test_get_hookimpl(pm):
class Hooks(object):
@hookspec
def he_method1(self, arg):
pass

pm.add_hookspecs(Hooks)

class Plugin1(object):
Sup3rGeo marked this conversation as resolved.
Show resolved Hide resolved
@hookimpl
def he_method1(self, arg):
pass

class Plugin2(object):
@hookimpl
def he_method1(self, arg):
pass

class PluginNo(object):
pass

plugin1, plugin2, plugin3 = Plugin1(), Plugin2(), PluginNo()
pm.register(plugin1)
pm.register(plugin2)
pm.register(plugin3)

hookimpls = pm.get_hookimpl("he_method1")
hook_plugins = [hookimpl.plugin for hookimpl in hookimpls]

assert plugin1 in hook_plugins
Copy link
Member

Choose a reason for hiding this comment

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

Took the liberty of improving this check a bit. 👍

assert plugin2 in hook_plugins
assert plugin3 not in hook_plugins


def test_add_hookspecs_nohooks(pm):
with pytest.raises(ValueError):
pm.add_hookspecs(10)
Expand Down