50
50
}
51
51
52
52
53
- class Results (AbstractPresenter , ScanossBase ):
53
+ class ResultsPresenter (AbstractPresenter ):
54
+ """
55
+ SCANOSS Results presenter class
56
+ Handles the presentation of the scan results
57
+ """
58
+
59
+ def __init__ (self , results_instance , ** kwargs ):
60
+ super ().__init__ (** kwargs )
61
+ self .results = results_instance
62
+
63
+ def _format_json_output (self ) -> str :
64
+ """
65
+ Format the output data into a JSON object
66
+ """
67
+
68
+ formatted_data = []
69
+ for item in self .results .data :
70
+ formatted_data .append (
71
+ {
72
+ 'file' : item .get ('filename' ),
73
+ 'status' : item .get ('status' , 'N/A' ),
74
+ 'match_type' : item ['id' ],
75
+ 'matched' : item .get ('matched' , 'N/A' ),
76
+ 'purl' : (item .get ('purl' )[0 ] if item .get ('purl' ) else 'N/A' ),
77
+ 'license' : (item .get ('licenses' )[0 ].get ('name' , 'N/A' ) if item .get ('licenses' ) else 'N/A' ),
78
+ }
79
+ )
80
+ try :
81
+ return json .dumps ({'results' : formatted_data , 'total' : len (formatted_data )}, indent = 2 )
82
+ except Exception as e :
83
+ self .base .print_stderr (f'ERROR: Problem formatting JSON output: { e } ' )
84
+ return ''
85
+
86
+ def _format_plain_output (self ) -> str :
87
+ """Format the output data into a plain text string
88
+
89
+ Returns:
90
+ str: The formatted output data
91
+ """
92
+ if not self .results .data :
93
+ msg = 'No results to present'
94
+ return msg
95
+
96
+ formatted = ''
97
+ for item in self .results .data :
98
+ formatted += f'{ self ._format_plain_output_item (item )} \n '
99
+ return formatted
100
+
101
+ @staticmethod
102
+ def _format_plain_output_item (item ):
103
+ purls = item .get ('purl' , [])
104
+ licenses = item .get ('licenses' , [])
105
+
106
+ return (
107
+ f'File: { item .get ("filename" )} \n '
108
+ f'Match type: { item .get ("id" )} \n '
109
+ f'Status: { item .get ("status" , "N/A" )} \n '
110
+ f'Matched: { item .get ("matched" , "N/A" )} \n '
111
+ f'Purl: { purls [0 ] if purls else "N/A" } \n '
112
+ f'License: { licenses [0 ].get ("name" , "N/A" ) if licenses else "N/A" } \n '
113
+ )
114
+
115
+
116
+ class Results :
54
117
"""
55
118
SCANOSS Results class \n
56
119
Handles the parsing and filtering of the scan results
@@ -80,10 +143,17 @@ def __init__( # noqa: PLR0913
80
143
output_format (str, optional): Output format. Defaults to None.
81
144
"""
82
145
83
- AbstractPresenter .__init__ (self , output_file = output_file , output_format = output_format )
84
- ScanossBase .__init__ (self , debug , trace , quiet )
146
+ self .base = ScanossBase (debug , trace , quiet )
85
147
self .data = self ._load_and_transform (filepath )
86
148
self .filters = self ._load_filters (match_type = match_type , status = status )
149
+ self .presenter = ResultsPresenter (
150
+ self ,
151
+ debug = debug ,
152
+ trace = trace ,
153
+ quiet = quiet ,
154
+ output_file = output_file ,
155
+ output_format = output_format ,
156
+ )
87
157
88
158
def load_file (self , file : str ) -> Dict [str , Any ]:
89
159
"""Load the JSON file
@@ -98,7 +168,7 @@ def load_file(self, file: str) -> Dict[str, Any]:
98
168
try :
99
169
return json .load (jsonfile )
100
170
except Exception as e :
101
- self .print_stderr (f'ERROR: Problem parsing input JSON: { e } ' )
171
+ self .base . print_stderr (f'ERROR: Problem parsing input JSON: { e } ' )
102
172
103
173
def _load_and_transform (self , file : str ) -> List [Dict [str , Any ]]:
104
174
"""
@@ -173,8 +243,8 @@ def _item_matches_filters(self, item):
173
243
def _validate_filter_values (filter_key : str , filter_value : List [str ]):
174
244
if any (value not in AVAILABLE_FILTER_VALUES .get (filter_key , []) for value in filter_value ):
175
245
valid_values = ', ' .join (AVAILABLE_FILTER_VALUES .get (filter_key , []))
176
- raise Exception (
177
- f"ERROR: Invalid filter value '{ filter_value } ' for filter '{ filter_key . value } '. "
246
+ raise ValueError (
247
+ f"ERROR: Invalid filter value '{ filter_value } ' for filter '{ filter_key } '. "
178
248
f'Valid values are: { valid_values } '
179
249
)
180
250
@@ -188,51 +258,6 @@ def get_pending_identifications(self):
188
258
def has_results (self ):
189
259
return bool (self .data )
190
260
191
- def _format_json_output (self ) -> str :
192
- """
193
- Format the output data into a JSON object
194
- """
195
-
196
- formatted_data = []
197
- for item in self .data :
198
- formatted_data .append (
199
- {
200
- 'file' : item .get ('filename' ),
201
- 'status' : item .get ('status' , 'N/A' ),
202
- 'match_type' : item ['id' ],
203
- 'matched' : item .get ('matched' , 'N/A' ),
204
- 'purl' : (item .get ('purl' )[0 ] if item .get ('purl' ) else 'N/A' ),
205
- 'license' : (item .get ('licenses' )[0 ].get ('name' , 'N/A' ) if item .get ('licenses' ) else 'N/A' ),
206
- }
207
- )
208
- return json .dumps ({'results' : formatted_data , 'total' : len (formatted_data )}, indent = 2 )
209
-
210
- def _format_plain_output (self ) -> str :
211
- """Format the output data into a plain text string
212
-
213
- Returns:
214
- str: The formatted output data
215
- """
216
- if not self .data :
217
- msg = 'No results to present'
218
- self .print_stderr (msg )
219
- return msg
220
-
221
- formatted = ''
222
- for item in self .data :
223
- formatted += f'{ self ._format_plain_output_item (item )} \n '
224
- return formatted
225
-
226
- @staticmethod
227
- def _format_plain_output_item (item ):
228
- purls = item .get ('purl' , [])
229
- licenses = item .get ('licenses' , [])
230
-
231
- return (
232
- f'File: { item .get ("filename" )} \n '
233
- f'Match type: { item .get ("id" )} \n '
234
- f'Status: { item .get ("status" , "N/A" )} \n '
235
- f'Matched: { item .get ("matched" , "N/A" )} \n '
236
- f'Purl: { purls [0 ] if purls else "N/A" } \n '
237
- f'License: { licenses [0 ].get ("name" , "N/A" ) if licenses else "N/A" } \n '
238
- )
261
+ def present (self , output_format : str = None , output_file : str = None ):
262
+ """Present the results in the selected format"""
263
+ self .presenter .present (output_format = output_format , output_file = output_file )
0 commit comments