Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python linting script #1032

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions .lint.py
@@ -0,0 +1,59 @@
#!/usr/bin/env python3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you run autopep8 over this?


import sys
import argparse
import re

parser = argparse.ArgumentParser(description='Lint markdown drafts.')
parser.add_argument('files', metavar='file', nargs='+', help='Files to lint')
parser.add_argument('-l', dest='maxLineLength', default=80)
parser.add_argument('-f', dest='maxFigureLineLength', default=65)

args = parser.parse_args()

foundError = False

for inputfile in args.files:
insideFigure = False
beforeAbstract = True
with open(inputfile, 'U') as draft:
linecounter = 1
lines = draft.readlines()

abstract = re.compile('^--- abstract')
table = re.compile('^\s*(?:\||{:)')
figure = re.compile('^[~`]{3}')

for line in lines:
line = line.rstrip('\r\n')
linenumber = linecounter
linecounter += 1

# Skip everything before abstract
if beforeAbstract:
matchObj = abstract.match(line)
if matchObj:
beforeAbstract = False
continue

# Skip tables
matchObj = table.match(line)
if matchObj:
continue

# Toggle figure state
matchObj = figure.match(line)
if matchObj:
insideFigure = not insideFigure
continue

# Check length
length = len(line)
limit = args.maxFigureLineLength if insideFigure else args.maxLineLength
if length > limit:
foundError = True
sys.stderr.write("{0}: Line is {1} characters; limit is {2}\n".format(
linenumber, length, limit))
sys.stderr.write("{0}\n".format(line))

sys.exit(1 if foundError else 0)
15 changes: 4 additions & 11 deletions Makefile
Expand Up @@ -14,15 +14,8 @@ endif

latest:: lint
.PHONY: lint

PYTHON := $(or $(shell which python3),$(shell which python))

lint::
@err=0; for f in draft-*.md ; do \
if cat "$$f" | (l=0; while read -r a; do l=$$(($$l + 1)); echo -E "$$l:$$a"; done) | \
sed -e '1,/--- abstract/d;/^[0-9]*: *|/d' | tr -d '\r' | grep '^[0-9]*:.\{81\}'; then \
echo "$$f contains a line with >80 characters"; err=1; \
fi; \
if cat "$$f" | (l=0; while read -r a; do l=$$(($$l + 1)); echo -E "$$l:$$a"; done) | \
sed -e '/^[0-9]*:~~~/,/^[0-9]*:~~~/p;/^[0-9]*:```/,/^[0-9]*:```/p;d' | \
tr -d '\r' | grep '^[0-9]*:.\{66\}'; then \
echo "$$f contains a figure with >65 characters"; err=1; \
fi; \
done; [ "$$err" -eq 0 ]
@$(PYTHON) ./.lint.py draft-*.md