diff --git a/.gitignore b/.gitignore index 51d0ffa0..414244b6 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,6 @@ target/ # generated by setuptools_scm pluggy/_version.py + +# generated by pip +pip-wheel-metadata/ diff --git a/changelog/189.feature.rst b/changelog/189.feature.rst new file mode 100644 index 00000000..de3a3533 --- /dev/null +++ b/changelog/189.feature.rst @@ -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. diff --git a/pluggy/manager.py b/pluggy/manager.py index dc3dfa9b..2d2095a7 100644 --- a/pluggy/manager.py +++ b/pluggy/manager.py @@ -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 @@ -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 diff --git a/testing/test_pluginmanager.py b/testing/test_pluginmanager.py index fc9cf32b..20ede3c3 100644 --- a/testing/test_pluginmanager.py +++ b/testing/test_pluginmanager.py @@ -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" @@ -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"