Last active
June 6, 2018 05:43
-
-
Save florentx/5232652 to your computer and use it in GitHub Desktop.
XML output format for pep8
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 | |
import pep8 | |
from xml.etree import ElementTree as ET | |
def prettyindent(elem, level=0): | |
# Adapted from http://effbot.org/zone/element-lib.htm#prettyprint | |
ind = "\n" + level * " " | |
if level: | |
elem.tail = ind | |
if len(elem): | |
if not elem.text: | |
elem.text = ind + " " | |
for sub in elem: | |
prettyindent(sub, level + 1) | |
sub.tail = ind | |
class XMLReport(pep8.StandardReport): | |
"""Collect and print the results in XML format.""" | |
def get_file_results(self): | |
"""Print the result and return the overall count for this file.""" | |
if self._deferred_print: | |
self._deferred_print.sort() | |
elem = ET.Element('file', {'path': self.filename}) | |
for line_number, offset, code, text, doc in self._deferred_print: | |
pos = '%d,%d' % (self.line_offset + line_number, offset + 1) | |
err = ET.SubElement(elem, 'error', {'code': code, 'pos': pos}) | |
err.text = text | |
prettyindent(elem) | |
print(ET.tostring(elem)) | |
return self.file_errors | |
if __name__ == '__main__': | |
pep8style = pep8.StyleGuide(parse_argv=True, config_file=True, | |
reporter=XMLReport) | |
report = pep8style.check_files() | |
if report.total_errors: | |
raise SystemExit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Workaround for feature request PyCQA/pycodestyle#179
This is a sample output: