mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-12 07:44:16 +00:00
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.
This commit is contained in:
parent
ad0200e792
commit
a4042d6d69
3 changed files with 81 additions and 8 deletions
|
|
@ -150,13 +150,22 @@ class OptionManager(object):
|
||||||
"""Simple proxy to calling the OptionParser's parse_args method."""
|
"""Simple proxy to calling the OptionParser's parse_args method."""
|
||||||
options, xargs = self.parser.parse_args(args, values)
|
options, xargs = self.parser.parse_args(args, values)
|
||||||
for option in self.options:
|
for option in self.options:
|
||||||
dest = option.dest
|
_normalize_option(options, option)
|
||||||
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))
|
|
||||||
|
|
||||||
return options, xargs
|
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))
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
14
tests/unit/test_utils.py
Normal file
14
tests/unit/test_utils.py
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue