예제 #1
0
파일: config.py 프로젝트: Khan/khan-linter
def get_local_plugins(config_finder, cli_config=None, isolated=False):
    """Get local plugins lists from config files.

    :param flake8.options.config.ConfigFileFinder config_finder:
        The config file finder to use.
    :param str cli_config:
        Value of --config when specified at the command-line. Overrides
        all other config files.
    :param bool isolated:
        Determines if we should parse configuration files at all or not.
        If running in isolated mode, we ignore all configuration files
    :returns:
        LocalPlugins namedtuple containing two lists of plugin strings,
        one for extension (checker) plugins and one for report plugins.
    :rtype:
        flake8.options.config.LocalPlugins
    """
    local_plugins = LocalPlugins(extension=[], report=[], paths=[])
    if isolated:
        LOG.debug('Refusing to look for local plugins in configuration'
                  'files due to user-requested isolation')
        return local_plugins

    if cli_config:
        LOG.debug('Reading local plugins only from "%s" specified via '
                  '--config by the user', cli_config)
        config = config_finder.cli_config(cli_config)
        config_files = [cli_config]
    else:
        config, config_files = config_finder.local_configs_with_files()

    base_dirs = {os.path.dirname(cf) for cf in config_files}

    section = '%s:local-plugins' % config_finder.program_name
    for plugin_type in ['extension', 'report']:
        if config.has_option(section, plugin_type):
            local_plugins_string = config.get(section, plugin_type).strip()
            plugin_type_list = getattr(local_plugins, plugin_type)
            plugin_type_list.extend(utils.parse_comma_separated_list(
                local_plugins_string,
                regexp=utils.LOCAL_PLUGIN_LIST_RE,
            ))
    if config.has_option(section, 'paths'):
        raw_paths = utils.parse_comma_separated_list(
            config.get(section, 'paths').strip()
        )
        norm_paths = []
        for base_dir in base_dirs:
            norm_paths.extend(
                path for path in
                utils.normalize_paths(raw_paths, parent=base_dir)
                if os.path.exists(path)
            )
        local_plugins.paths.extend(norm_paths)
    return local_plugins
예제 #2
0
def get_local_plugins(config_finder):
    """Get local plugins lists from config files.

    :param flake8.options.config.ConfigFileFinder config_finder:
        The config file finder to use.
    :returns:
        LocalPlugins namedtuple containing two lists of plugin strings,
        one for extension (checker) plugins and one for report plugins.
    :rtype:
        flake8.options.config.LocalPlugins
    """
    local_plugins = LocalPlugins(extension=[], report=[], paths=[])
    if config_finder.ignore_config_files:
        LOG.debug(
            "Refusing to look for local plugins in configuration"
            "files due to user-requested isolation"
        )
        return local_plugins

    if config_finder.config_file:
        LOG.debug(
            'Reading local plugins only from "%s" specified via '
            "--config by the user",
            config_finder.config_file,
        )
        config = config_finder.cli_config(config_finder.config_file)
        config_files = [config_finder.config_file]
    else:
        config, config_files = config_finder.local_configs_with_files()

    base_dirs = {os.path.dirname(cf) for cf in config_files}

    section = f"{config_finder.program_name}:local-plugins"
    for plugin_type in ["extension", "report"]:
        if config.has_option(section, plugin_type):
            local_plugins_string = config.get(section, plugin_type).strip()
            plugin_type_list = getattr(local_plugins, plugin_type)
            plugin_type_list.extend(
                utils.parse_comma_separated_list(
                    local_plugins_string, regexp=utils.LOCAL_PLUGIN_LIST_RE
                )
            )
    if config.has_option(section, "paths"):
        raw_paths = utils.parse_comma_separated_list(
            config.get(section, "paths").strip()
        )
        norm_paths: List[str] = []
        for base_dir in base_dirs:
            norm_paths.extend(
                path
                for path in utils.normalize_paths(raw_paths, parent=base_dir)
                if os.path.exists(path)
            )
        local_plugins.paths.extend(norm_paths)
    return local_plugins
