Example #1
0
def shell(args: List[str] = None, error: bool = True):
    """Endpoint for console.

    Parse a command arguments, configuration files and run a checkers.
    """
    if args is None:
        args = sys.argv[1:]

    options = parse_options(args)
    setup_logger(options)
    LOGGER.info(options)

    # Install VSC hook
    if options.hook:
        from .hook import install_hook  # noqa

        for path in options.paths:
            return install_hook(path)

    if options.from_stdin and not options.paths:
        LOGGER.error("--from-stdin requires a filename")
        return sys.exit(1)

    errors = check_paths(
        options.paths,
        code=read_stdin() if options.from_stdin else None,
        options=options,
        rootdir=CURDIR,
    )
    display_errors(errors, options)

    if error:
        sys.exit(int(bool(errors)))

    return errors
Example #2
0
    def parse_options(args=None, config=True, rootdir=pylamaconfig.CURDIR, **overrides):
        """ Parse options from command line and configuration files.
        :return argparse.Namespace:
        """
        args = args or []

        # Parse args from command string
        options = pylamaconfig.PARSER.parse_args(args)
        options.file_params = dict()
        options.linters_params = dict()

        # Compile options from ini
        if config:
            cfg = get_config(str(options.options), rootdir=rootdir)
            for opt, val in cfg.default.items():
                LOGGER.info("Find option %s (%s)", opt, val)
                passed_value = getattr(options, opt, pylamaconfig._Default())
                if isinstance(passed_value, pylamaconfig._Default):
                    if opt == "paths":
                        val = val.split()
                    elif opt == "skip":
                        val = pylamaconfig.fix_pathname_sep(val)
                    setattr(options, opt, pylamaconfig._Default(val))

            # Parse file related options
            for name, opts in cfg.sections.items():

                if name == cfg.default_section:
                    continue

                if name.startswith(cfg.default_section):
                    name = name[7:]

                if name in pylamaconfig.LINTERS:
                    options.linters_params[name] = dict(opts)
                    continue

                mask = re.compile(
                    pylamaconfig.fnmatch.translate(pylamaconfig.fix_pathname_sep(name))
                )
                options.file_params[mask] = dict(opts)

        # Override options
        pylamaconfig._override_options(options, **overrides)

        # Postprocess options
        for name in options.__dict__:
            value = getattr(options, name)
            if isinstance(value, pylamaconfig._Default):
                setattr(options, name, pylamaconfig.process_value(name, value.value))

        if options.concurrent and "pylint" in options.linters:
            LOGGER.warning("Can't parse code asynchronously with pylint enabled.")
            options.concurrent = False

        return options