From 91e07ebcffc0d4ecc472bb0031113f6030f43a20 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sat, 28 May 2016 07:54:07 -0500 Subject: [PATCH] Refactor off-by-default plugins and enabling them We move the logic to add or remove a plugin from the default ignore list to individual methods on the Plugin class (Plugin#enable, Plugin#disable) and use that when registering and parsing options. If the plugin is off-by-default, Plugin#register_options will use Plugin#disable. When parsing options via Plugin#provide_options, if the plugin has been specified in --enable-extensions then it will be re-enabled via Plugin#enable. --- flake8/main/cli.py | 4 ++-- flake8/options/manager.py | 15 +++++++++++++++ flake8/plugins/manager.py | 13 ++++++++++++- tests/unit/test_plugin.py | 7 +++++-- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/flake8/main/cli.py b/flake8/main/cli.py index ef9b328..e9eedb7 100644 --- a/flake8/main/cli.py +++ b/flake8/main/cli.py @@ -34,7 +34,7 @@ def register_default_options(option_manager): - ``--disable-noqa`` - ``--show-source`` - ``--statistics`` - - ``--enabled-extensions`` + - ``--enable-extensions`` - ``--exit-zero`` - ``-j``/``--jobs`` - ``--output-file`` @@ -141,7 +141,7 @@ def register_default_options(option_manager): # Flake8 options add_option( - '--enabled-extensions', default='', parse_from_config=True, + '--enable-extensions', default='', parse_from_config=True, comma_separated_list=True, type='string', help='Enable plugins and extensions that are otherwise disabled ' 'by default', diff --git a/flake8/options/manager.py b/flake8/options/manager.py index bf144c9..439cba2 100644 --- a/flake8/options/manager.py +++ b/flake8/options/manager.py @@ -189,6 +189,21 @@ class OptionManager(object): self.config_options_dict[option.config_name] = option LOG.debug('Registered option "%s".', option) + def remove_from_default_ignore(self, error_codes): + """Remove specified error codes from the default ignore list. + + :param list error_codes: + List of strings that are the error/warning codes to attempt to + remove from the extended default ignore list. + """ + LOG.debug('Removing %r from the default ignore list', error_codes) + for error_code in error_codes: + try: + self.extend_default_ignore.remove(error_code) + except ValueError: + LOG.debug('Attempted to remove %s from default ignore' + ' but it was not a member of the list.', error_code) + def extend_default_ignore(self, error_codes): """Extend the default ignore list with the error codes provided. diff --git a/flake8/plugins/manager.py b/flake8/plugins/manager.py index fe51391..dffc2f6 100644 --- a/flake8/plugins/manager.py +++ b/flake8/plugins/manager.py @@ -158,6 +158,14 @@ class Plugin(object): LOG.critical(str(failed_to_load)) raise failed_to_load + def enable(self, optmanager): + """Remove plugin name from the default ignore list.""" + optmanager.remove_from_default_ignore([self.name]) + + def disable(self, optmanager): + """Add the plugin name to the default ignore list.""" + optmanager.extend_default_ignore([self.name]) + def provide_options(self, optmanager, options, extra_args): """Pass the parsed options and extra arguments to the plugin.""" parse_options = getattr(self.plugin, 'parse_options', None) @@ -168,6 +176,9 @@ class Plugin(object): except TypeError: parse_options(options) + if self.name in options.enable_extensions: + self.enable(optmanager) + def register_options(self, optmanager): """Register the plugin's command-line options on the OptionManager. @@ -187,7 +198,7 @@ class Plugin(object): add_options(optmanager) if self.off_by_default: - optmanager.extend_default_ignore([self.name]) + self.disable(optmanager) class PluginManager(object): # pylint: disable=too-few-public-methods diff --git a/tests/unit/test_plugin.py b/tests/unit/test_plugin.py index e995c7b..d3d1185 100644 --- a/tests/unit/test_plugin.py +++ b/tests/unit/test_plugin.py @@ -1,4 +1,6 @@ """Tests for flake8.plugins.manager.Plugin.""" +import optparse + from flake8 import exceptions from flake8.plugins import manager @@ -138,14 +140,15 @@ def test_provide_options(): entry_point = mock.Mock(spec=['require', 'resolve', 'load']) plugin_obj = mock.Mock(spec_set=['name', 'version', 'add_options', 'parse_options']) + option_values = optparse.Values({'enable_extensions': []}) option_manager = mock.Mock() plugin = manager.Plugin('T000', entry_point) plugin._plugin = plugin_obj # Call the method we're testing. - plugin.provide_options(option_manager, 'options', None) + plugin.provide_options(option_manager, option_values, None) # Assert that we call add_options plugin_obj.parse_options.assert_called_once_with( - option_manager, 'options', None + option_manager, option_values, None )