예제 #3
0
def get_local_plugins(config_finder, cli_config=None, isolated=False):
    """Get local plugins lists from config files.

    :param flake8.options.config.ConfigFileFinder config_finder:
        The config file finder to use.
    :param str cli_config:
        Value of --config when specified at the command-line. Overrides
        all other config files.
    :param bool isolated:
        Determines if we should parse configuration files at all or not.
        If running in isolated mode, we ignore all configuration files
    :returns:
        LocalPlugins namedtuple containing two lists of plugin strings,
        one for extension (checker) plugins and one for report plugins.
    :rtype:
        flake8.options.config.LocalPlugins
    """
    local_plugins = LocalPlugins(extension=[], report=[], paths=[])
    if isolated:
        LOG.debug("Refusing to look for local plugins in configuration"
                  "files due to user-requested isolation")
        return local_plugins

    if cli_config:
        LOG.debug(
            'Reading local plugins only from "%s" specified via '
            "--config by the user",
            cli_config,
        )
        config = config_finder.cli_config(cli_config)
        config_files = [cli_config]
    else:
        config, config_files = config_finder.local_configs_with_files()

    base_dirs = {os.path.dirname(cf) for cf in config_files}

    section = "%s:local-plugins" % config_finder.program_name
    for plugin_type in ["extension", "report"]:
        if config.has_option(section, plugin_type):
            local_plugins_string = config.get(section, plugin_type).strip()
            plugin_type_list = getattr(local_plugins, plugin_type)
            plugin_type_list.extend(
                utils.parse_comma_separated_list(
                    local_plugins_string, regexp=utils.LOCAL_PLUGIN_LIST_RE))
    if config.has_option(section, "paths"):
        raw_paths = utils.parse_comma_separated_list(
            config.get(section, "paths").strip())
        norm_paths = []  # type: List[str]
        for base_dir in base_dirs:
            norm_paths.extend(
                path
                for path in utils.normalize_paths(raw_paths, parent=base_dir)
                if os.path.exists(path))
        local_plugins.paths.extend(norm_paths)
    return local_plugins
예제 #4
0
    def is_inline_ignored(self, error):
        # type: (Error) -> bool
        """Determine if an comment has been added to ignore this line."""
        physical_line = error.physical_line
        # TODO(sigmavirus24): Determine how to handle stdin with linecache
        if self.options.disable_noqa:
            return False

        if physical_line is None:
            physical_line = linecache.getline(error.filename,
                                              error.line_number)
        noqa_match = defaults.NOQA_INLINE_REGEXP.search(physical_line)
        if noqa_match is None:
            LOG.debug('%r is not inline ignored', error)
            return False

        codes_str = noqa_match.groupdict()['codes']
        if codes_str is None:
            LOG.debug('%r is ignored by a blanket ``# noqa``', error)
            return True

        codes = set(utils.parse_comma_separated_list(codes_str))
        if error.code in codes or error.code.startswith(tuple(codes)):
            LOG.debug('%r is ignored specifically inline with ``# noqa: %s``',
                      error, codes_str)
            return True

        LOG.debug('%r is not ignored inline with ``# noqa: %s``',
                  error, codes_str)
        return False
예제 #5
0
    def is_inline_ignored(self, error):
        # type: (Error) -> bool
        """Determine if an comment has been added to ignore this line."""
        physical_line = error.physical_line
        # TODO(sigmavirus24): Determine how to handle stdin with linecache
        if self.options.disable_noqa:
            return False

        if physical_line is None:
            physical_line = linecache.getline(error.filename,
                                              error.line_number)
        noqa_match = defaults.NOQA_INLINE_REGEXP.search(physical_line)
        if noqa_match is None:
            LOG.debug('%r is not inline ignored', error)
            return False

        codes_str = noqa_match.groupdict()['codes']
        if codes_str is None:
            LOG.debug('%r is ignored by a blanket ``# noqa``', error)
            return True

        codes = set(utils.parse_comma_separated_list(codes_str))
        if error.code in codes or error.code.startswith(tuple(codes)):
            LOG.debug('%r is ignored specifically inline with ``# noqa: %s``',
                      error, codes_str)
            return True

        LOG.debug('%r is not ignored inline with ``# noqa: %s``', error,
                  codes_str)
        return False
