1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
20 from prettytable
import PrettyTable
22 from jsonpath_ng
import parse
25 def print_output(format
, headers
, rows
):
27 table
= PrettyTable(headers
)
31 elif format
== "json":
34 data
.append(dict(zip(headers
, row
)))
35 json_data
= json
.dumps(data
)
38 table
= PrettyTable(headers
)
41 print(table
.get_csv_string())
42 elif format
== "yaml":
45 data
.append(dict(zip(headers
, row
)))
46 yaml_string
= yaml
.safe_dump(data
, sort_keys
=False)
48 elif format
.startswith("jsonpath="):
50 json_path_expression
= format
.partition("=")[-1]
51 json_path
= parse(json_path_expression
)
54 data
.append(dict(zip(headers
, row
)))
55 # json_data = json.dumps(data)
56 # Apply JSONPath expression on the JSON object
57 results
= [match
.value
for match
in json_path
.find(data
)]
60 print("Valid values for -o are table, yaml, csv, json, jsonpath")
63 def validate_command_output(ctx
, param
, value
):
71 ] # List of allowed commands
73 if ctx
.command
.name
in allowed_commands
:
78 raise click
.BadParameter(
79 f
'Option "{param.name}" is not allowed for this command.'