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 name parameter to PluginManager.load_setuptools_entrypoints #189

Merged
merged 2 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ target/

# generated by setuptools_scm
pluggy/_version.py

# generated by pip
pip-wheel-metadata/
5 changes: 5 additions & 0 deletions changelog/189.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
``PluginManager.load_setuptools_entrypoints`` now accepts a ``name`` parameter that when given will
load only entry points with that name.

``PluginManager.load_setuptools_entrypoints`` also now returns the number of plugins loaded by the
call, as opposed to the number of all plugins loaded by all calls to this method.
17 changes: 12 additions & 5 deletions pluggy/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,22 @@ def check_pending(self):
% (name, hookimpl.plugin),
)

def load_setuptools_entrypoints(self, entrypoint_name):
""" Load modules from querying the specified setuptools entrypoint name.
Return the number of loaded plugins. """
def load_setuptools_entrypoints(self, group, name=None):
""" Load modules from querying the specified setuptools ``group``.

:param str group: entry point group to load plugins
:param str name: if given, loads only plugins with the given ``name``.
:rtype: int
:return: return the number of loaded plugins by this call.
"""
from pkg_resources import (
iter_entry_points,
DistributionNotFound,
VersionConflict,
)

for ep in iter_entry_points(entrypoint_name):
count = 0
for ep in iter_entry_points(group, name=name):
Copy link
Contributor

Choose a reason for hiding this comment

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

for count, ep in enumerate(iter_entry_points(group, name=name)): maybe a little cleaner?

Copy link
Member

Choose a reason for hiding this comment

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

it would be wrong ^^ - see the continue cases

Copy link
Contributor

Choose a reason for hiding this comment

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

a good call. missed that cause of the stacked diff 😖

# is the plugin registered or blocked?
if self.get_plugin(ep.name) or self.is_blocked(ep.name):
continue
Expand All @@ -275,7 +281,8 @@ def load_setuptools_entrypoints(self, entrypoint_name):
)
self.register(plugin, name=ep.name)
self._plugin_distinfo.append((plugin, ep.dist))
return len(self._plugin_distinfo)
count += 1
return count

def list_plugin_distinfo(self):
""" return list of distinfo/plugin tuples for all setuptools registered
Expand Down
10 changes: 6 additions & 4 deletions testing/test_pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ def example_hook():
def test_load_setuptools_instantiation(monkeypatch, pm):
pkg_resources = pytest.importorskip("pkg_resources")

def my_iter(name):
assert name == "hello"
def my_iter(group, name=None):
assert group == "hello"

class EntryPoint(object):
name = "myname"
Expand All @@ -470,14 +470,16 @@ class PseudoPlugin(object):
plugin = pm.get_plugin("myname")
assert plugin.x == 42
assert pm.list_plugin_distinfo() == [(plugin, None)]
num = pm.load_setuptools_entrypoints("hello")
assert num == 0 # no plugin loaded by this call


def test_load_setuptools_version_conflict(monkeypatch, pm):
"""Check that we properly handle a VersionConflict problem when loading entry points"""
pkg_resources = pytest.importorskip("pkg_resources")

def my_iter(name):
assert name == "hello"
def my_iter(group, name=None):
assert group == "hello"

class EntryPoint(object):
name = "myname"
Expand Down