Create a gist now

Instantly share code, notes, and snippets.

@florentx /
Last active Aug 18, 2016

What would you like to do?
XML output format for pep8
import pep8
from xml.etree import ElementTree as ET
def prettyindent(elem, level=0):
# Adapted from
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:
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
return self.file_errors
if __name__ == '__main__':
pep8style = pep8.StyleGuide(parse_argv=True, config_file=True,
report = pep8style.check_files()
if report.total_errors:
raise SystemExit(1)

florentx commented Mar 24, 2013

Workaround for feature request jcrocholl/pep8#179

This is a sample output:

$ python ../cpy25/Lib/ ../cpy25/Lib/ 
<file path="../cpy25/Lib/">
  <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 path="../cpy25/Lib/">
  <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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment