-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathconsole_printer.py
More file actions
83 lines (64 loc) · 2.92 KB
/
console_printer.py
File metadata and controls
83 lines (64 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from typing import TYPE_CHECKING, ClassVar, Dict, List, Optional, Type
import typer
from cycode.cli.exceptions.custom_exceptions import CycodeError
from cycode.cli.models import CliError, CliResult
from cycode.cli.printers.json_printer import JsonPrinter
from cycode.cli.printers.rich_printer import RichPrinter
from cycode.cli.printers.tables.sca_table_printer import ScaTablePrinter
from cycode.cli.printers.tables.table_printer import TablePrinter
from cycode.cli.printers.text_printer import TextPrinter
if TYPE_CHECKING:
from cycode.cli.models import LocalScanResult
from cycode.cli.printers.tables.table_printer_base import PrinterBase
class ConsolePrinter:
_AVAILABLE_PRINTERS: ClassVar[Dict[str, Type['PrinterBase']]] = {
'rich': RichPrinter,
'text': TextPrinter,
'json': JsonPrinter,
'table': TablePrinter,
# overrides
'table_sca': ScaTablePrinter,
'text_sca': ScaTablePrinter,
'rich_sca': ScaTablePrinter,
}
def __init__(self, ctx: typer.Context) -> None:
self.ctx = ctx
self.scan_type = self.ctx.obj.get('scan_type')
self.output_type = self.ctx.obj.get('output')
self.aggregation_report_url = self.ctx.obj.get('aggregation_report_url')
self._printer_class = self._AVAILABLE_PRINTERS.get(self.output_type)
if self._printer_class is None:
raise CycodeError(f'"{self.output_type}" output type is not supported.')
def print_scan_results(
self,
local_scan_results: List['LocalScanResult'],
errors: Optional[Dict[str, 'CliError']] = None,
) -> None:
printer = self._get_scan_printer()
printer.print_scan_results(local_scan_results, errors)
def _get_scan_printer(self) -> 'PrinterBase':
printer_class = self._printer_class
composite_printer = self._AVAILABLE_PRINTERS.get(f'{self.output_type}_{self.scan_type}')
if composite_printer:
printer_class = composite_printer
return printer_class(self.ctx)
def print_result(self, result: CliResult) -> None:
self._printer_class(self.ctx).print_result(result)
def print_error(self, error: CliError) -> None:
self._printer_class(self.ctx).print_error(error)
def print_exception(self, e: Optional[BaseException] = None, force_print: bool = False) -> None:
"""Print traceback message in stderr if verbose mode is set."""
if force_print or self.ctx.obj.get('verbose', False):
self._printer_class(self.ctx).print_exception(e)
@property
def is_json_printer(self) -> bool:
return self._printer_class == JsonPrinter
@property
def is_table_printer(self) -> bool:
return self._printer_class == TablePrinter
@property
def is_text_printer(self) -> bool:
return self._printer_class == TextPrinter
@property
def is_rich_printer(self) -> bool:
return self._printer_class == RichPrinter