11import json
22import sys
33import textwrap
4+ from datetime import datetime
45from typing import (
56 Dict ,
67 Generator ,
1718from probely .cli .enums import OutputEnum
1819from probely .cli .tables .base_table import BaseOutputTable
1920from probely .sdk .enums import ProbelyCLIEnum
21+ from probely .sdk ._schemas import Finding , FindingLabel
2022
2123UNKNOWN_VALUE_REP = "UNKNOWN"
2224TARGET_NEVER_SCANNED_OUTPUT = "Never_scanned"
@@ -29,7 +31,7 @@ class OutputRenderer:
2931
3032 def __init__ (
3133 self ,
32- records : Generator [dict , None , None ],
34+ records : Generator [Union [ dict , Finding ] , None , None ],
3335 output_type : Optional [OutputEnum ],
3436 console : Console ,
3537 table_cls : Type [BaseOutputTable ],
@@ -53,12 +55,19 @@ def _render_json(self) -> None:
5355 for record in self .records :
5456 if not first :
5557 self .console .print ("," )
56- self .console .print (json .dumps (record , indent = 2 ))
58+
59+ if hasattr (record , "to_json" ):
60+ # NOTE: just temporary solution while we finish SDK refactor and start using OOP approach everywhere
61+ self .console .print (record .to_json (indent = 2 ))
62+ else :
63+ self .console .print (json .dumps (record , indent = 2 ))
5764 first = False
5865 self .console .print ("]" )
5966
6067 def _render_yaml (self ) -> None :
6168 for record in self .records :
69+ if hasattr (record , "to_dict" ):
70+ record = record .to_dict (mode = "json" )
6271 self .console .print (yaml .dump ([record ], indent = 2 , width = sys .maxsize ))
6372
6473 def _render_table (self ) -> None :
@@ -79,16 +88,18 @@ def get_printable_enum_value(enum: Type[ProbelyCLIEnum], api_enum_value: str) ->
7988 return UNKNOWN_VALUE_REP # TODO: scenario that risk enum updated but CLI is forgotten
8089
8190
82- def get_printable_labels (labels : List [Dict ] = None ) -> str :
91+ def get_printable_labels (labels : List [Union [ Dict , FindingLabel ] ] = None ) -> str :
8392 if labels is None :
8493 return "UNKNOWN_LABELS"
8594
8695 labels_names = []
8796 try :
8897 for label in labels :
89- truncated_label = textwrap .shorten (
90- label ["name" ], width = 16 , placeholder = "..."
91- )
98+ if isinstance (label , FindingLabel ):
99+ label_name = label .name
100+ else :
101+ label_name = label ["name" ]
102+ truncated_label = textwrap .shorten (label_name , width = 16 , placeholder = "..." )
92103 labels_names .append (truncated_label )
93104 except :
94105 return "UNKNOWN_LABELS"
@@ -99,12 +110,18 @@ def get_printable_labels(labels: List[Dict] = None) -> str:
99110
100111
101112def get_printable_date (
102- date_string : Union [str , None ],
113+ date_input : Union [str , datetime , None ],
103114 default_string : Union [str , None ] = None ,
104115) -> str :
105- if date_string :
106- datetime = parser .isoparse (date_string )
107- return datetime .strftime ("%Y-%m-%d %H:%M" )
116+ if isinstance (date_input , str ):
117+ date_obj = parser .isoparse (date_input )
118+ elif isinstance (date_input , datetime ):
119+ date_obj = date_input
120+ else :
121+ date_obj = None
122+
123+ if date_obj :
124+ return date_obj .strftime ("%Y-%m-%d %H:%M" )
108125
109126 if default_string :
110127 return default_string
0 commit comments