Skip to content

Commit

Permalink
Merge pull request #189 from nicoddemus/early-load-4718
Browse files Browse the repository at this point in the history
Add name parameter to PluginManager.load_setuptools_entrypoints
  • Loading branch information
nicoddemus committed Feb 21, 2019
2 parents 9a12ad4 + 893b546 commit 3c384dd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
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):
# 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

0 comments on commit 3c384dd

Please sign in to comment.