예제 #6
0
파일: manager.py 프로젝트: jayvdb/flake8
 def normalize(self, value):
     """Normalize the value based on the option configuration."""
     if self.normalize_paths:
         # Decide whether to parse a list of paths or a single path
         normalize = utils.normalize_path
         if self.comma_separated_list:
             normalize = utils.normalize_paths
         return normalize(value)
     elif self.comma_separated_list:
         return utils.parse_comma_separated_list(value)
     return value
예제 #7
0
파일: manager.py 프로젝트: jayvdb/flake8
 def normalize(self, value):
     """Normalize the value based on the option configuration."""
     if self.normalize_paths:
         # Decide whether to parse a list of paths or a single path
         normalize = utils.normalize_path
         if self.comma_separated_list:
             normalize = utils.normalize_paths
         return normalize(value)
     elif self.comma_separated_list:
         return utils.parse_comma_separated_list(value)
     return value
예제 #8
0
    def normalize(self, value: Any, *normalize_args: str) -> Any:
        """Normalize the value based on the option configuration."""
        if self.comma_separated_list and isinstance(value, str):
            value = utils.parse_comma_separated_list(value)

        if self.normalize_paths:
            if isinstance(value, list):
                value = utils.normalize_paths(value, *normalize_args)
            else:
                value = utils.normalize_path(value, *normalize_args)

        return value
예제 #9
0
파일: manager.py 프로젝트: fersure/flake8
def _flake8_normalize(value, *args, **kwargs):
    comma_separated_list = kwargs.pop("comma_separated_list", False)
    normalize_paths = kwargs.pop("normalize_paths", False)
    if kwargs:
        raise TypeError("Unexpected keyword args: {}".format(kwargs))

    if comma_separated_list and isinstance(value, utils.string_types):
        value = utils.parse_comma_separated_list(value)

    if normalize_paths:
        if isinstance(value, list):
            value = utils.normalize_paths(value, *args)
        else:
            value = utils.normalize_path(value, *args)

    return value
예제 #10
0
    def populate_style_guides_with(self, options):
        """Generate style guides from the per-file-ignores option.

        :param options:
            The original options parsed from the CLI and config file.
        :type options:
            :class:`~optparse.Values`
        :returns:
            A copy of the default style guide with overridden values.
        :rtype:
            :class:`~flake8.style_guide.StyleGuide`
        """
        for value in options.per_file_ignores:
            filename, violations_str = value.split(":")
            violations = utils.parse_comma_separated_list(violations_str)
            yield self.default_style_guide.copy(filename=filename,
                                                extend_ignore_with=violations)
예제 #11
0
def _flake8_normalize(value, *args, **kwargs):
    # type: (str, *str, **bool) -> Union[str, List[str]]
    comma_separated_list = kwargs.pop("comma_separated_list", False)
    normalize_paths = kwargs.pop("normalize_paths", False)
    if kwargs:
        raise TypeError("Unexpected keyword args: {}".format(kwargs))

    ret = value  # type: Union[str, List[str]]
    if comma_separated_list and isinstance(ret, utils.string_types):
        ret = utils.parse_comma_separated_list(value)

    if normalize_paths:
        if isinstance(ret, utils.string_types):
            ret = utils.normalize_path(ret, *args)
        else:
            ret = utils.normalize_paths(ret, *args)

    return ret
예제 #12
0
def _flake8_normalize(value: str, *args: str,
                      **kwargs: bool) -> Union[str, List[str]]:
    comma_separated_list = kwargs.pop("comma_separated_list", False)
    normalize_paths = kwargs.pop("normalize_paths", False)
    if kwargs:
        raise TypeError(f"Unexpected keyword args: {kwargs}")

    ret: Union[str, List[str]] = value
    if comma_separated_list and isinstance(ret, str):
        ret = utils.parse_comma_separated_list(value)

    if normalize_paths:
        if isinstance(ret, str):
            ret = utils.normalize_path(ret, *args)
        else:
            ret = utils.normalize_paths(ret, *args)

    return ret
