Last active
December 15, 2015 08:39
-
-
Save florentx/5232746 to your computer and use it in GitHub Desktop.
Output Flake8 errors immediately
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
#!python | |
# https://github.com/jcrocholl/pep8/issues/181 | |
import sys | |
import pep8 | |
from flake8.engine import get_style_guide | |
from flake8.main import DEFAULT_CONFIG | |
class ImmediateReport(pep8.BaseReport): | |
"""Collect and print the results of the checks.""" | |
def __init__(self, options): | |
super(ImmediateReport, self).__init__(options) | |
self._fmt = pep8.REPORT_FORMAT.get(options.format.lower(), | |
options.format) | |
self._repeat = options.repeat | |
def error(self, line_number, offset, text, check): | |
"""Report an error, according to options.""" | |
code = super(ImmediateReport, self).error(line_number, offset, | |
text, check) | |
if code and (self.counters[code] == 1 or self._repeat): | |
print(self._fmt % { | |
'path': self.filename, | |
'row': self.line_offset + line_number, 'col': offset + 1, | |
'code': code, 'text': text[5:], | |
}) | |
return code | |
# XXX this could be simplified if the signature of `flake8.run.main` is changed | |
# to accept random keywords to pass to the `StyleGuide` constructor, | |
# like `def main(**kw):` | |
def main(): | |
"""Parse options and run checks on Python source.""" | |
# Prepare | |
flake8_style = get_style_guide(parse_argv=True, config_file=DEFAULT_CONFIG, | |
reporter=ImmediateReport) | |
options = flake8_style.options | |
if options.install_hook: | |
from flake8.hooks import install_hook | |
install_hook() | |
# Run the checkers | |
report = flake8_style.check_files() | |
# Print the final report | |
options = flake8_style.options | |
if options.statistics: | |
report.print_statistics() | |
if options.benchmark: | |
report.print_benchmark() | |
if report.total_errors: | |
if options.count: | |
sys.stderr.write(str(report.total_errors) + '\n') | |
if not options.exit_zero: | |
raise SystemExit(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was going to ask this elsewhere, but why do we do
options = flake8_style.options
twice in main?