From: Jakob Meng <[email protected]> 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 <[email protected]> --- tests/appctl.py | 15 ++++++++++++--- tests/unixctl-py.at | 6 ++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/appctl.py b/tests/appctl.py index ce97a5dcf..230208a19 100644 --- a/tests/appctl.py +++ b/tests/appctl.py @@ -37,11 +37,12 @@ def connect_to_target(target): return client -def reply_to_string(reply, fmt=ovs.unixctl.UnixctlOutputFormat.TEXT): +def reply_to_string(reply, fmt=ovs.unixctl.UnixctlOutputFormat.TEXT, + fmt_flags={}): if fmt == ovs.unixctl.UnixctlOutputFormat.TEXT: body = str(reply) else: - body = ovs.json.to_string(reply) + body = ovs.json.to_string(reply, **fmt_flags) if body and not body.endswith("\n"): body += "\n" @@ -65,13 +66,21 @@ def main(): help="Output format.", default="text", choices=[fmt.name.lower() for fmt in ovs.unixctl.UnixctlOutputFormat]) + parser.add_argument("--pretty", action="store_true", + help="Format the output in a more readable fashion." + " Requires: --format json.") args = parser.parse_args() + if (args.format != ovs.unixctl.UnixctlOutputFormat.JSON.name.lower() + 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.unixctl.UnixctlOutputFormat[args.format.upper()] + format_flags = dict(pretty=True) if args.pretty else {} client = connect_to_target(target) if format != ovs.unixctl.UnixctlOutputFormat.TEXT: @@ -96,7 +105,7 @@ def main(): sys.exit(2) else: assert result is not None - sys.stdout.write(reply_to_string(result, format)) + sys.stdout.write(reply_to_string(result, format, format_flags)) if __name__ == '__main__': diff --git a/tests/unixctl-py.at b/tests/unixctl-py.at index 4d59c5818..0106af825 100644 --- a/tests/unixctl-py.at +++ b/tests/unixctl-py.at @@ -116,6 +116,12 @@ 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)","reply-format":"plain"} ]) +AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format json --pretty version], [0], [dnl +{ + "reply":"$(cat expout)", + "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 [email protected] https://mail.openvswitch.org/mailman/listinfo/ovs-dev
