pylama_pycodestyle.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """pycodestyle support."""
  2. from pycodestyle import BaseReport, Checker, StyleGuide, get_parser
  3. from pylama.context import RunContext
  4. from pylama.lint import LinterV2 as Abstract
  5. class Linter(Abstract):
  6. """pycodestyle runner."""
  7. name = "pycodestyle"
  8. def run_check(self, ctx: RunContext): # noqa
  9. """Check code with pycodestyle."""
  10. params = ctx.get_params("pycodestyle")
  11. options = ctx.options
  12. if options:
  13. params.setdefault("max_line_length", options.max_line_length)
  14. if params:
  15. parser = get_parser()
  16. for option in parser.option_list:
  17. if option.dest and option.dest in params:
  18. value = params[option.dest]
  19. if isinstance(value, str):
  20. params[option.dest] = option.convert_value(option, value)
  21. style = StyleGuide(reporter=_PycodestyleReport, **params)
  22. options = style.options
  23. options.report.ctx = ctx # type: ignore
  24. checker = Checker(ctx.filename, lines=ctx.lines, options=options)
  25. checker.check_all()
  26. class _PycodestyleReport(BaseReport):
  27. ctx: RunContext
  28. def error(self, line_number, offset, text, _):
  29. """Save errors."""
  30. code, _, text = text.partition(" ")
  31. self.ctx.push(
  32. text=text,
  33. type=code[0],
  34. number=code,
  35. col=offset + 1,
  36. lnum=line_number,
  37. source="pycodestyle",
  38. )