diff mbox series

[ovs-dev,v14,4/6] python: Add option for pretty-printing JSON output to appctl.py.

Message ID 20240709071422.11492-5-jmeng@redhat.com
State Accepted
Commit 3c572af65e54ef97aeb339c91497ba213cd9c515
Delegated to: Ilya Maximets
Headers show
Series Add global option to output JSON from ovs-appctl cmds. | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/intel-ovs-compilation fail test: fail

Commit Message

Jakob Meng July 9, 2024, 7:14 a.m. UTC
From: Jakob Meng <code@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 <code@jakobmeng.de>
---
 tests/appctl.py     | 15 ++++++++++++---
 tests/unixctl-py.at |  6 ++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/tests/appctl.py b/tests/appctl.py
index 4aca7efbc..5f4b2754a 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"
@@ -66,13 +67,21 @@  def main():
                         choices=[fmt.name.lower()
                                  for fmt in ovs.unixctl.UnixctlOutputFormat],
                         type=str.lower)
+    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:
@@ -97,7 +106,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 f4a664dc0..ae8bd5ad1 100644
--- a/tests/unixctl-py.at
+++ b/tests/unixctl-py.at
@@ -119,6 +119,12 @@  AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format json version], [0], [
 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