コード例 #1
0
ファイル: checker.py プロジェクト: jayvdb/flake8
    def is_path_excluded(self, path):
        # type: (str) -> bool
        """Check if a path is excluded.

        :param str path:
            Path to check against the exclude patterns.
        :returns:
            True if there are exclude patterns and the path matches,
            otherwise False.
        :rtype:
            bool
        """
        exclude = self.options.exclude
        if not exclude:
            return False
        basename = os.path.basename(path)
        if utils.fnmatch(basename, exclude):
            LOG.info('"%s" has been excluded', basename)
            return True

        absolute_path = os.path.abspath(path)
        match = utils.fnmatch(absolute_path, exclude)
        LOG.info('"%s" has %sbeen excluded', absolute_path,
                 '' if match else 'not ')
        return match
コード例 #2
0
ファイル: checker.py プロジェクト: AtomLinter/linter-pylama
    def is_path_excluded(self, path):
        # type: (str) -> bool
        """Check if a path is excluded.

        :param str path:
            Path to check against the exclude patterns.
        :returns:
            True if there are exclude patterns and the path matches,
            otherwise False.
        :rtype:
            bool
        """
        if path == '-':
            if self.options.stdin_display_name == 'stdin':
                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 match
コード例 #3
0
ファイル: checker.py プロジェクト: JimmyCN1/financeApp
    def is_path_excluded(self, path):
        # type: (str) -> bool
        """Check if a path is excluded.

        :param str path:
            Path to check against the exclude patterns.
        :returns:
            True if there are exclude patterns and the path matches,
            otherwise False.
        :rtype:
            bool
        """
        if path == "-":
            if self.options.stdin_display_name == "stdin":
                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 match
コード例 #4
0
ファイル: _plugin.py プロジェクト: waszil/flakehell
def get_plugin_rules(plugin_name: str, plugins: PluginsType) -> List[str]:
    """Get rules for plugin from `plugins` in the config

    Plugin name can be specified as a glob expression.
    So, it's not trivial to match the right one

    1. Try to find exact match (normalizing ass all packages names normalized)
    2. Try to find globs that match and select the longest one (nginx-style)
    3. Return empty list if nothing is found.
    """
    if not plugins:
        return []
    plugin_name = REX_NAME.sub('-', plugin_name).lower()
    # try to find exact match
    for pattern, rules in plugins.items():
        if '*' not in pattern and REX_NAME.sub('-',
                                               pattern).lower() == plugin_name:
            return rules

    # try to find match by pattern and select the longest
    best_match = (0, [])  # type: Tuple[int, List[str]]
    for pattern, rules in plugins.items():
        if not fnmatch(filename=plugin_name, patterns=[pattern]):
            continue
        match = len(pattern)
        if match > best_match[0]:
            best_match = match, rules
    if best_match[0]:
        return best_match[1]

    return []
コード例 #5
0
ファイル: checker.py プロジェクト: jayvdb/flake8
 def should_create_file_checker(filename):
     """Determine if we should create a file checker."""
     matches_filename_patterns = utils.fnmatch(filename,
                                               filename_patterns)
     is_stdin = filename == '-'
     file_exists = os.path.exists(filename)
     return (file_exists and matches_filename_patterns) or is_stdin
コード例 #6
0
ファイル: _plugin.py プロジェクト: waszil/flakehell
def check_include(code: str, rules: List[str]) -> bool:
    """
    0. Validate rules

    1. Return True if rule explicitly included
    2. Return False if rule explicitly excluded

    3. Return True if the latest glob-matching rule is include
    4. Return False if the latest glob-matching rule is exclude
    """
    # always report exceptions in file processing
    if code in ('E902', 'E999'):
        return True

    for rule in rules:
        if len(rule) < 2 or rule[0] not in {'-', '+'}:
            raise ValueError('invalid rule: `{}`'.format(rule))

    for rule in reversed(rules):
        if code.lower() == rule[1:].lower():
            return rule[0] == '+'

    include = False
    for rule in rules:
        if fnmatch(code, patterns=[rule[1:]]):
            include = rule[0] == '+'
    return include
コード例 #7
0
ファイル: _plugin.py プロジェクト: waszil/flakehell
def get_exceptions(
    path: Union[str, Path],
    exceptions: Dict[str, PluginsType],
    root: Path = None,
) -> PluginsType:
    if not exceptions:
        return dict()
    if isinstance(path, str):
        path = Path(path)
    if root is None:
        root = Path().resolve()
    path = path.resolve().relative_to(root).as_posix()
    exceptions = sorted(
        exceptions.items(),
        key=lambda item: len(item[0]),
        reverse=True,
    )

    # prefix
    for path_rule, rules in exceptions:
        if '*' in path_rule:
            continue
        if path.startswith(path_rule):
            return rules

    # glob
    for path_rule, rules in exceptions:
        if '*' not in path_rule:
            continue
        if fnmatch(filename=path, patterns=[path_rule]):
            return rules

    return dict()
コード例 #8
0
 def should_create_file_checker(filename):
     """Determine if we should create a file checker."""
     matches_filename_patterns = utils.fnmatch(
         filename, filename_patterns
     )
     is_stdin = filename == '-'
     file_exists = os.path.exists(filename)
     return (file_exists and matches_filename_patterns) or is_stdin
コード例 #9
0
    def _should_create_file_checker(self, filename: str, argument) -> bool:
        """Filter out excluded files
        """
        if filename == '-':
            return True
        if fnmatch(filename=filename, patterns=self.options.filename):
            return True

        if self.options._running_from_vcs:
            return False
        if self.options.diff:
            return False
        return argument == filename
