Skip to content

Commit 5feba52

Browse files
committed
Merge pull request #429 from doismellburning/feature/file-parsing-flexibility
Multi-line ignore parsing in config_file
2 parents 151758c + e7d64d3 commit 5feba52

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

pep8.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,10 +2081,10 @@ def process_options(arglist=None, parse_argv=False, config_file=None,
20812081
options = read_config(options, args, arglist, parser)
20822082
options.reporter = parse_argv and options.quiet == 1 and FileReport
20832083

2084-
options.filename = options.filename and options.filename.split(',')
2084+
options.filename = _parse_multi_options(options.filename.split(','))
20852085
options.exclude = normalize_paths(options.exclude)
2086-
options.select = options.select and options.select.split(',')
2087-
options.ignore = options.ignore and options.ignore.split(',')
2086+
options.select = _parse_multi_options(options.select.split(','))
2087+
options.ignore = _parse_multi_options(options.ignore.split(','))
20882088

20892089
if options.diff:
20902090
options.reporter = DiffReport
@@ -2095,6 +2095,22 @@ def process_options(arglist=None, parse_argv=False, config_file=None,
20952095
return options, args
20962096

20972097

2098+
def _parse_multi_options(options):
2099+
r"""Split and strip and discard empties.
2100+
2101+
Turns the following:
2102+
2103+
A,
2104+
B,
2105+
2106+
into ["A", "B"]
2107+
"""
2108+
if options:
2109+
return [o.strip() for o in options if o.strip()]
2110+
else:
2111+
return options
2112+
2113+
20982114
def _main():
20992115
"""Parse options and run checks on Python source."""
21002116
import signal

testsuite/test_all.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ def test_own_dog_food(self):
4646

4747

4848
def suite():
49-
from testsuite import test_api, test_shell, test_util
49+
from testsuite import test_api, test_parser, test_shell, test_util
5050

5151
suite = unittest.TestSuite()
5252
suite.addTest(unittest.makeSuite(Pep8TestCase))
5353
suite.addTest(unittest.makeSuite(test_api.APITestCase))
54+
suite.addTest(unittest.makeSuite(test_parser.ParserTestCase))
5455
suite.addTest(unittest.makeSuite(test_shell.ShellTestCase))
5556
suite.addTest(unittest.makeSuite(test_util.UtilTestCase))
5657
return suite

testsuite/test_parser.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import os
2+
import tempfile
3+
import unittest
4+
5+
import pep8
6+
7+
8+
def _process_file(contents):
9+
with tempfile.NamedTemporaryFile(delete=False) as f:
10+
f.write(contents)
11+
12+
options, args = pep8.process_options(config_file=f.name)
13+
os.remove(f.name)
14+
15+
return options, args
16+
17+
18+
class ParserTestCase(unittest.TestCase):
19+
20+
def test_vanilla_ignore_parsing(self):
21+
contents = b"""
22+
[pep8]
23+
ignore = E226,E24
24+
"""
25+
options, args = _process_file(contents)
26+
27+
self.assertEqual(options.ignore, ["E226", "E24"])
28+
29+
def test_multiline_ignore_parsing(self):
30+
contents = b"""
31+
[pep8]
32+
ignore =
33+
E226,
34+
E24
35+
"""
36+
37+
options, args = _process_file(contents)
38+
39+
self.assertEqual(options.ignore, ["E226", "E24"])
40+
41+
def test_trailing_comma_ignore_parsing(self):
42+
contents = b"""
43+
[pep8]
44+
ignore = E226,
45+
"""
46+
47+
options, args = _process_file(contents)
48+
49+
self.assertEqual(options.ignore, ["E226"])
50+
51+
def test_multiline_trailing_comma_ignore_parsing(self):
52+
contents = b"""
53+
[pep8]
54+
ignore =
55+
E226,
56+
E24,
57+
"""
58+
59+
options, args = _process_file(contents)
60+
61+
self.assertEqual(options.ignore, ["E226", "E24"])

0 commit comments

Comments
 (0)