diff options
| author | Carl Meyer <carl@oddbird.net> | 2017-08-03 00:25:37 -0700 |
|---|---|---|
| committer | Carl Meyer <carl@oddbird.net> | 2017-08-03 00:25:37 -0700 |
| commit | 4e58068657ece52e3f1636cb028e42c15b86fec3 (patch) | |
| tree | c739fe9428a1a3f46d836773e243cdcdb22ddb27 /src/flake8/plugins | |
| parent | 6df26ffd57178e50194aea31b3bf9c572f91fa54 (diff) | |
| download | flake8-4e58068657ece52e3f1636cb028e42c15b86fec3.tar.gz | |
Add support for local (in-repo, non-setuptools) plugins.
Closes #357
Diffstat (limited to 'src/flake8/plugins')
| -rw-r--r-- | src/flake8/plugins/manager.py | 53 |
1 files changed, 40 insertions, 13 deletions
diff --git a/src/flake8/plugins/manager.py b/src/flake8/plugins/manager.py index 80a9ef2..e7b402f 100644 --- a/src/flake8/plugins/manager.py +++ b/src/flake8/plugins/manager.py @@ -236,11 +236,14 @@ class Plugin(object): class PluginManager(object): # pylint: disable=too-few-public-methods """Find and manage plugins consistently.""" - def __init__(self, namespace, verify_requirements=False): + def __init__(self, namespace, + local_plugins=None, verify_requirements=False): """Initialize the manager. :param str namespace: Namespace of the plugins to manage, e.g., 'flake8.extension'. + :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. @@ -249,15 +252,34 @@ class PluginManager(object): # pylint: disable=too-few-public-methods self.verify_requirements = verify_requirements self.plugins = {} self.names = [] - self._load_all_plugins() + self._load_local_plugins(local_plugins or []) + self._load_entrypoint_plugins() - def _load_all_plugins(self): + def _load_local_plugins(self, local_plugins): + """Load local plugins from config. + + :param list local_plugins: + Plugins from config (as "X = path.to:Plugin" strings). + """ + for plugin_str in local_plugins: + entry_point = pkg_resources.EntryPoint.parse(plugin_str) + self._load_plugin_from_entrypoint(entry_point) + + 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): - name = entry_point.name - self.plugins[name] = Plugin(name, entry_point) - self.names.append(name) - LOG.debug('Loaded %r for plugin "%s".', self.plugins[name], name) + self._load_plugin_from_entrypoint(entry_point) + + def _load_plugin_from_entrypoint(self, entry_point): + """Load a plugin from a setuptools EntryPoint. + + :param EntryPoint entry_point: + EntryPoint to load plugin from. + """ + name = entry_point.name + self.plugins[name] = Plugin(name, entry_point) + self.names.append(name) + LOG.debug('Loaded %r for plugin "%s".', self.plugins[name], name) def map(self, func, *args, **kwargs): r"""Call ``func`` with the plugin and \*args and \**kwargs after. @@ -329,9 +351,14 @@ class PluginTypeManager(object): namespace = None - def __init__(self): - """Initialize the plugin type's manager.""" - self.manager = PluginManager(self.namespace) + def __init__(self, local_plugins=None): + """Initialize the plugin type's manager. + + :param list local_plugins: + Plugins from config file instead of entry-points + """ + self.manager = PluginManager( + self.namespace, local_plugins=local_plugins) self.plugins_loaded = False def __contains__(self, name): @@ -436,7 +463,7 @@ class NotifierBuilderMixin(object): # pylint: disable=too-few-public-methods class Checkers(PluginTypeManager): - """All of the checkers registered through entry-ponits.""" + """All of the checkers registered through entry-points or config.""" namespace = 'flake8.extension' @@ -515,12 +542,12 @@ class Checkers(PluginTypeManager): class Listeners(PluginTypeManager, NotifierBuilderMixin): - """All of the listeners registered through entry-points.""" + """All of the listeners registered through entry-points or config.""" namespace = 'flake8.listen' class ReportFormatters(PluginTypeManager): - """All of the report formatters registered through entry-points.""" + """All of the report formatters registered through entry-points/config.""" namespace = 'flake8.report' |
