diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index f41e5d4..0c9e151 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -14,3 +14,5 @@ Contributors (by order of appearance) : - Miki Tebeka - David Cramer - Peter Teichman +- Oleg Broytman +- Marc Labbé diff --git a/README b/README index 064e3fd..0d4cf7d 100644 --- a/README +++ b/README @@ -101,6 +101,9 @@ standard output. 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. +*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 point to your python interpreter. @@ -223,8 +226,11 @@ CHANGES ======= 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 diff --git a/flake8/pep8.py b/flake8/pep8.py index af68577..da9a112 100644 --- a/flake8/pep8.py +++ b/flake8/pep8.py @@ -482,6 +482,9 @@ def continuation_line_indentation(logical_line, tokens, indent_level, verbose): print(">>> " + tokens[0][4].rstrip()) for token_type, text, start, end, line in tokens: + if line.strip().lower().endswith('# nopep8'): + continue + newline = row < start[0] - first_row if newline: row = start[0] - first_row diff --git a/flake8/pyflakes.py b/flake8/pyflakes.py index 4705224..3d65e7d 100644 --- a/flake8/pyflakes.py +++ b/flake8/pyflakes.py @@ -336,6 +336,9 @@ class Checker(object): if node.name is not None: if isinstance(node.name, str): name = node.name + elif hasattr(node.name, 'elts'): + names = [e.id for e in node.name.elts] + name = '({0})'.format(', '.join(names)) else: name = node.name.id self.addBinding(node.lineno, Assignment(name, node)) @@ -646,21 +649,21 @@ class Checker(object): self.addBinding(node.lineno, importation) -def checkPath(filename): +def checkPath(filename, ignore=[]): """ Check the given path, printing out any warnings detected. @return: the number of warnings printed """ try: - return check(open(filename, 'U').read() + '\n', filename) + return check(open(filename, 'U').read() + '\n', ignore, filename) except IOError: msg = sys.exc_info()[1] sys.stderr.write("%s: %s\n" % (filename, msg.args[1])) return 1 -def check(codeString, filename='(code)'): +def check(codeString, ignore, filename='(code)'): """ Check the Python source given by C{codeString} for flakes. @@ -711,7 +714,7 @@ def check(codeString, filename='(code)'): valid_warnings = 0 for warning in w.messages: - if skip_warning(warning): + if skip_warning(warning, ignore): continue print(warning) valid_warnings += 1 diff --git a/flake8/run.py b/flake8/run.py index 663bd4a..f7b5b52 100644 --- a/flake8/run.py +++ b/flake8/run.py @@ -20,8 +20,10 @@ from flake8 import mccabe pep8style = None -def check_file(path, complexity=-1): - warnings = pyflakes.checkPath(path) +def check_file(path, ignore=[], complexity=-1): + if pep8style.excluded(path): + return 0 + warnings = pyflakes.checkPath(path, ignore) warnings += pep8style.input_file(path) if complexity > -1: warnings += mccabe.get_module_complexity(path, complexity) @@ -46,11 +48,11 @@ def _get_python_files(paths): if not filename.endswith('.py'): continue fullpath = os.path.join(dirpath, filename) - if not skip_file(fullpath, pep8style): + if not skip_file(fullpath) or pep8style.excluded(fullpath): yield fullpath else: - if not skip_file(path, pep8style): + if not skip_file(path) or pep8style.excluded(path): yield path @@ -68,7 +70,7 @@ def main(): if pep8style.paths and options.filename is not None: for path in _get_python_files(pep8style.paths): - warnings += check_file(path, complexity) + warnings += check_file(path, options.ignore, complexity) else: # wait for 1 second on the stdin fd reads, __, __ = select.select([sys.stdin], [], [], 1.) @@ -140,14 +142,18 @@ def run(command): [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() if ignore: pep8.options.ignore = ignore 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: ext = os.path.splitext(filename)[-1] if ext != '.py': diff --git a/flake8/util.py b/flake8/util.py index 9ab86ef..1cd61b0 100644 --- a/flake8/util.py +++ b/flake8/util.py @@ -3,8 +3,10 @@ import re import os -def skip_warning(warning): +def skip_warning(warning, ignore=[]): # 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): return False @@ -22,14 +24,16 @@ def skip_line(line): _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 # flake8: noqa """ + if not os.path.isfile(path): + return False f = open(path) try: content = f.read() finally: f.close() - return _NOQA.search(content) is not None or pep8style.excluded(path) + return _NOQA.search(content) is not None diff --git a/setup.py b/setup.py old mode 100755 new mode 100644