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
def display_errors(errors: List[Error], options: Namespace): """Format and display the given errors.""" if options.format == "json": LOGGER.warning(dumps([err.to_dict() for err in errors])) else: pattern = MESSAGE_FORMATS.get(options.format, DEFAULT_FORMAT) for err in errors: LOGGER.warning(err.format(pattern))
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
def code_check(): """ Run pylama and check current file. :return bool: """ with silence_stderr(): from pylama.main import parse_options from pylama.tasks import check_path if not env.curbuf.name: return env.stop() options = parse_options( ignore=env.var('g:pymode_lint_ignore'), select=env.var('g:pymode_lint_select'), linters=env.var('g:pymode_lint_checkers'), ) path = os.path.relpath(env.curbuf.name, env.curdir) env.debug("Start code check: ", path) if getattr(options, 'skip', None) and any( p.match(path) for p in options.skip): # noqa env.message('Skip code checking.') env.debug("Skipped") return env.stop() if env.options.get('debug'): from pylama.core import LOGGER, logging LOGGER.setLevel(logging.DEBUG) errors = check_path(path, options=options, code='\n'.join(env.curbuf) + '\n') env.debug("Find errors: ", len(errors)) sort_rules = env.var('g:pymode_lint_sort') def __sort(e): try: return sort_rules.index(e.get('type')) except ValueError: return 999 if sort_rules: env.debug("Find sorting: ", sort_rules) errors = sorted(errors, key=__sort) for e in errors: e['bufnr'] = env.curbuf.number env.run('g:PymodeLocList.current().extend', errors)
def code_check(): """ Run pylama and check current file. :return bool: """ with silence_stderr(): from pylama.main import parse_options from pylama.tasks import check_path if not env.curbuf.name: return env.stop() options = parse_options( ignore=env.var('g:pymode_lint_ignore'), select=env.var('g:pymode_lint_select'), linters=env.var('g:pymode_lint_checkers'), ) env.debug(options) path = os.path.relpath(env.curbuf.name, env.curdir) env.debug("Start code check: ", path) if getattr(options, 'skip', None) and any(p.match(path) for p in options.skip): # noqa env.message('Skip code checking.') env.debug("Skipped") return env.stop() if env.options.get('debug'): from pylama.core import LOGGER, logging LOGGER.setLevel(logging.DEBUG) errors = check_path( path, options=options, code='\n'.join(env.curbuf) + '\n') env.debug("Find errors: ", len(errors)) sort_rules = env.var('g:pymode_lint_sort') def __sort(e): try: return sort_rules.index(e.get('type')) except ValueError: return 999 if sort_rules: env.debug("Find sorting: ", sort_rules) errors = sorted(errors, key=__sort) for e in errors: e['bufnr'] = env.curbuf.number env.run('g:PymodeLocList.current().extend', errors)
def process_paths(options, candidates=None, error=True): """Process files and log errors.""" errors = pylama.check_path(options, rootdir=pylamaconfig.CURDIR, candidates=candidates) pattern = "%(filename)s:%(lnum)s:%(col)s:%(type)s:%(number)s:%(text)s" for er in errors: text = er._info["text"].split() if PATTERN_NUMBER.match(text[0]): er._info["text"] = " ".join(text[1:]) if not er._info["number"]: er._info["number"] = f"0000" if options.abspath: er._info["filename"] = ospath.abspath(er.filename) LOGGER.warning(pattern, er._info) if error: sys.exit(int(bool(errors))) return errors
def code_check(): """Run pylama and check current file. :return bool: """ with silence_stderr(): from pylama.core import run from pylama.config import parse_options if not env.curbuf.name: return env.stop() linters = env.var('g:pymode_lint_checkers') env.debug(linters) # Fixed in v0.9.3: these two parameters may be passed as strings. # DEPRECATE: v:0.10.0: need to be set as lists. if isinstance(env.var('g:pymode_lint_ignore'), str): raise ValueError ('g:pymode_lint_ignore should have a list type') else: ignore = env.var('g:pymode_lint_ignore') if isinstance(env.var('g:pymode_lint_select'), str): raise ValueError ('g:pymode_lint_select should have a list type') else: select = env.var('g:pymode_lint_select') options = parse_options( linters=linters, force=1, ignore=ignore, select=select, ) env.debug(options) for linter in linters: opts = env.var('g:pymode_lint_options_%s' % linter, silence=True) if opts: options.linters_params[linter] = options.linters_params.get( linter, {}) options.linters_params[linter].update(opts) path = os.path.relpath(env.curbuf.name, env.curdir) env.debug("Start code check: ", path) if getattr(options, 'skip', None) and any(p.match(path) for p in options.skip): # noqa env.message('Skip code checking.') env.debug("Skipped") return env.stop() if env.options.get('debug'): from pylama.core import LOGGER, logging LOGGER.setLevel(logging.DEBUG) errors = run(path, code='\n'.join(env.curbuf) + '\n', options=options) env.debug("Find errors: ", len(errors)) sort_rules = env.var('g:pymode_lint_sort') def __sort(e): try: return sort_rules.index(e.get('type')) except ValueError: return 999 if sort_rules: env.debug("Find sorting: ", sort_rules) errors = sorted(errors, key=__sort) for e in errors: e._info['bufnr'] = env.curbuf.number if e._info['col'] is None: e._info['col'] = 1 env.run('g:PymodeLocList.current().extend', [e._info for e in errors])
def code_check(): # noqa """Run pylama and check current file. :return bool: """ with silence_stderr(): from pylama.core import run from pylama.main import parse_options from pylama.config import _override_options if not env.curbuf.name: return env.stop() options = parse_options( force=1, ignore=env.var('g:pymode_lint_ignore'), select=env.var('g:pymode_lint_select'), ) linters = env.var('g:pymode_lint_checkers', default=[]) if linters: _override_options(options, linters=",".join(linters)) for linter in dict(options.linters): opts = env.var('g:pymode_lint_options_%s' % linter, silence=True) if opts: options.linters_params[ linter] = options.linters_params.get(linter, {}) options.linters_params[linter].update(opts) if 'pylint' in options.linters_params: options.linters_params['pylint']['clear_cache'] = True env.debug(options) path = os.path.relpath(env.curbuf.name, env.curdir) env.debug("Start code check: ", path) if getattr(options, 'skip', None) and any( p.match(path) for p in options.skip): # noqa env.message('Skip code checking.') env.debug("Skipped") return env.stop() if env.options.get('debug'): from pylama.core import LOGGER, logging LOGGER.setLevel(logging.DEBUG) errors = run(path, code='\n'.join(env.curbuf) + '\n', options=options) env.debug("Find errors: ", len(errors)) sort_rules = env.var('g:pymode_lint_sort') def __sort(e): try: return sort_rules.index(e.get('type')) except ValueError: return 999 if sort_rules: env.debug("Find sorting: ", sort_rules) errors = sorted(errors, key=__sort) for e in errors: e._info['bufnr'] = env.curbuf.number if e._info['col'] is None: e._info['col'] = 1 env.run('g:PymodeLocList.current().extend', [e._info for e in errors])
def code_check(): """Run pylama and check current file. :return bool: """ with silence_stderr(): from pylama.core import run from pylama.main import parse_options if not env.curbuf.name: return env.stop() linters = env.var('g:pymode_lint_checkers') env.debug(linters) options = parse_options( linters=linters, force=1, ignore=env.var('g:pymode_lint_ignore'), select=env.var('g:pymode_lint_select'), ) for linter in linters: opts = env.var('g:pymode_lint_options_%s' % linter, silence=True) if opts: options.linters_params[linter] = options.linters_params.get(linter, {}) options.linters_params[linter].update(opts) options.linters_params['pylint']['clear_cache'] = True env.debug(options) path = os.path.relpath(env.curbuf.name, env.curdir) env.debug("Start code check: ", path) if getattr(options, 'skip', None) and any(p.match(path) for p in options.skip): # noqa env.message('Skip code checking.') env.debug("Skipped") return env.stop() if env.options.get('debug'): from pylama.core import LOGGER, logging LOGGER.setLevel(logging.DEBUG) errors = run(path, code='\n'.join(env.curbuf) + '\n', options=options) env.debug("Find errors: ", len(errors)) sort_rules = env.var('g:pymode_lint_sort') def __sort(e): try: return sort_rules.index(e.get('type')) except ValueError: return 999 if sort_rules: env.debug("Find sorting: ", sort_rules) errors = sorted(errors, key=__sort) for e in errors: e._info['bufnr'] = env.curbuf.number if e._info['col'] is None: e._info['col'] = 1 env.run('g:PymodeLocList.current().extend', [e._info for e in errors])