docs.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  3. """Various helper functions to create the docs of a linter object"""
  4. import sys
  5. from typing import TYPE_CHECKING, Dict, TextIO
  6. from pylint.constants import MAIN_CHECKER_NAME
  7. from pylint.utils.utils import get_rst_section, get_rst_title
  8. if TYPE_CHECKING:
  9. from pylint.lint.pylinter import PyLinter
  10. def _get_checkers_infos(linter: "PyLinter") -> Dict[str, Dict]:
  11. """Get info from a checker and handle KeyError"""
  12. by_checker: Dict[str, Dict] = {}
  13. for checker in linter.get_checkers():
  14. name = checker.name
  15. if name != "master":
  16. try:
  17. by_checker[name]["checker"] = checker
  18. by_checker[name]["options"] += checker.options_and_values()
  19. by_checker[name]["msgs"].update(checker.msgs)
  20. by_checker[name]["reports"] += checker.reports
  21. except KeyError:
  22. by_checker[name] = {
  23. "checker": checker,
  24. "options": list(checker.options_and_values()),
  25. "msgs": dict(checker.msgs),
  26. "reports": list(checker.reports),
  27. }
  28. return by_checker
  29. def _get_checkers_documentation(linter: "PyLinter") -> str:
  30. """Get documentation for individual checkers"""
  31. result = get_rst_title("Pylint global options and switches", "-")
  32. result += """
  33. Pylint provides global options and switches.
  34. """
  35. for checker in linter.get_checkers():
  36. name = checker.name
  37. if name == MAIN_CHECKER_NAME:
  38. if checker.options:
  39. for section, options in checker.options_by_section():
  40. if section is None:
  41. title = "General options"
  42. else:
  43. title = f"{section.capitalize()} options"
  44. result += get_rst_title(title, "~")
  45. result += f"{get_rst_section(None, options)}\n"
  46. result += get_rst_title("Pylint checkers' options and switches", "-")
  47. result += """\
  48. Pylint checkers can provide three set of features:
  49. * options that control their execution,
  50. * messages that they can raise,
  51. * reports that they can generate.
  52. Below is a list of all checkers and their features.
  53. """
  54. by_checker = _get_checkers_infos(linter)
  55. for checker in sorted(by_checker):
  56. information = by_checker[checker]
  57. checker = information["checker"]
  58. del information["checker"]
  59. result += checker.get_full_documentation(**information)
  60. return result
  61. def print_full_documentation(linter: "PyLinter", stream: TextIO = sys.stdout) -> None:
  62. """Output a full documentation in ReST format"""
  63. print(_get_checkers_documentation(linter)[:-1], file=stream)