docs.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. import warnings
  8. from typing import TYPE_CHECKING, Any, TextIO
  9. from pylint.constants import MAIN_CHECKER_NAME
  10. from pylint.utils.utils import get_rst_section, get_rst_title
  11. if TYPE_CHECKING:
  12. from pylint.lint.pylinter import PyLinter
  13. def _get_checkers_infos(linter: PyLinter) -> dict[str, dict[str, Any]]:
  14. """Get info from a checker and handle KeyError."""
  15. by_checker: dict[str, dict[str, Any]] = {}
  16. for checker in linter.get_checkers():
  17. name = checker.name
  18. if name != MAIN_CHECKER_NAME:
  19. try:
  20. by_checker[name]["checker"] = checker
  21. with warnings.catch_warnings():
  22. warnings.filterwarnings("ignore", category=DeprecationWarning)
  23. by_checker[name]["options"] += checker.options_and_values()
  24. by_checker[name]["msgs"].update(checker.msgs)
  25. by_checker[name]["reports"] += checker.reports
  26. except KeyError:
  27. with warnings.catch_warnings():
  28. warnings.filterwarnings("ignore", category=DeprecationWarning)
  29. by_checker[name] = {
  30. "checker": checker,
  31. "options": list(checker.options_and_values()),
  32. "msgs": dict(checker.msgs),
  33. "reports": list(checker.reports),
  34. }
  35. return by_checker
  36. def _get_global_options_documentation(linter: PyLinter) -> str:
  37. """Get documentation for the main checker."""
  38. result = get_rst_title("Pylint global options and switches", "-")
  39. result += """
  40. Pylint provides global options and switches.
  41. """
  42. for checker in linter.get_checkers():
  43. if checker.name == MAIN_CHECKER_NAME and checker.options:
  44. with warnings.catch_warnings():
  45. warnings.filterwarnings("ignore", category=DeprecationWarning)
  46. for section, options in checker.options_by_section():
  47. if section is None:
  48. title = "General options"
  49. else:
  50. title = f"{section.capitalize()} options"
  51. result += get_rst_title(title, "~")
  52. assert isinstance(options, list)
  53. result += f"{get_rst_section(None, options)}\n"
  54. return result
  55. def _get_checkers_documentation(linter: PyLinter, show_options: bool = True) -> str:
  56. """Get documentation for individual checkers."""
  57. if show_options:
  58. result = _get_global_options_documentation(linter)
  59. else:
  60. result = ""
  61. result += get_rst_title("Pylint checkers' options and switches", "-")
  62. result += """\
  63. Pylint checkers can provide three set of features:
  64. * options that control their execution,
  65. * messages that they can raise,
  66. * reports that they can generate.
  67. Below is a list of all checkers and their features.
  68. """
  69. by_checker = _get_checkers_infos(linter)
  70. for checker_name in sorted(by_checker):
  71. information = by_checker[checker_name]
  72. checker = information["checker"]
  73. del information["checker"]
  74. result += checker.get_full_documentation(
  75. **information, show_options=show_options
  76. )
  77. return result
  78. def print_full_documentation(
  79. linter: PyLinter, stream: TextIO = sys.stdout, show_options: bool = True
  80. ) -> None:
  81. """Output a full documentation in ReST format."""
  82. print(
  83. _get_checkers_documentation(linter, show_options=show_options)[:-3], file=stream
  84. )