json_reporter.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (c) 2014 Vlad Temian <vladtemian@gmail.com>
  2. # Copyright (c) 2015-2020 Claudiu Popa <pcmanticore@gmail.com>
  3. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  4. # Copyright (c) 2017 guillaume2 <guillaume.peillex@gmail.col>
  5. # Copyright (c) 2019-2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  6. # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
  7. # Copyright (c) 2020 hippo91 <guillaume.peillex@gmail.com>
  8. # Copyright (c) 2020 Clément Pit-Claudel <cpitclaudel@users.noreply.github.com>
  9. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  10. # Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  11. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  12. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  13. """JSON reporter"""
  14. import json
  15. from typing import TYPE_CHECKING, Optional
  16. from pylint.interfaces import IReporter
  17. from pylint.reporters.base_reporter import BaseReporter
  18. if TYPE_CHECKING:
  19. from pylint.lint.pylinter import PyLinter
  20. from pylint.reporters.ureports.nodes import Section
  21. class JSONReporter(BaseReporter):
  22. """Report messages and layouts in JSON."""
  23. __implements__ = IReporter
  24. name = "json"
  25. extension = "json"
  26. def display_messages(self, layout: Optional["Section"]) -> None:
  27. """Launch layouts display"""
  28. json_dumpable = [
  29. {
  30. "type": msg.category,
  31. "module": msg.module,
  32. "obj": msg.obj,
  33. "line": msg.line,
  34. "column": msg.column,
  35. "endLine": msg.end_line,
  36. "endColumn": msg.end_column,
  37. "path": msg.path,
  38. "symbol": msg.symbol,
  39. "message": msg.msg or "",
  40. "message-id": msg.msg_id,
  41. }
  42. for msg in self.messages
  43. ]
  44. print(json.dumps(json_dumpable, indent=4), file=self.out)
  45. def display_reports(self, layout: "Section") -> None:
  46. """Don't do anything in this reporter."""
  47. def _display(self, layout: "Section") -> None:
  48. """Do nothing."""
  49. def register(linter: "PyLinter") -> None:
  50. """Register the reporter classes with the linter."""
  51. linter.register_reporter(JSONReporter)