Skip to content

Instantly share code, notes, and snippets.

@AnthonyBriggs
Last active July 10, 2017 13:26
Show Gist options
  • Save AnthonyBriggs/87b7c6012a0284cebdb577676f0c8f83 to your computer and use it in GitHub Desktop.
Save AnthonyBriggs/87b7c6012a0284cebdb577676f0c8f83 to your computer and use it in GitHub Desktop.
Rough code to scrape pycodestyle for errors and output .rst-ish
"""Rough script to scrape pycodestyle.py and intro.rst for error codes and descriptions, then generate something .rst-ish"""
import collections
import re
import pycodestyle
ErrorCode = collections.namedtuple("ErrorCode", field_names=("code", "short_msg", "long_msg"))
### scrape intro.rst
def generate_error_codes(file_name="intro.rst"):
""" Read the docs/intro.rst file and extract error and warning codes.
Relatively easy, since they're in a table and start with "| W" or "| E"
"""
intro_codes = {}
for line in open(file_name).read().split('\n'):
if not (line.startswith("| E") or
line.startswith("| W")):
continue
code = line[2:6]
short_msg = line.split("|")[2].strip()
#print((code, short_msg))
intro_codes[code] = short_msg
return intro_codes
intro_codes = generate_error_codes()
#print (intro_codes)
warn_or_error = re.compile('([EW]\d\d\d):')
### now scrape pycodestyle.py for warning/error functions
err_funcs = {}
# TODO: Check: multiple codes per function, but one function per code??
for f in dir(pycodestyle):
func = getattr(pycodestyle, f)
if type(func).__name__ != 'function' or not func.__doc__:
continue
codes = warn_or_error.findall(func.__doc__)
if codes:
#print (f"Match! {codes}")
for match in set(codes):
err_funcs[match] = func
#print ("-" * 42)
### Build 'master list' of errors
error_codes = {}
for code in sorted(intro_codes.keys()):
if code in err_funcs:
doc = err_funcs[code].__doc__
else:
doc = "No doc found for " + code
ec = ErrorCode(
code=code,
short_msg=intro_codes[code],
long_msg=doc)
error_codes[code] = ec
#print (f"{ec.code}: {ec.short_msg}")
#print (ec.long_msg)
#print ("-----\n")
print (""".. currentmodule:: pycodestyle
Error codes
============
These are the `PEP 8`_ errors that pycodestyle checks for.
.. contents::
:local:
""")
for code, ec in error_codes.items():
print (f"""
{ec.code}: {ec.short_msg}
{"-"*( len(ec.code) + len(ec.short_msg) + 2)}
{ec.long_msg}""")
# for some weird reason long message comes out with multiple trailing lines -- might be a Windows thing?
print ("""
.. _PEP 8: http://www.python.org/dev/peps/pep-0008/
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment