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 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

View file

@ -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):

View file

@ -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.