Merged in fix_excludes (pull request #27)

This commit is contained in:
Tarek Ziadé 2012-11-16 10:26:42 +01:00
commit c13fdc8ce3
7 changed files with 39 additions and 15 deletions

View file

@ -14,3 +14,5 @@ Contributors (by order of appearance) :
- Miki Tebeka - Miki Tebeka
- David Cramer - David Cramer
- Peter Teichman - Peter Teichman
- Oleg Broytman
- Marc Labbé

8
README
View file

@ -101,6 +101,9 @@ standard output.
is emited. If you don't specify it or set it to **-1**, it's just ignored. is emited. If you don't specify it or set it to **-1**, it's just ignored.
If specified, it must be a positive value. 12 is usually a good value. If specified, it must be a positive value. 12 is usually a good value.
*lazy* when set to ``True`` will also take into account files not added to the
index.
Also, make sure the file is executable and adapt the shebang line so it Also, make sure the file is executable and adapt the shebang line so it
point to your python interpreter. point to your python interpreter.
@ -223,8 +226,11 @@ CHANGES
======= =======
1.6 1.6
---
XXX - changed the signatures of the ``check_file`` function in flake8/run.py,
``skip_warning`` in flake8/util.py and the ``check``, ``checkPath``
functions in flake8/pyflakes.py.
1.5 - 2012-10-13 1.5 - 2012-10-13

View file

@ -482,6 +482,9 @@ def continuation_line_indentation(logical_line, tokens, indent_level, verbose):
print(">>> " + tokens[0][4].rstrip()) print(">>> " + tokens[0][4].rstrip())
for token_type, text, start, end, line in tokens: for token_type, text, start, end, line in tokens:
if line.strip().lower().endswith('# nopep8'):
continue
newline = row < start[0] - first_row newline = row < start[0] - first_row
if newline: if newline:
row = start[0] - first_row row = start[0] - first_row

View file

@ -336,6 +336,9 @@ class Checker(object):
if node.name is not None: if node.name is not None:
if isinstance(node.name, str): if isinstance(node.name, str):
name = node.name name = node.name
elif hasattr(node.name, 'elts'):
names = [e.id for e in node.name.elts]
name = '({0})'.format(', '.join(names))
else: else:
name = node.name.id name = node.name.id
self.addBinding(node.lineno, Assignment(name, node)) self.addBinding(node.lineno, Assignment(name, node))
@ -646,21 +649,21 @@ class Checker(object):
self.addBinding(node.lineno, importation) self.addBinding(node.lineno, importation)
def checkPath(filename): def checkPath(filename, ignore=[]):
""" """
Check the given path, printing out any warnings detected. Check the given path, printing out any warnings detected.
@return: the number of warnings printed @return: the number of warnings printed
""" """
try: try:
return check(open(filename, 'U').read() + '\n', filename) return check(open(filename, 'U').read() + '\n', ignore, filename)
except IOError: except IOError:
msg = sys.exc_info()[1] msg = sys.exc_info()[1]
sys.stderr.write("%s: %s\n" % (filename, msg.args[1])) sys.stderr.write("%s: %s\n" % (filename, msg.args[1]))
return 1 return 1
def check(codeString, filename='(code)'): def check(codeString, ignore, filename='(code)'):
""" """
Check the Python source given by C{codeString} for flakes. Check the Python source given by C{codeString} for flakes.
@ -711,7 +714,7 @@ def check(codeString, filename='(code)'):
valid_warnings = 0 valid_warnings = 0
for warning in w.messages: for warning in w.messages:
if skip_warning(warning): if skip_warning(warning, ignore):
continue continue
print(warning) print(warning)
valid_warnings += 1 valid_warnings += 1

View file

@ -20,8 +20,10 @@ from flake8 import mccabe
pep8style = None pep8style = None
def check_file(path, complexity=-1): def check_file(path, ignore=[], complexity=-1):
warnings = pyflakes.checkPath(path) if pep8style.excluded(path):
return 0
warnings = pyflakes.checkPath(path, ignore)
warnings += pep8style.input_file(path) warnings += pep8style.input_file(path)
if complexity > -1: if complexity > -1:
warnings += mccabe.get_module_complexity(path, complexity) warnings += mccabe.get_module_complexity(path, complexity)
@ -46,11 +48,11 @@ def _get_python_files(paths):
if not filename.endswith('.py'): if not filename.endswith('.py'):
continue continue
fullpath = os.path.join(dirpath, filename) fullpath = os.path.join(dirpath, filename)
if not skip_file(fullpath, pep8style): if not skip_file(fullpath) or pep8style.excluded(fullpath):
yield fullpath yield fullpath
else: else:
if not skip_file(path, pep8style): if not skip_file(path) or pep8style.excluded(path):
yield path yield path
@ -68,7 +70,7 @@ def main():
if pep8style.paths and options.filename is not None: if pep8style.paths and options.filename is not None:
for path in _get_python_files(pep8style.paths): for path in _get_python_files(pep8style.paths):
warnings += check_file(path, complexity) warnings += check_file(path, options.ignore, complexity)
else: else:
# wait for 1 second on the stdin fd # wait for 1 second on the stdin fd
reads, __, __ = select.select([sys.stdin], [], [], 1.) reads, __, __ = select.select([sys.stdin], [], [], 1.)
@ -140,14 +142,18 @@ 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, ignore=None): def git_hook(complexity=-1, strict=False, ignore=None, lazy=False):
_initpep8() _initpep8()
if ignore: if ignore:
pep8.options.ignore = ignore pep8.options.ignore = ignore
warnings = 0 warnings = 0
_, files_modified, _ = run("git diff-index --cached --name-only HEAD") gitcmd = "git diff-index --cached --name-only HEAD"
if lazy:
gitcmd = gitcmd.replace('--cached ', '')
_, files_modified, _ = run(gitcmd)
for filename in files_modified: for filename in files_modified:
ext = os.path.splitext(filename)[-1] ext = os.path.splitext(filename)[-1]
if ext != '.py': if ext != '.py':

View file

@ -3,8 +3,10 @@ import re
import os import os
def skip_warning(warning): def skip_warning(warning, ignore=[]):
# XXX quick dirty hack, just need to keep the line in the warning # XXX quick dirty hack, just need to keep the line in the warning
if warning.message.split()[0] in ignore:
return True
if not os.path.isfile(warning.filename): if not os.path.isfile(warning.filename):
return False return False
@ -22,14 +24,16 @@ def skip_line(line):
_NOQA = re.compile(r'flake8[:=]\s*noqa', re.I | re.M) _NOQA = re.compile(r'flake8[:=]\s*noqa', re.I | re.M)
def skip_file(path, pep8style): def skip_file(path):
"""Returns True if this header is found in path """Returns True if this header is found in path
# flake8: noqa # flake8: noqa
""" """
if not os.path.isfile(path):
return False
f = open(path) f = open(path)
try: try:
content = f.read() content = f.read()
finally: finally:
f.close() f.close()
return _NOQA.search(content) is not None or pep8style.excluded(path) return _NOQA.search(content) is not None

0
setup.py Executable file → Normal file
View file