From 222f0a8115651eecc4f88933bb887068427e97bf Mon Sep 17 00:00:00 2001 From: Ian Stapleton Cordasco Date: Wed, 9 Aug 2017 19:35:01 -0500 Subject: [PATCH] Allow our local plugin parsing to accept commas By slightly modifying our utility function to parse comma separated lists we can parse local plugins similar to other configuration options. --- src/flake8/options/config.py | 13 ++++++++----- src/flake8/utils.py | 10 ++++++++-- tests/unit/test_get_local_plugins.py | 3 +++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index 49910ba..7e770b6 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -5,6 +5,8 @@ import logging import os.path import sys +from flake8 import utils + LOG = logging.getLogger(__name__) __all__ = ('ConfigFileFinder', 'MergedConfigParser') @@ -320,11 +322,12 @@ def get_local_plugins(config_finder, cli_config=None, isolated=False): section = '%s:local-plugins' % config_finder.program_name for plugin_type in ['extension', 'report']: if config.has_option(section, plugin_type): - getattr(local_plugins, plugin_type).extend( - c.strip() for c in config.get( - section, plugin_type - ).strip().splitlines() - ) + local_plugins_string = config.get(section, plugin_type).strip() + plugin_type_list = getattr(local_plugins, plugin_type) + plugin_type_list.extend(utils.parse_comma_separated_list( + local_plugins_string, + regexp=utils.LOCAL_PLUGIN_LIST_RE, + )) return local_plugins diff --git a/src/flake8/utils.py b/src/flake8/utils.py index 953b594..d28b810 100644 --- a/src/flake8/utils.py +++ b/src/flake8/utils.py @@ -11,14 +11,20 @@ import tokenize DIFF_HUNK_REGEXP = re.compile(r'^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@.*$') COMMA_SEPARATED_LIST_RE = re.compile(r'[,\s]') +LOCAL_PLUGIN_LIST_RE = re.compile(r'[,\t\n\r\f\v]') -def parse_comma_separated_list(value): +def parse_comma_separated_list(value, regexp=COMMA_SEPARATED_LIST_RE): # type: (Union[Sequence[str], str]) -> List[str] """Parse a comma-separated list. :param value: String or list of strings to be parsed and normalized. + :param regexp: + Compiled regular expression used to split the value when it is a + string. + :type regexp: + _sre.SRE_Pattern :returns: List of values with whitespace stripped. :rtype: @@ -28,7 +34,7 @@ def parse_comma_separated_list(value): return [] if not isinstance(value, (list, tuple)): - value = COMMA_SEPARATED_LIST_RE.split(value) + value = regexp.split(value) item_gen = (item.strip() for item in value) return [item for item in item_gen if item] diff --git a/tests/unit/test_get_local_plugins.py b/tests/unit/test_get_local_plugins.py index 942599a..b2f985e 100644 --- a/tests/unit/test_get_local_plugins.py +++ b/tests/unit/test_get_local_plugins.py @@ -18,7 +18,10 @@ def test_get_local_plugins_respects_isolated(): def test_get_local_plugins_uses_cli_config(): """Verify behaviour of get_local_plugins with a specified config.""" + config_obj = mock.Mock() config_finder = mock.MagicMock() + config_finder.cli_config.return_value = config_obj + config_obj.get.return_value = '' config.get_local_plugins(config_finder, cli_config='foo.ini')