Merge branch 'entrypoints' into 'master'

Replace setuptools with entrypoints

See merge request pycqa/flake8!264
This commit is contained in:
Anthony Sottile 2018-11-21 17:28:55 +00:00
commit b3f205a936
9 changed files with 60 additions and 116 deletions

View file

@ -169,7 +169,7 @@ class Application(object):
If :attr:`check_plugins`, :attr:`listening_plugins`, or
:attr:`formatting_plugins` are ``None`` then this method will update
them with the appropriate plugin manager instance. Given the expense
of finding plugins (via :mod:`pkg_resources`) we want this to be
of finding plugins (via :mod:`entrypoints`) we want this to be
idempotent and so only update those attributes if they are ``None``.
"""
if self.local_plugins is None:
@ -238,16 +238,7 @@ class Application(object):
def formatter_for(self, formatter_plugin_name):
"""Retrieve the formatter class by plugin name."""
try:
default_formatter = self.formatting_plugins["default"]
except KeyError:
raise exceptions.ExecutionError(
"The 'default' Flake8 formatting plugin is unavailable. "
"This usually indicates that your setuptools is too old. "
"Please upgrade setuptools. If that does not fix the issue"
" please file an issue."
)
default_formatter = self.formatting_plugins["default"]
formatter_plugin = self.formatting_plugins.get(formatter_plugin_name)
if formatter_plugin is None:
LOG.warning(

View file

@ -4,6 +4,8 @@ from __future__ import print_function
import json
import platform
import entrypoints
def print_information(
option, option_string, value, parser, option_manager=None
@ -64,7 +66,4 @@ def plugins_from(option_manager):
def dependencies():
"""Generate the list of dependencies we care about."""
# defer this expensive import, not used outside --bug-report
import setuptools
return [{"dependency": "setuptools", "version": setuptools.__version__}]
return [{"dependency": "entrypoints", "version": entrypoints.__version__}]

View file

@ -2,7 +2,7 @@
import logging
import sys
import pkg_resources
import entrypoints
from flake8 import exceptions
from flake8 import utils
@ -143,17 +143,8 @@ class Plugin(object):
r"""Call the plugin with \*args and \*\*kwargs."""
return self.plugin(*args, **kwargs) # pylint: disable=not-callable
def _load(self, verify_requirements):
# Avoid relying on hasattr() here.
resolve = getattr(self.entry_point, "resolve", None)
require = getattr(self.entry_point, "require", None)
if resolve and require:
if verify_requirements:
LOG.debug('Verifying plugin "%s"\'s requirements.', self.name)
require()
self._plugin = resolve()
else:
self._plugin = self.entry_point.load(require=verify_requirements)
def _load(self):
self._plugin = self.entry_point.load()
if not callable(self._plugin):
msg = (
"Plugin %r is not a callable. It might be written for an"
@ -171,15 +162,14 @@ class Plugin(object):
cached plugin.
:param bool verify_requirements:
Whether or not to make setuptools verify that the requirements for
the plugin are satisfied.
Does nothing, retained for backwards compatibility.
:returns:
Nothing
"""
if self._plugin is None:
LOG.info('Loading plugin "%s" from entry-point.', self.name)
try:
self._load(verify_requirements)
self._load()
except Exception as load_exception:
LOG.exception(load_exception)
failed_to_load = exceptions.FailedToLoadPlugin(
@ -256,11 +246,9 @@ class PluginManager(object): # pylint: disable=too-few-public-methods
:param list local_plugins:
Plugins from config (as "X = path.to:Plugin" strings).
:param bool verify_requirements:
Whether or not to make setuptools verify that the requirements for
the plugin are satisfied.
Does nothing, retained for backwards compatibility.
"""
self.namespace = namespace
self.verify_requirements = verify_requirements
self.plugins = {}
self.names = []
self._load_local_plugins(local_plugins or [])
@ -273,12 +261,14 @@ class PluginManager(object): # pylint: disable=too-few-public-methods
Plugins from config (as "X = path.to:Plugin" strings).
"""
for plugin_str in local_plugins:
entry_point = pkg_resources.EntryPoint.parse(plugin_str)
name, _, entry_str = plugin_str.partition("=")
name, entry_str = name.strip(), entry_str.strip()
entry_point = entrypoints.EntryPoint.from_string(entry_str, name)
self._load_plugin_from_entrypoint(entry_point, local=True)
def _load_entrypoint_plugins(self):
LOG.info('Loading entry-points for "%s".', self.namespace)
for entry_point in pkg_resources.iter_entry_points(self.namespace):
for entry_point in entrypoints.get_group_all(self.namespace):
self._load_plugin_from_entrypoint(entry_point)
def _load_plugin_from_entrypoint(self, entry_point, local=False):