@@ -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
@@ -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
@@ -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:
@@ -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