diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-09-14 16:06:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-14 16:06:48 +0200 |
commit | de9af75104f74f954f3eae0085cc0c400f04511d (patch) | |
tree | d0f49091721f57cadc82ef7905e8c7067e1ab04b | |
parent | bc95cd34071ec2e71de5bca8ff95cc9b88e23814 (diff) | |
download | pylint-git-de9af75104f74f954f3eae0085cc0c400f04511d.tar.gz |
Pylint reporter cleanup (#5003)
* Create a file for pylint.reporters.BaseWritter
* Calculate visitor name only once in Reporter nodes
Remove dead code that was always failing and replace it by
an attribute directly.
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
-rw-r--r-- | pylint/reporters/ureports/__init__.py | 96 | ||||
-rw-r--r-- | pylint/reporters/ureports/base_writer.py | 98 | ||||
-rw-r--r-- | pylint/reporters/ureports/nodes.py | 18 | ||||
-rw-r--r-- | tests/pyreverse/test_writer.py | 2 |
4 files changed, 104 insertions, 110 deletions
diff --git a/pylint/reporters/ureports/__init__.py b/pylint/reporters/ureports/__init__.py index f0651f98c..29bc85d89 100644 --- a/pylint/reporters/ureports/__init__.py +++ b/pylint/reporters/ureports/__init__.py @@ -1,98 +1,6 @@ -# Copyright (c) 2015-2016, 2018, 2020 Claudiu Popa <pcmanticore@gmail.com> -# Copyright (c) 2018 ssolanki <sushobhitsolanki@gmail.com> -# Copyright (c) 2018 Sushobhit <31987769+sushobhit27@users.noreply.github.com> -# Copyright (c) 2018 Anthony Sottile <asottile@umich.edu> -# Copyright (c) 2019, 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com> -# Copyright (c) 2020 hippo91 <guillaume.peillex@gmail.com> -# Copyright (c) 2021 Nick Drozd <nicholasdrozd@gmail.com> -# Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com> - # 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 -"""Universal report objects and some formatting drivers. - -A way to create simple reports using python objects, primarily designed to be -formatted as text and html. -""" -import os -import sys -from io import StringIO -from typing import Iterator, TextIO - - -class BaseWriter: - """base class for ureport writers""" - - def format(self, layout, stream: TextIO = sys.stdout, encoding=None) -> None: - """format and write the given layout into the stream object - - unicode policy: unicode strings may be found in the layout; - try to call stream.write with it, but give it back encoded using - the given encoding if it fails - """ - if not encoding: - encoding = getattr(stream, "encoding", "UTF-8") - self.encoding = encoding or "UTF-8" - self.out = stream - self.begin_format() - layout.accept(self) - self.end_format() - - def format_children(self, layout): - """recurse on the layout children and call their accept method - (see the Visitor pattern) - """ - for child in getattr(layout, "children", ()): - child.accept(self) - - def writeln(self, string=""): - """write a line in the output buffer""" - self.write(string + os.linesep) - - def write(self, string): - """write a string in the output buffer""" - self.out.write(string) - - def begin_format(self): - """begin to format a layout""" - self.section = 0 - - def end_format(self): - """finished to format a layout""" - - def get_table_content(self, table): - """trick to get table content without actually writing it - - return an aligned list of lists containing table cells values as string - """ - result = [[]] - cols = table.cols - for cell in self.compute_content(table): - if cols == 0: - result.append([]) - cols = table.cols - cols -= 1 - result[-1].append(cell) - # fill missing cells - result[-1] += [""] * (cols - len(result[-1])) - return result - - def compute_content(self, layout) -> Iterator[str]: - """trick to compute the formatting of children layout before actually - writing it +__all__ = ("BaseWriter",) - return an iterator on strings (one for each child element) - """ - # Patch the underlying output stream with a fresh-generated stream, - # which is used to store a temporary representation of a child - # node. - out = self.out - try: - for child in layout.children: - stream = StringIO() - self.out = stream - child.accept(self) - yield stream.getvalue() - finally: - self.out = out +from pylint.reporters.ureports.base_writer import BaseWriter diff --git a/pylint/reporters/ureports/base_writer.py b/pylint/reporters/ureports/base_writer.py new file mode 100644 index 000000000..f0651f98c --- /dev/null +++ b/pylint/reporters/ureports/base_writer.py @@ -0,0 +1,98 @@ +# Copyright (c) 2015-2016, 2018, 2020 Claudiu Popa <pcmanticore@gmail.com> +# Copyright (c) 2018 ssolanki <sushobhitsolanki@gmail.com> +# Copyright (c) 2018 Sushobhit <31987769+sushobhit27@users.noreply.github.com> +# Copyright (c) 2018 Anthony Sottile <asottile@umich.edu> +# Copyright (c) 2019, 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com> +# Copyright (c) 2020 hippo91 <guillaume.peillex@gmail.com> +# Copyright (c) 2021 Nick Drozd <nicholasdrozd@gmail.com> +# Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com> + +# 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 + +"""Universal report objects and some formatting drivers. + +A way to create simple reports using python objects, primarily designed to be +formatted as text and html. +""" +import os +import sys +from io import StringIO +from typing import Iterator, TextIO + + +class BaseWriter: + """base class for ureport writers""" + + def format(self, layout, stream: TextIO = sys.stdout, encoding=None) -> None: + """format and write the given layout into the stream object + + unicode policy: unicode strings may be found in the layout; + try to call stream.write with it, but give it back encoded using + the given encoding if it fails + """ + if not encoding: + encoding = getattr(stream, "encoding", "UTF-8") + self.encoding = encoding or "UTF-8" + self.out = stream + self.begin_format() + layout.accept(self) + self.end_format() + + def format_children(self, layout): + """recurse on the layout children and call their accept method + (see the Visitor pattern) + """ + for child in getattr(layout, "children", ()): + child.accept(self) + + def writeln(self, string=""): + """write a line in the output buffer""" + self.write(string + os.linesep) + + def write(self, string): + """write a string in the output buffer""" + self.out.write(string) + + def begin_format(self): + """begin to format a layout""" + self.section = 0 + + def end_format(self): + """finished to format a layout""" + + def get_table_content(self, table): + """trick to get table content without actually writing it + + return an aligned list of lists containing table cells values as string + """ + result = [[]] + cols = table.cols + for cell in self.compute_content(table): + if cols == 0: + result.append([]) + cols = table.cols + cols -= 1 + result[-1].append(cell) + # fill missing cells + result[-1] += [""] * (cols - len(result[-1])) + return result + + def compute_content(self, layout) -> Iterator[str]: + """trick to compute the formatting of children layout before actually + writing it + + return an iterator on strings (one for each child element) + """ + # Patch the underlying output stream with a fresh-generated stream, + # which is used to store a temporary representation of a child + # node. + out = self.out + try: + for child in layout.children: + stream = StringIO() + self.out = stream + child.accept(self) + yield stream.getvalue() + finally: + self.out = out diff --git a/pylint/reporters/ureports/nodes.py b/pylint/reporters/ureports/nodes.py index d359093b8..3f7842a18 100644 --- a/pylint/reporters/ureports/nodes.py +++ b/pylint/reporters/ureports/nodes.py @@ -22,6 +22,7 @@ class VNode: # navigation self.parent = None self.children = [] + self.visitor_name = self.__class__.__name__.lower() def __iter__(self): return iter(self.children) @@ -36,25 +37,12 @@ class VNode: self.children.insert(index, child) child.parent = self - def _get_visit_name(self): - """ - return the visit name for the mixed class. When calling 'accept', the - method <'visit_' + name returned by this method> will be called on the - visitor - """ - try: - # pylint: disable=no-member - return self.TYPE.replace("-", "_") - # pylint: disable=broad-except - except Exception: - return self.__class__.__name__.lower() - def accept(self, visitor, *args, **kwargs): - func = getattr(visitor, f"visit_{self._get_visit_name()}") + func = getattr(visitor, f"visit_{self.visitor_name}") return func(self, *args, **kwargs) def leave(self, visitor, *args, **kwargs): - func = getattr(visitor, f"leave_{self._get_visit_name()}") + func = getattr(visitor, f"leave_{self.visitor_name}") return func(self, *args, **kwargs) diff --git a/tests/pyreverse/test_writer.py b/tests/pyreverse/test_writer.py index 28ed16046..9c847be29 100644 --- a/tests/pyreverse/test_writer.py +++ b/tests/pyreverse/test_writer.py @@ -28,7 +28,7 @@ from typing import Callable, Iterator, List from unittest.mock import Mock import pytest -from conftest import PyreverseConfig # type: ignore +from conftest import PyreverseConfig # type: ignore #pylint: disable=no-name-in-module from pylint.pyreverse.diadefslib import DefaultDiadefGenerator, DiadefsHandler from pylint.pyreverse.inspector import Linker, Project |