diff --git a/flake8/engine.py b/flake8/engine.py index 332c8c8..bcbad54 100644 --- a/flake8/engine.py +++ b/flake8/engine.py @@ -1,9 +1,13 @@ # -*- coding: utf-8 -*- +import re + import pep8 from flake8 import __version__ from flake8.util import OrderedSet +_flake8_noqa = re.compile(r'flake8[:=]\s*noqa', re.I).search + def _register_extensions(): """Register all the extensions.""" @@ -46,10 +50,26 @@ def get_parser(): return parser, options_hooks +class StyleGuide(pep8.StyleGuide): + # Backward compatibility pep8 <= 1.4.2 + checker_class = pep8.Checker + + def input_file(self, filename, lines=None, expected=None, line_offset=0): + """Run all checks on a Python source file.""" + if self.options.verbose: + print('checking %s' % filename) + fchecker = self.checker_class( + filename, lines=lines, options=self.options) + # Any "# flake8: noqa" line? + if any(_flake8_noqa(line) for line in fchecker.lines): + return 0 + return fchecker.check_all(expected=expected, line_offset=line_offset) + + def get_style_guide(**kwargs): """Parse the options and configure the checker.""" kwargs['parser'], options_hooks = get_parser() - styleguide = pep8.StyleGuide(**kwargs) + styleguide = StyleGuide(**kwargs) options = styleguide.options for options_hook in options_hooks: options_hook(options) diff --git a/flake8/hooks.py b/flake8/hooks.py index efbc063..37c1b60 100644 --- a/flake8/hooks.py +++ b/flake8/hooks.py @@ -10,7 +10,6 @@ except ImportError: # Python 2 from flake8.engine import get_parser, get_style_guide from flake8.main import DEFAULT_CONFIG -from flake8.util import skip_file def git_hook(complexity=-1, strict=False, ignore=None, lazy=False): @@ -64,7 +63,7 @@ def _get_files(repo, **kwargs): if file_ in seen or not os.path.exists(file_): continue seen.add(file_) - if file_.endswith('.py') and not skip_file(file_): + if file_.endswith('.py'): yield file_ diff --git a/flake8/util.py b/flake8/util.py index 3354274..771cca0 100644 --- a/flake8/util.py +++ b/flake8/util.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import with_statement -import os.path -import re -__all__ = ['ast', 'iter_child_nodes', 'OrderedSet', 'skip_file'] +__all__ = ['ast', 'iter_child_nodes', 'OrderedSet'] try: import ast @@ -40,19 +38,3 @@ class OrderedSet(list): def add(self, value): if value not in self: self.append(value) - -_NOQA = re.compile(r'flake8[:=]\s*noqa', re.I | re.M) - - -def skip_file(path, source=None): - """Returns True if this header is found in path - - # flake8: noqa - """ - if os.path.isfile(path): - with open(path) as f: - source = f.read() - elif not source: - return False - - return _NOQA.search(source) is not None