Coverage for C:\Repos\ekr-pylint\pylint\reporters\ureports\text_writer.py: 27%
64 statements
« prev ^ index » next coverage.py v6.4, created at 2022-05-24 10:21 -0500
« prev ^ index » next coverage.py v6.4, created at 2022-05-24 10:21 -0500
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# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
5"""Text formatting drivers for ureports."""
7from __future__ import annotations
9from typing import TYPE_CHECKING
11from pylint.reporters.ureports.base_writer import BaseWriter
13if TYPE_CHECKING:
14 from pylint.reporters.ureports.nodes import (
15 EvaluationSection,
16 Paragraph,
17 Section,
18 Table,
19 Text,
20 Title,
21 VerbatimText,
22 )
24TITLE_UNDERLINES = ["", "=", "-", "`", ".", "~", "^"]
25BULLETS = ["*", "-"]
28class TextWriter(BaseWriter):
29 """Format layouts as text
30 (ReStructured inspiration but not totally handled yet).
31 """
33 def __init__(self) -> None:
34 super().__init__()
35 self.list_level = 0
37 def visit_section(self, layout: Section) -> None:
38 """Display a section as text."""
39 self.section += 1
40 self.writeln()
41 self.format_children(layout)
42 self.section -= 1
43 self.writeln()
45 def visit_evaluationsection(self, layout: EvaluationSection) -> None:
46 """Display an evaluation section as a text."""
47 self.section += 1
48 self.format_children(layout)
49 self.section -= 1
50 self.writeln()
52 def visit_title(self, layout: Title) -> None:
53 title = "".join(list(self.compute_content(layout)))
54 self.writeln(title)
55 try:
56 self.writeln(TITLE_UNDERLINES[self.section] * len(title))
57 except IndexError:
58 print("FIXME TITLE TOO DEEP. TURNING TITLE INTO TEXT")
60 def visit_paragraph(self, layout: Paragraph) -> None:
61 """Enter a paragraph."""
62 self.format_children(layout)
63 self.writeln()
65 def visit_table(self, layout: Table) -> None:
66 """Display a table as text."""
67 table_content = self.get_table_content(layout)
68 # get columns width
69 cols_width = [0] * len(table_content[0])
70 for row in table_content:
71 for index, col in enumerate(row):
72 cols_width[index] = max(cols_width[index], len(col))
73 self.default_table(layout, table_content, cols_width)
74 self.writeln()
76 def default_table(
77 self, layout: Table, table_content: list[list[str]], cols_width: list[int]
78 ) -> None:
79 """Format a table."""
80 cols_width = [size + 1 for size in cols_width]
81 format_strings = " ".join(["%%-%ss"] * len(cols_width))
82 format_strings %= tuple(cols_width)
84 table_linesep = "\n+" + "+".join("-" * w for w in cols_width) + "+\n"
85 headsep = "\n+" + "+".join("=" * w for w in cols_width) + "+\n"
87 self.write(table_linesep)
88 split_strings = format_strings.split(" ")
89 for index, line in enumerate(table_content):
90 self.write("|")
91 for line_index, at_index in enumerate(line):
92 self.write(split_strings[line_index] % at_index)
93 self.write("|")
94 if index == 0 and layout.rheaders:
95 self.write(headsep)
96 else:
97 self.write(table_linesep)
99 def visit_verbatimtext(self, layout: VerbatimText) -> None:
100 """Display a verbatim layout as text (so difficult ;)."""
101 self.writeln("::\n")
102 for line in layout.data.splitlines():
103 self.writeln(" " + line)
104 self.writeln()
106 def visit_text(self, layout: Text) -> None:
107 """Add some text."""
108 self.write(f"{layout.data}")