From a4042d6d692354f486d9f3e3f810d08ff3d7339e Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sat, 9 Jan 2016 15:58:26 -0600 Subject: [PATCH] Start fleshing out flake8.utils Add parse_comma_separated_list, normalize_path, and normalize_paths and add logic in OptionManager.parse_args to use the right normalize_path* function based on comma_separated_list value of the option. --- flake8/options/manager.py | 25 +++++++++++++------- flake8/utils.py | 50 +++++++++++++++++++++++++++++++++++++++ tests/unit/test_utils.py | 14 +++++++++++ 3 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 tests/unit/test_utils.py diff --git a/flake8/options/manager.py b/flake8/options/manager.py index 6c0d036..3e4c128 100644 --- a/flake8/options/manager.py +++ b/flake8/options/manager.py @@ -150,13 +150,22 @@ class OptionManager(object): """Simple proxy to calling the OptionParser's parse_args method.""" options, xargs = self.parser.parse_args(args, values) for option in self.options: - dest = option.dest - if option.normalize_paths: - old_value = getattr(options, dest) - setattr(options, dest, utils.normalize_paths(old_value)) - elif option.comma_separated_list: - old_value = getattr(options, dest) - setattr(options, dest, - utils.parse_comma_separated_list(old_value)) + _normalize_option(options, option) return options, xargs + + +def _normalize_option(options, option): + dest = option.dest + if option.normalize_paths: + old_value = getattr(options, dest) + # Decide whether to parse a list of paths or a single path + normalize = utils.normalize_path + if option.comma_separated_list: + normalize = utils.normalize_paths + setattr(options, dest, normalize(old_value)) + + elif option.comma_separated_list: + old_value = getattr(options, dest) + setattr(options, dest, + utils.parse_comma_separated_list(old_value)) diff --git a/flake8/utils.py b/flake8/utils.py index e69de29..72c7852 100644 --- a/flake8/utils.py +++ b/flake8/utils.py @@ -0,0 +1,50 @@ +"""Utility methods for flake8.""" +import os + + +def parse_comma_separated_list(value): + """Parse a comma-separated list. + + :param value: + String or list of strings to be parsed and normalized. + :returns: + List of values with whitespace stripped. + :rtype: + list + """ + if not value: + return [] + + if not isinstance(value, (list, tuple)): + value = value.split(',') + + return [item.strip() for item in value] + + +def normalize_paths(paths, parent=os.curdir): + """Parse a comma-separated list of paths. + + :returns: + The normalized paths. + :rtype: + [str] + """ + paths = [] + for path in parse_comma_separated_list(paths): + if '/' in path: + path = os.path.abspath(os.path.join(parent, path)) + paths.append(path.rstrip('/')) + return paths + + +def normalize_path(path, parent=os.curdir): + """Normalize a single-path. + + :returns: + The normalized path. + :rtype: + str + """ + if '/' in path: + path = os.path.abspath(os.path.join(parent, path)) + return path diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py new file mode 100644 index 0000000..28c47c8 --- /dev/null +++ b/tests/unit/test_utils.py @@ -0,0 +1,14 @@ +"""Tests for flake8's utils module.""" +import pytest + +from flake8 import utils + + +@pytest.mark.parametrize("value,expected", [ + ("E123,\n\tW234,\n E206", ["E123", "W234", "E206"]), + ("E123,W234,E206", ["E123", "W234", "E206"]), + (["E123", "W234", "E206"], ["E123", "W234", "E206"]), + (["E123", "\n\tW234", "\n E206"], ["E123", "W234", "E206"]), +]) +def test_parse_comma_separated_list(value, expected): + assert utils.parse_comma_separated_list(value) == expected