X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osmclient%2Fcommon%2Fprint_output.py;h=a9364c8e7cab413c0f9bfb37beaab3c5fda274c8;hb=cd7cd5eba8cfbb5f1941744ff90cf45e0ebcf422;hp=e5bc7f7b24c6c813b8a80c9eb0216b79f6d1ba7c;hpb=0e2d832d6e4099b7d2f868f9ee5a782b5fca2a56;p=osm%2Fosmclient.git diff --git a/osmclient/common/print_output.py b/osmclient/common/print_output.py index e5bc7f7..a9364c8 100644 --- a/osmclient/common/print_output.py +++ b/osmclient/common/print_output.py @@ -14,67 +14,79 @@ # See the License for the specific language governing permissions and # limitations under the License. ####################################################################################### +import click +import logging import json import yaml -import click from prettytable import PrettyTable from jsonpath_ng import parse +logger = logging.getLogger("osmclient") + + +def evaluate_output_format(ctx, param, value): + logger.debug("") + if value in ["table", "json", "yaml", "csv"]: + return value + elif value.startswith("jsonpath="): + return value + else: + raise click.BadParameter( + f"{value} is not one of 'table', 'json', 'yaml', 'csv', 'jsonpath'" + ) + + +output_option = click.option( + "-o", + "--output", + default="table", + is_eager=True, + callback=evaluate_output_format, + help="output format (default: table)", +) + + +literal_option = click.option( + "--literal", is_flag=True, help="print literally, no pretty table" +) + + def print_output(format, headers, rows): + logger.debug("") if format == "table": table = PrettyTable(headers) + table.align = "l" for row in rows: table.add_row(row) print(table) - elif format == "json": - data = [] - for row in rows: - data.append(dict(zip(headers, row))) - json_data = json.dumps(data) - print(json_data) elif format == "csv": table = PrettyTable(headers) for row in rows: table.add_row(row) print(table.get_csv_string()) - elif format == "yaml": + elif format == "json" or format == "yaml" or format.startswith("jsonpath="): data = [] for row in rows: - data.append(dict(zip(headers, row))) - yaml_string = yaml.safe_dump(data, sort_keys=False) - print(yaml_string) - elif format.startswith("jsonpath="): - # JSONPath expression - json_path_expression = format.partition("=")[-1] - json_path = parse(json_path_expression) - data = [] - for row in rows: - data.append(dict(zip(headers, row))) - # json_data = json.dumps(data) - # Apply JSONPath expression on the JSON object - results = [match.value for match in json_path.find(data)] - print(results) - else: - print("Valid values for -o are table, yaml, csv, json, jsonpath") - - -def validate_command_output(ctx, param, value): - allowed_commands = [ - "ns-list", - "ns-show", - "vim-list", - "vim-show", - "vnf-list", - "vnf-show", - ] # List of allowed commands - - if ctx.command.name in allowed_commands: - return value - # if param: - # print (value) - else: - raise click.BadParameter( - f'Option "{param.name}" is not allowed for this command.' - ) + item = {} + for i in range(len(row)): + item[headers[i]] = row[i] + data.append(item) + if format == "json": + print(json.dumps(data, indent=4)) + elif format == "yaml": + print( + yaml.safe_dump( + data, indent=4, default_flow_style=False, sort_keys=False + ) + ) + elif format.startswith("jsonpath="): + # JSONPath expression + json_path_expression = format.partition("=")[-1] + logger.debug(f"Jsonpath expression: {json_path_expression}") + json_path = parse(json_path_expression) + # Apply JSONPath expression on the JSON object + results = [match.value for match in json_path.find(data)] + for i in results: + print(i)