docs.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. """Various helper functions to create the docs of a linter object."""
  5. from __future__ import annotations
  6. import sys
  7. from typing import TYPE_CHECKING, Any, TextIO
  8. from pylint.constants import MAIN_CHECKER_NAME
  9. from pylint.utils.utils import get_rst_section, get_rst_title
  10. if TYPE_CHECKING:
  11. from pylint.lint.pylinter import PyLinter
  12. def _get_checkers_infos(linter: PyLinter) -> dict[str, dict[str, Any]]:
  13. """Get info from a checker and handle KeyError."""
  14. by_checker: dict[str, dict[str, Any]] = {}
  15. for checker in linter.get_checkers():
  16. name = checker.name
  17. if name != MAIN_CHECKER_NAME:
  18. try:
  19. by_checker[name]["checker"] = checker
  20. by_checker[name]["options"] += checker._options_and_values()
  21. by_checker[name]["msgs"].update(checker.msgs)
  22. by_checker[name]["reports"] += checker.reports
  23. except KeyError:
  24. by_checker[name] = {
  25. "checker": checker,
  26. "options": list(checker._options_and_values()),
  27. "msgs": dict(checker.msgs),
  28. "reports": list(checker.reports),
  29. }
  30. return by_checker
  31. def _get_global_options_documentation(linter: PyLinter) -> str:
  32. """Get documentation for the main checker."""
  33. result = get_rst_title("Pylint global options and switches", "-")
  34. result += """
  35. Pylint provides global options and switches.
  36. """
  37. for checker in linter.get_checkers():
  38. if checker.name == MAIN_CHECKER_NAME and 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. assert isinstance(options, list)
  46. result += f"{get_rst_section(None, options)}\n"
  47. return result
  48. def _get_checkers_documentation(linter: PyLinter, show_options: bool = True) -> str:
  49. """Get documentation for individual checkers."""
  50. if show_options:
  51. result = _get_global_options_documentation(linter)
  52. else:
  53. result = ""
  54. result += get_rst_title("Pylint checkers' options and switches", "-")
  55. result += """\
  56. Pylint checkers can provide three set of features:
  57. * options that control their execution,
  58. * messages that they can raise,
  59. * reports that they can generate.
  60. Below is a list of all checkers and their features.
  61. """
  62. by_checker = _get_checkers_infos(linter)
  63. for checker_name in sorted(by_checker):
  64. information = by_checker[checker_name]
  65. checker = information["checker"]
  66. del information["checker"]
  67. result += checker.get_full_documentation(
  68. **information, show_options=show_options
  69. )
  70. return result
  71. def print_full_documentation(
  72. linter: PyLinter, stream: TextIO = sys.stdout, show_options: bool = True
  73. ) -> None:
  74. """Output a full documentation in ReST format."""
  75. print(
  76. _get_checkers_documentation(linter, show_options=show_options)[:-3], file=stream
  77. )