Created
February 18, 2016 19:29
-
-
Save jvanasco/46034f42000872e5d6ff to your computer and use it in GitHub Desktop.
Flake8 output was giving me a headache, so i wrote this quick parser.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import os.path | |
import pprint | |
_sample_data = """/Users/me/path/to/project/files/lib/data/bar.py:52:1: W293 blank line contains whitespace | |
/Users/me/path/to/project/files/lib/data/foo.py:101:1: W293 blank line contains whitespace | |
/Users/me/path/to/project/files/lib/utils/date.py:328:5: F811 redefinition of unused 'currentYear' from line 306 | |
/Users/me/path/to/project/files/lib/utils/date.py:331:5: F811 redefinition of unused 'currentMonth' from line 310 | |
/Users/me/path/to/project/files/lib/utils/date.py:334:5: F811 redefinition of unused 'currentDay' from line 314 | |
/Users/me/path/to/project/files/lib/utils/foo.py:595:1: W293 blank line contains whitespace | |
/Users/me/path/to/project/files/lib/utils/bar.py:605:1: W293 blank line contains whitespace | |
/Users/me/path/to/project/files/foo.py:133:76: W291 trailing whitespace""" | |
def flake8_to_json(data, prefix=None): | |
output = {} | |
prefix_length = None if prefix is None else len(prefix) | |
data = [l.strip() for l in data.split('\n')] | |
for line in data: | |
_newline = False | |
(_path, _row, _col, _code_text) = line.split(':') | |
if prefix is not None: | |
if _path[:prefix_length] == prefix: | |
_path = _path[prefix_length:] | |
if _path not in output: | |
output[_path] = [] | |
output[_path].append(':'.join([_row, _col, _code_text])) | |
return output | |
def to_checklist(output): | |
ks = output.keys() | |
ks.sort() | |
as_checklist = [] | |
for k in ks: | |
as_checklist.append(k) | |
as_checklist.extend(['\t%s' % i for i in output[k]]) | |
return '\n'.join(as_checklist) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--file", | |
help="Which file?", | |
) | |
parser.add_argument("--mode", | |
help="how should the output be formatted? defaults to `checklist`, but `json` and `json-pretty` are valid", | |
choices=['json', 'json-pretty', 'checklist'], | |
default='checklist', | |
) | |
parser.add_argument("--prefix", | |
type=str, | |
help="Use a prefix?", | |
) | |
parser.add_argument("--demo", | |
help="Run in demo mode?", | |
action='store_true', | |
) | |
options = parser.parse_args() | |
if options.demo: | |
data = _sample_data | |
output = flake8_to_json(data, prefix='/Users/me/path/to/project/files/') | |
as_checklist = to_checklist(output) | |
print "#" * 20 | |
print "flake8_to_json -----" | |
pprint.pprint(output) | |
print "#" * 20 | |
print "as_checklist -----" | |
print as_checklist | |
exit() | |
if not options.file: | |
print "Outside of `--demo` mode, you must supply a `--file`" | |
exit() | |
if not os.path.exists(options.file): | |
raise ValueError("`%s` is not a valid file." % options.file) | |
data = open(options.file, 'r').read() | |
output = flake8_to_json(data, prefix=options.prefix) | |
if options.mode == 'json-pretty': | |
pprint.pprint(output) | |
elif options.mode == 'json': | |
print output | |
elif options.mode == 'checklist': | |
output = to_checklist(output) | |
print output | |
""" | |
http://pep8.readthedocs.org/en/latest/intro.html#example-usage-and-output | |
$ pep8 testsuite/E40.py --format=default | |
testsuite/E40.py:2:10: E401 multiple imports on one line | |
$ pep8 testsuite/E40.py --format=pylint | |
testsuite/E40.py:2: [E401] multiple imports on one line | |
$ pep8 testsuite/E40.py --format='%(path)s\n\y%(row)d|%(col)d| %(code)s %(text)s' | |
testsuite/E40.py|2|10| E401 multiple imports on one line | |
Variables in the custom format option | |
Variable Significance | |
path File name | |
row Row number | |
col Column number | |
code Error code | |
text Error text | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment