diff options
author | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-10-07 00:06:31 +0200 |
---|---|---|
committer | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-10-07 17:22:33 +0200 |
commit | c4ed76d3948afdb0176835c1fa6eb48f54bfd54f (patch) | |
tree | f65896a555e4f8e89b278c94863bdf30773cab55 | |
parent | 05d3e6c6e41e0aec9cf24c101a9a01f1046f61f6 (diff) | |
download | pylint-git-c4ed76d3948afdb0176835c1fa6eb48f54bfd54f.tar.gz |
Move docs helper functions out of ``MessagesHandlerMixIn``
-rwxr-xr-x | doc/exts/pylint_features.py | 3 | ||||
-rw-r--r-- | pylint/lint/run.py | 4 | ||||
-rw-r--r-- | pylint/message/message_handler_mix_in.py | 66 | ||||
-rw-r--r-- | pylint/utils/__init__.py | 2 | ||||
-rw-r--r-- | pylint/utils/docs.py | 77 | ||||
-rw-r--r-- | tests/lint/unittest_lint.py | 4 |
6 files changed, 87 insertions, 69 deletions
diff --git a/doc/exts/pylint_features.py b/doc/exts/pylint_features.py index 7a296986c..a867dd05f 100755 --- a/doc/exts/pylint_features.py +++ b/doc/exts/pylint_features.py @@ -9,6 +9,7 @@ import os import sphinx from pylint.lint import PyLinter +from pylint.utils import print_full_documentation def builder_inited(app): @@ -23,7 +24,7 @@ def builder_inited(app): stream.write("Pylint features\n") stream.write("===============\n\n") stream.write(".. generated by pylint --full-documentation\n\n") - linter.print_full_documentation(stream) + print_full_documentation(linter, stream) def setup(app): diff --git a/pylint/lint/run.py b/pylint/lint/run.py index d84ed2703..1555400cd 100644 --- a/pylint/lint/run.py +++ b/pylint/lint/run.py @@ -9,7 +9,7 @@ from pylint import __pkginfo__, config, extensions, interfaces from pylint.constants import full_version from pylint.lint.pylinter import PyLinter from pylint.lint.utils import ArgumentPreprocessingError, preprocess_options -from pylint.utils import utils +from pylint.utils import print_full_documentation, utils try: import multiprocessing @@ -436,7 +436,7 @@ group are mutually exclusive.", def cb_full_documentation(self, option, optname, value, parser): """optik callback for printing full documentation""" - self.linter.print_full_documentation() + print_full_documentation(self.linter) sys.exit(0) def cb_list_messages(self, option, optname, value, parser): diff --git a/pylint/message/message_handler_mix_in.py b/pylint/message/message_handler_mix_in.py index 3391bd1b1..d7f367a32 100644 --- a/pylint/message/message_handler_mix_in.py +++ b/pylint/message/message_handler_mix_in.py @@ -1,14 +1,12 @@ # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE -import sys -from typing import TYPE_CHECKING, Any, List, Optional, TextIO, Tuple, Union +from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union from astroid import nodes from pylint import exceptions, interfaces from pylint.constants import ( - MAIN_CHECKER_NAME, MSG_STATE_CONFIDENCE, MSG_STATE_SCOPE_CONFIG, MSG_STATE_SCOPE_MODULE, @@ -17,7 +15,7 @@ from pylint.constants import ( MSG_TYPES_STATUS, ) from pylint.message.message import Message -from pylint.utils import get_module_and_frameid, get_rst_section, get_rst_title +from pylint.utils import get_module_and_frameid if TYPE_CHECKING: from pylint.lint.pylinter import PyLinter @@ -336,63 +334,3 @@ class MessagesHandlerMixIn: confidence, ) ) - - def _get_checkers_infos(self): - by_checker = {} - for checker in self.get_checkers(): - name = checker.name - if name != "master": - try: - by_checker[name]["checker"] = checker - by_checker[name]["options"] += checker.options_and_values() - by_checker[name]["msgs"].update(checker.msgs) - by_checker[name]["reports"] += checker.reports - except KeyError: - by_checker[name] = { - "checker": checker, - "options": list(checker.options_and_values()), - "msgs": dict(checker.msgs), - "reports": list(checker.reports), - } - return by_checker - - def get_checkers_documentation(self): - result = get_rst_title("Pylint global options and switches", "-") - result += """ -Pylint provides global options and switches. - -""" - for checker in self.get_checkers(): - name = checker.name - if name == MAIN_CHECKER_NAME: - if checker.options: - for section, options in checker.options_by_section(): - if section is None: - title = "General options" - else: - title = f"{section.capitalize()} options" - result += get_rst_title(title, "~") - result += f"{get_rst_section(None, options)}\n" - result += get_rst_title("Pylint checkers' options and switches", "-") - result += """\ - -Pylint checkers can provide three set of features: - -* options that control their execution, -* messages that they can raise, -* reports that they can generate. - -Below is a list of all checkers and their features. - -""" - by_checker = self._get_checkers_infos() - for checker in sorted(by_checker): - information = by_checker[checker] - checker = information["checker"] - del information["checker"] - result += checker.get_full_documentation(**information) - return result - - def print_full_documentation(self, stream: TextIO = sys.stdout) -> None: - """output a full documentation in ReST format""" - print(self.get_checkers_documentation()[:-1], file=stream) diff --git a/pylint/utils/__init__.py b/pylint/utils/__init__.py index 73f2e0fa0..aed86387b 100644 --- a/pylint/utils/__init__.py +++ b/pylint/utils/__init__.py @@ -44,6 +44,7 @@ main pylint class """ from pylint.utils.ast_walker import ASTWalker +from pylint.utils.docs import print_full_documentation from pylint.utils.file_state import FileState from pylint.utils.linterstats import LinterStats, ModuleStats, merge_stats from pylint.utils.utils import ( @@ -87,4 +88,5 @@ __all__ = [ "merge_stats", "LinterStats", "ModuleStats", + "print_full_documentation", ] diff --git a/pylint/utils/docs.py b/pylint/utils/docs.py new file mode 100644 index 000000000..5b46a0bc1 --- /dev/null +++ b/pylint/utils/docs.py @@ -0,0 +1,77 @@ +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE +"""Various helper functions to create the docs of a linter object""" + +import sys +from typing import TYPE_CHECKING, Dict, TextIO + +from pylint.constants import MAIN_CHECKER_NAME +from pylint.utils.utils import get_rst_section, get_rst_title + +if TYPE_CHECKING: + from pylint.lint.pylinter import PyLinter + + +def _get_checkers_infos(linter: "PyLinter") -> Dict[str, Dict]: + """Get info from a checker and handle KeyError""" + by_checker: Dict[str, Dict] = {} + for checker in linter.get_checkers(): + name = checker.name + if name != "master": + try: + by_checker[name]["checker"] = checker + by_checker[name]["options"] += checker.options_and_values() + by_checker[name]["msgs"].update(checker.msgs) + by_checker[name]["reports"] += checker.reports + except KeyError: + by_checker[name] = { + "checker": checker, + "options": list(checker.options_and_values()), + "msgs": dict(checker.msgs), + "reports": list(checker.reports), + } + return by_checker + + +def _get_checkers_documentation(linter: "PyLinter") -> str: + """Get documentation for individual checkers""" + result = get_rst_title("Pylint global options and switches", "-") + result += """ +Pylint provides global options and switches. + +""" + for checker in linter.get_checkers(): + name = checker.name + if name == MAIN_CHECKER_NAME: + if checker.options: + for section, options in checker.options_by_section(): + if section is None: + title = "General options" + else: + title = f"{section.capitalize()} options" + result += get_rst_title(title, "~") + result += f"{get_rst_section(None, options)}\n" + result += get_rst_title("Pylint checkers' options and switches", "-") + result += """\ + +Pylint checkers can provide three set of features: + +* options that control their execution, +* messages that they can raise, +* reports that they can generate. + +Below is a list of all checkers and their features. + +""" + by_checker = _get_checkers_infos(linter) + for checker in sorted(by_checker): + information = by_checker[checker] + checker = information["checker"] + del information["checker"] + result += checker.get_full_documentation(**information) + return result + + +def print_full_documentation(linter: "PyLinter", stream: TextIO = sys.stdout) -> None: + """Output a full documentation in ReST format""" + print(_get_checkers_documentation(linter)[:-1], file=stream) diff --git a/tests/lint/unittest_lint.py b/tests/lint/unittest_lint.py index 79437e987..f8a9ba412 100644 --- a/tests/lint/unittest_lint.py +++ b/tests/lint/unittest_lint.py @@ -66,7 +66,7 @@ from pylint.lint import ArgumentPreprocessingError, PyLinter, Run, preprocess_op from pylint.message import Message from pylint.reporters import text from pylint.typing import MessageLocationTuple -from pylint.utils import FileState, tokenize_module +from pylint.utils import FileState, print_full_documentation, tokenize_module if os.name == "java": # pylint: disable=no-member @@ -621,7 +621,7 @@ def test_analyze_explicit_script(linter: PyLinter) -> None: def test_full_documentation(linter: PyLinter) -> None: out = StringIO() - linter.print_full_documentation(out) + print_full_documentation(linter, out) output = out.getvalue() # A few spot checks only for re_str in ( |