public
Last active

XML output format for pep8

  • Download Gist
pep8xml.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#!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)

Workaround for feature request https://github.com/jcrocholl/pep8/issues/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>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.