Merged in huhao/flake8 (pull request #25)

This commit is contained in:
Tarek Ziadé 2012-09-12 10:03:51 +02:00
commit 0d29a4b02a
3 changed files with 17 additions and 10 deletions

2
README
View file

@ -90,7 +90,7 @@ To use the Git hook on any *commit*, add a **pre-commit** file in the
STRICT = False STRICT = False
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(git_hook(complexity=COMPLEXITY, strict=STRICT)) sys.exit(git_hook(complexity=COMPLEXITY, strict=STRICT, ignore='E501'))
If *strict* option is set to **True**, any warning will block the commit. When If *strict* option is set to **True**, any warning will block the commit. When

View file

@ -11,6 +11,7 @@ except ImportError:
import optparse import optparse
import sys import sys
from flake8.util import skip_warning
from collections import defaultdict from collections import defaultdict
WARNING_CODE = "W901" WARNING_CODE = "W901"
@ -243,14 +244,16 @@ def get_code_complexity(code, min=7, filename='stdin'):
# ? # ?
continue continue
if graph.complexity() >= min: if graph.complexity() >= min:
msg = '%s:%d:1: %s %r is too complex (%d)' % ( graph.filename = filename
filename, if not skip_warning(graph):
graph.lineno, msg = '%s:%d:1: %s %r is too complex (%d)' % (
WARNING_CODE, filename,
graph.entity, graph.lineno,
graph.complexity(), WARNING_CODE,
) graph.entity,
complex.append(msg) graph.complexity(),
)
complex.append(msg)
if len(complex) == 0: if len(complex) == 0:
return 0 return 0

View file

@ -123,6 +123,7 @@ def _initpep8():
pep8.options.logical_checks = pep8.find_checks('logical_line') pep8.options.logical_checks = pep8.find_checks('logical_line')
pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0) pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0)
pep8.options.messages = {} pep8.options.messages = {}
pep8.options.max_line_length = 79
pep8.args = [] pep8.args = []
@ -133,8 +134,11 @@ def run(command):
[line.strip() for line in p.stderr.readlines()]) [line.strip() for line in p.stderr.readlines()])
def git_hook(complexity=-1, strict=False): def git_hook(complexity=-1, strict=False, ignore=None):
_initpep8() _initpep8()
if ignore:
pep8.options.ignore=ignore
warnings = 0 warnings = 0
_, files_modified, _ = run("git diff-index --cached --name-only HEAD") _, files_modified, _ = run("git diff-index --cached --name-only HEAD")