コード例 #10
0
ファイル: checker.py プロジェクト: flipuover/flake8
 def should_create_file_checker(filename, argument):
     """Determine if we should create a file checker."""
     matches_filename_patterns = utils.fnmatch(filename,
                                               filename_patterns)
     is_stdin = filename == "-"
     # NOTE(sigmavirus24): If a user explicitly specifies something,
     # e.g, ``flake8 bin/script`` then we should run Flake8 against
     # that. Since should_create_file_checker looks to see if the
     # filename patterns match the filename, we want to skip that in
     # the event that the argument and the filename are identical.
     # If it was specified explicitly, the user intended for it to be
     # checked.
     explicitly_provided = not running_from_diff and (argument
                                                      == filename)
     return (explicitly_provided
             or matches_filename_patterns) or is_stdin
コード例 #11
0
ファイル: checker.py プロジェクト: AtomLinter/linter-pylama
 def should_create_file_checker(filename, argument):
     """Determine if we should create a file checker."""
     matches_filename_patterns = utils.fnmatch(
         filename, filename_patterns
     )
     is_stdin = filename == '-'
     file_exists = os.path.exists(filename)
     # NOTE(sigmavirus24): If a user explicitly specifies something,
     # e.g, ``flake8 bin/script`` then we should run Flake8 against
     # that. Since should_create_file_checker looks to see if the
     # filename patterns match the filename, we want to skip that in
     # the event that the argument and the filename are identical.
     # If it was specified explicitly, the user intended for it to be
     # checked.
     explicitly_provided = (not running_from_vcs and
                            not running_from_diff and
                            (argument == filename))
     return ((file_exists and
              (explicitly_provided or matches_filename_patterns)) or
             is_stdin)
コード例 #12
0
def get_exceptions(
    path: Union[str, Path],
    exceptions: Dict[str, PluginsType],
    root: Path = None,
) -> PluginsType:
    if not exceptions:
        return dict()
    if isinstance(path, str):
        path = Path(path)
    if root is None:
        root = Path().resolve()
    try:
        path = path.resolve().relative_to(root).as_posix()
    except ValueError:
        # path is not in the project root
        return dict()
    exceptions = sorted(
        exceptions.items(),
        key=lambda item: len(item[0]),
        reverse=True,
    )

    aggregated_rules = defaultdict(list)

    # prefix
    for path_rule, rules in exceptions:
        if '*' in path_rule:
            continue
        if path.startswith(path_rule):
            for plugin_name, exception_list in rules.items():
                aggregated_rules[plugin_name].extend(exception_list)

    # glob
    for path_rule, rules in exceptions:
        if '*' not in path_rule:
            continue
        if fnmatch(filename=path, patterns=[path_rule]):
            for plugin_name, exception_list in rules.items():
                aggregated_rules[plugin_name].extend(exception_list)

    return aggregated_rules
コード例 #13
0
ファイル: _checkers.py プロジェクト: waszil/flakehell
    def _should_process(
        self,
        argument: str,
        filename: str,
        check_type: str,
        check: Dict[str, Any],
    ) -> bool:
        # do not run plugins without rules specified
        plugin_name = get_plugin_name(check)
        rules = self._get_rules(plugin_name=plugin_name, filename=filename)
        if not rules or set(rules) == {'-*'}:
            return False

        if filename == '-':
            return True
        if fnmatch(filename=filename, patterns=self.options.filename):
            return True

        if self.options._running_from_vcs:
            return False
        if self.options.diff:
            return False
        return argument == filename
コード例 #14
0
def check_include(code, rules):
    # type: (str, List[str]) -> bool
    """
    0. Validate rules

    1. Return True if rule explicitly included
    2. Return False if rule explicitly excluded

    3. Return True if the latest glob-matching rule is include
    4. Return False if the latest glob-matching rule is exclude
    """
    for rule in rules:
        if len(rule) < 2 or rule[0] not in {'-', '+'}:
            raise ValueError('invalid rule: `{}`'.format(rule))

    for rule in reversed(rules):
        if code.lower() == rule[1:].lower():
            return rule[0] == '+'

    include = False
    for rule in rules:
        if fnmatch(code, patterns=[rule[1:]]):
            include = rule[0] == '+'
    return include
コード例 #15
0
def test_fnmatch(filename, patterns, expected):
    """Verify that our fnmatch wrapper works as expected."""
    assert utils.fnmatch(filename, patterns) is expected
コード例 #16
0
def test_fnmatch_returns_the_default_with_empty_default():
    """The default parameter should be returned when no patterns are given."""
    sentinel = object()
    assert utils.fnmatch('file.py', [], default=sentinel) is sentinel
コード例 #17
0
ファイル: test_utils.py プロジェクト: PyCQA/flake8
 def predicate(pth):
     return utils.fnmatch(os.path.abspath(pth), exclude)
コード例 #18
0
ファイル: test_utils.py プロジェクト: mkubux/pycqa-flake8
def test_fnmatch(filename, patterns, expected):
    """Verify that our fnmatch wrapper works as expected."""
    assert utils.fnmatch(filename, patterns) is expected
コード例 #19
0
ファイル: test_utils.py プロジェクト: mkubux/pycqa-flake8
 def predicate(pth):
     return utils.fnmatch(os.path.abspath(pth), exclude)
コード例 #20
0
def test_fnmatch_returns_the_default_with_empty_default():
    """The default parameter should be returned when no patterns are given."""
    sentinel = object()
    assert utils.fnmatch('file.py', [], default=sentinel) is sentinel