summaryrefslogtreecommitdiff
path: root/src/flake8
diff options
context:
space:
mode:
authorIan Stapleton Cordasco <graffatcolmingov@gmail.com>2018-10-26 15:05:06 -0500
committerIan Stapleton Cordasco <graffatcolmingov@gmail.com>2018-10-27 08:01:14 -0500
commite09a22a416b61f242feb90d8a536286713d0ab7e (patch)
tree8b8cf3740141788547749902c0b2eb0c3bbc1c85 /src/flake8
parent1433a008b30cb97af2c28709daf763ae872c5bf9 (diff)
downloadflake8-e09a22a416b61f242feb90d8a536286713d0ab7e.tar.gz
Refactor to support the per-file-ignores
Most of the problems with our logic were due to not having the same logic as our exclude parameter. This refactors that out into a separate function so we can confidently achieve that.
Diffstat (limited to 'src/flake8')
-rw-r--r--src/flake8/checker.py19
-rw-r--r--src/flake8/style_guide.py12
-rw-r--r--src/flake8/utils.py32
3 files changed, 43 insertions, 20 deletions
diff --git a/src/flake8/checker.py b/src/flake8/checker.py
index e8a71d4..3b97414 100644
--- a/src/flake8/checker.py
+++ b/src/flake8/checker.py
@@ -2,7 +2,6 @@
import collections
import errno
import logging
-import os
import signal
import sys
import tokenize
@@ -188,20 +187,12 @@ class Manager(object):
return False
path = self.options.stdin_display_name
- exclude = self.options.exclude
- if not exclude:
- return False
- basename = os.path.basename(path)
- if utils.fnmatch(basename, exclude):
- LOG.debug('"%s" has been excluded', basename)
- return True
-
- absolute_path = os.path.abspath(path)
- match = utils.fnmatch(absolute_path, exclude)
- LOG.debug(
- '"%s" has %sbeen excluded', absolute_path, "" if match else "not "
+ return utils.matches_filename(
+ path,
+ patterns=self.options.exclude,
+ log_message='"%(path)s" has %(whether)sbeen excluded',
+ logger=LOG,
)
- return match
def make_checkers(self, paths=None):
# type: (List[str]) -> NoneType
diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py
index 42576e9..46f72fc 100644
--- a/src/flake8/style_guide.py
+++ b/src/flake8/style_guide.py
@@ -459,7 +459,7 @@ class StyleGuide(object):
def copy(self, filename=None, extend_ignore_with=None, **kwargs):
"""Create a copy of this style guide with different values."""
filename = filename or self.filename
- options = copy.copy(self.options)
+ options = copy.deepcopy(self.options)
options.ignore.extend(extend_ignore_with or [])
return StyleGuide(
options, self.listener, self.formatter, filename=filename
@@ -485,11 +485,11 @@ class StyleGuide(object):
"""
if self.filename is None:
return True
- normalized_filename = utils.normalize_path(filename)
- return (
- normalized_filename == self.filename
- or utils.fnmatch(filename, self.filename)
- or utils.fnmatch(normalized_filename, self.filename)
+ return utils.matches_filename(
+ filename,
+ patterns=[self.filename],
+ log_message='{!r} does %(whether)smatch "%(path)s"'.format(self),
+ logger=LOG,
)
def should_report_error(self, code):
diff --git a/src/flake8/utils.py b/src/flake8/utils.py
index e77998d..68627af 100644
--- a/src/flake8/utils.py
+++ b/src/flake8/utils.py
@@ -336,6 +336,38 @@ def parameters_for(plugin):
return parameters
+def matches_filename(path, patterns, log_message, logger):
+ """Use fnmatch to discern if a path exists in patterns.
+
+ :param str path:
+ The path to the file under question
+ :param patterns:
+ The patterns to match the path against.
+ :type patterns:
+ list[str]
+ :param str log_message:
+ The message used for logging purposes.
+ :returns:
+ True if path matches patterns, False otherwise
+ :rtype:
+ bool
+ """
+ if not patterns:
+ return False
+ basename = os.path.basename(path)
+ if fnmatch(basename, patterns):
+ logger.debug(log_message, {"path": basename, "whether": ""})
+ return True
+
+ absolute_path = os.path.abspath(path)
+ match = fnmatch(absolute_path, patterns)
+ logger.debug(
+ log_message,
+ {"path": absolute_path, "whether": "" if match else "not "},
+ )
+ return match
+
+
def get_python_version():
"""Find and format the python implementation and version.