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.
This commit is contained in:
Ian Stapleton Cordasco 2018-10-26 15:05:06 -05:00
parent 1433a008b3
commit e09a22a416
No known key found for this signature in database
GPG key ID: C9D7A2604B4FCB2A
3 changed files with 43 additions and 20 deletions

View file

@ -2,7 +2,6 @@
import collections import collections
import errno import errno
import logging import logging
import os
import signal import signal
import sys import sys
import tokenize import tokenize
@ -188,20 +187,12 @@ class Manager(object):
return False return False
path = self.options.stdin_display_name path = self.options.stdin_display_name
exclude = self.options.exclude return utils.matches_filename(
if not exclude: path,
return False patterns=self.options.exclude,
basename = os.path.basename(path) log_message='"%(path)s" has %(whether)sbeen excluded',
if utils.fnmatch(basename, exclude): logger=LOG,
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 match
def make_checkers(self, paths=None): def make_checkers(self, paths=None):
# type: (List[str]) -> NoneType # type: (List[str]) -> NoneType

View file

@ -459,7 +459,7 @@ class StyleGuide(object):
def copy(self, filename=None, extend_ignore_with=None, **kwargs): def copy(self, filename=None, extend_ignore_with=None, **kwargs):
"""Create a copy of this style guide with different values.""" """Create a copy of this style guide with different values."""
filename = filename or self.filename filename = filename or self.filename
options = copy.copy(self.options) options = copy.deepcopy(self.options)
options.ignore.extend(extend_ignore_with or []) options.ignore.extend(extend_ignore_with or [])
return StyleGuide( return StyleGuide(
options, self.listener, self.formatter, filename=filename options, self.listener, self.formatter, filename=filename
@ -485,11 +485,11 @@ class StyleGuide(object):
""" """
if self.filename is None: if self.filename is None:
return True return True
normalized_filename = utils.normalize_path(filename) return utils.matches_filename(
return ( filename,
normalized_filename == self.filename patterns=[self.filename],
or utils.fnmatch(filename, self.filename) log_message='{!r} does %(whether)smatch "%(path)s"'.format(self),
or utils.fnmatch(normalized_filename, self.filename) logger=LOG,
) )
def should_report_error(self, code): def should_report_error(self, code):

View file

@ -336,6 +336,38 @@ def parameters_for(plugin):
return parameters 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(): def get_python_version():
"""Find and format the python implementation and version. """Find and format the python implementation and version.