예제 #13
0
def get_local_plugins(config_finder, cli_config=None, isolated=False):
    """Get local plugins lists from config files.

    :param flake8.options.config.ConfigFileFinder config_finder:
        The config file finder to use.
    :param str cli_config:
        Value of --config when specified at the command-line. Overrides
        all other config files.
    :param bool isolated:
        Determines if we should parse configuration files at all or not.
        If running in isolated mode, we ignore all configuration files
    :returns:
        LocalPlugins namedtuple containing two lists of plugin strings,
        one for extension (checker) plugins and one for report plugins.
    :rtype:
        flake8.options.config.LocalPlugins
    """
    local_plugins = LocalPlugins(extension=[], report=[])
    if isolated:
        LOG.debug('Refusing to look for local plugins in configuration'
                  'files due to user-requested isolation')
        return local_plugins

    if cli_config:
        LOG.debug(
            'Reading local plugins only from "%s" specified via '
            '--config by the user', cli_config)
        config = config_finder.cli_config(cli_config)
    else:
        config = config_finder.local_configs()

    section = '%s:local-plugins' % config_finder.program_name
    for plugin_type in ['extension', 'report']:
        if config.has_option(section, plugin_type):
            local_plugins_string = config.get(section, plugin_type).strip()
            plugin_type_list = getattr(local_plugins, plugin_type)
            plugin_type_list.extend(
                utils.parse_comma_separated_list(
                    local_plugins_string,
                    regexp=utils.LOCAL_PLUGIN_LIST_RE,
                ))
    return local_plugins
예제 #14
0
    def is_inline_ignored(self, disable_noqa):
        # type: (bool) -> bool
        """Determine if a comment has been added to ignore this line.

        :param bool disable_noqa:
            Whether or not users have provided ``--disable-noqa``.
        :returns:
            True if error is ignored in-line, False otherwise.
        :rtype:
            bool
        """
        physical_line = self.physical_line
        # TODO(sigmavirus24): Determine how to handle stdin with linecache
        if disable_noqa:
            return False

        if physical_line is None:
            physical_line = linecache.getline(self.filename, self.line_number)
        noqa_match = find_noqa(physical_line)
        if noqa_match is None:
            LOG.debug("%r is not inline ignored", self)
            return False

        codes_str = noqa_match.groupdict()["codes"]
        if codes_str is None:
            LOG.debug("%r is ignored by a blanket ``# noqa``", self)
            return True

        codes = set(utils.parse_comma_separated_list(codes_str))
        if self.code in codes or self.code.startswith(tuple(codes)):
            LOG.debug(
                "%r is ignored specifically inline with ``# noqa: %s``",
                self,
                codes_str,
            )
            return True

        LOG.debug(
            "%r is not ignored inline with ``# noqa: %s``", self, codes_str
        )
        return False
예제 #15
0
    def is_inline_ignored(self, disable_noqa):
        # type: (Violation) -> bool
        """Determine if a comment has been added to ignore this line.

        :param bool disable_noqa:
            Whether or not users have provided ``--disable-noqa``.
        :returns:
            True if error is ignored in-line, False otherwise.
        :rtype:
            bool
        """
        physical_line = self.physical_line
        # TODO(sigmavirus24): Determine how to handle stdin with linecache
        if disable_noqa:
            return False

        if physical_line is None:
            physical_line = linecache.getline(self.filename, self.line_number)
        noqa_match = find_noqa(physical_line)
        if noqa_match is None:
            LOG.debug("%r is not inline ignored", self)
            return False

        codes_str = noqa_match.groupdict()["codes"]
        if codes_str is None:
            LOG.debug("%r is ignored by a blanket ``# noqa``", self)
            return True

        codes = set(utils.parse_comma_separated_list(codes_str))
        if self.code in codes or self.code.startswith(tuple(codes)):
            LOG.debug(
                "%r is ignored specifically inline with ``# noqa: %s``",
                self,
                codes_str,
            )
            return True

        LOG.debug(
            "%r is not ignored inline with ``# noqa: %s``", self, codes_str
        )
        return False
예제 #16
0
def test_parse_comma_separated_list(value, expected):
    """Verify that similar inputs produce identical outputs."""
    assert utils.parse_comma_separated_list(value) == expected
예제 #17
0
def test_parse_comma_separated_list(value, expected):
    """Verify that similar inputs produce identical outputs."""
    assert utils.parse_comma_separated_list(value) == expected