Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@florentx
Last active June 6, 2018 05:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save florentx/5232652 to your computer and use it in GitHub Desktop.
Save florentx/5232652 to your computer and use it in GitHub Desktop.
XML output format for pep8
#!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)
@florentx
Copy link
Author

Workaround for feature request PyCQA/pycodestyle#179

This is a sample output:

$ python pep8xml.py ../cpy25/Lib/textwrap.py ../cpy25/Lib/netrc.py 
<file path="../cpy25/Lib/textwrap.py">
  <error code="E401" pos="10,14">multiple imports on one line</error>
  <error code="E302" pos="32,1">expected 2 blank lines, found 1</error>
  <error code="E303" pos="94,5">too many blank lines (2)</error>
  <error code="E303" pos="114,5">too many blank lines (2)</error>
  <error code="E303" pos="131,5">too many blank lines (2)</error>
  <error code="E303" pos="265,5">too many blank lines (2)</error>
  <error code="E302" pos="305,1">expected 2 blank lines, found 1</error>
  <error code="E302" pos="323,1">expected 2 blank lines, found 1</error>
  <error code="E121" pos="365,20">continuation line indentation is not a multiple of four</error>
</file>
<file path="../cpy25/Lib/netrc.py">
  <error code="E401" pos="5,10">multiple imports on one line</error>
  <error code="E501" pos="58,80">line too long (80 &gt; 79 characters)</error>
  <error code="E225" pos="64,23">missing whitespace around operator</error>
  <error code="E125" pos="65,21">continuation line does not distinguish itself from next logical line</error>
  <error code="E225" pos="65,45">missing whitespace around operator</error>
  <error code="E225" pos="99,35">missing whitespace around operator</error>
</file>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment