From: Jakob Meng <c...@jakobmeng.de> With the '--pretty' option, appctl.py will now print JSON output in a more readable fashion, i.e. with additional line breaks, spaces and sorted dictionary keys. The pretty-printed output from appctl.py is not strictly the same as with ovs-appctl because of both use different pretty-printing implementations.
Signed-off-by: Jakob Meng <c...@jakobmeng.de> --- NEWS | 3 +++ python/ovs/unixctl/client.py | 4 ++-- tests/appctl.py | 16 ++++++++++++++-- tests/unixctl-py.at | 5 +++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 52cadbe0d..957351acb 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,9 @@ Post-v3.3.0 with indentation. - Python: * Added support for choosing the output format, e.g. 'json' or 'text'. + * Added new option [--pretty] to print JSON output in a readable fashion. + E.g. members of objects and elements of arrays are printed one per line, + with indentation. v3.3.0 - 16 Feb 2024 diff --git a/python/ovs/unixctl/client.py b/python/ovs/unixctl/client.py index 000d261c0..460c79e88 100644 --- a/python/ovs/unixctl/client.py +++ b/python/ovs/unixctl/client.py @@ -27,7 +27,7 @@ class UnixctlClient(object): assert isinstance(conn, ovs.jsonrpc.Connection) self._conn = conn - def transact(self, command, argv, fmt): + def transact(self, command, argv, fmt, fmt_flags): assert isinstance(command, str) assert isinstance(argv, list) for arg in argv: @@ -46,7 +46,7 @@ class UnixctlClient(object): if fmt == ovs.util.OutputFormat.TEXT: return str(body) else: - return ovs.json.to_string(body) + return ovs.json.to_string(body, **fmt_flags) if reply.error is not None: return 0, to_string(reply.error), None diff --git a/tests/appctl.py b/tests/appctl.py index 7ccf34cc3..bd9953e75 100644 --- a/tests/appctl.py +++ b/tests/appctl.py @@ -53,18 +53,29 @@ def main(): help="Output format.", default="text", choices=[fmt.name.lower() for fmt in ovs.util.OutputFormat]) + parser.add_argument("--pretty", action="store_true", + help="By default, JSON in output is printed as" + " compactly as possible. This option causes JSON" + " in output to be printed in a more readable" + " fashion. Members of objects and elements of" + " arrays are printed one per line, with" + " indentation.") args = parser.parse_args() + if args.format != ovs.util.OutputFormat.JSON and args.pretty: + ovs.util.ovs_fatal(0, "--pretty is supported with --format json only") + signal_alarm(int(args.timeout) if args.timeout else None) ovs.vlog.Vlog.init() target = args.target format = ovs.util.OutputFormat[args.format.upper()] + format_flags = dict(pretty=True, sort_keys=True) if args.pretty else {} client = connect_to_target(target) if format != ovs.util.OutputFormat.TEXT: err_no, error, _ = client.transact( - "set-options", ["--format", args.format], format) + "set-options", ["--format", args.format], format, format_flags) if err_no: ovs.util.ovs_fatal(err_no, "%s: transaction error" % target) @@ -73,7 +84,8 @@ def main(): ovs.util.ovs_error(0, "%s: server returned an error" % target) sys.exit(2) - err_no, error, result = client.transact(args.command, args.argv, format) + err_no, error, result = client.transact( + args.command, args.argv, format, format_flags) client.close() if err_no: diff --git a/tests/unixctl-py.at b/tests/unixctl-py.at index 1440b30b7..bcea19abc 100644 --- a/tests/unixctl-py.at +++ b/tests/unixctl-py.at @@ -115,6 +115,11 @@ AT_CHECK([APPCTL -t test-unixctl.py version], [0], [expout]) AT_CHECK([PYAPPCTL_PY -t test-unixctl.py version], [0], [expout]) AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format json version], [0], [dnl {"reply":"$(cat expout)\n","reply-format":"plain"}]) +AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format json --pretty version], [0], [dnl +{ + "reply":"$(cat expout)\n", + "reply-format":"plain" +}]) AT_CHECK([APPCTL -t test-unixctl.py echo robot ninja], [0], [stdout]) AT_CHECK([cat stdout | sed -e "s/u'/'/g"], [0], [dnl -- 2.39.2 _______________________________________________ dev mailing list d...@openvswitch.org https://mail.openvswitch.org/mailman/listinfo/ovs-dev