diff mbox series

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

Message ID 20240516072015.18455-5-jmeng@redhat.com
State Superseded, archived
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 fail github build: failed
ovsrobot/intel-ovs-compilation success test: success

Commit Message

Jakob Meng May 16, 2024, 7:20 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     | 13 ++++++++++---
 tests/unixctl-py.at |  5 +++++
 2 files changed, 15 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/tests/appctl.py b/tests/appctl.py
index cf3ea3642..b08bf9033 100644
--- a/tests/appctl.py
+++ b/tests/appctl.py
@@ -37,7 +37,7 @@  def connect_to_target(target):
     return client
 
 
-def reply_to_string(reply, fmt=ovs.util.OutputFormat.TEXT):
+def reply_to_string(reply, fmt=ovs.util.OutputFormat.TEXT, fmt_flags={}):
     if fmt == ovs.util.OutputFormat.TEXT:
         body = str(reply)
 
@@ -46,7 +46,7 @@  def reply_to_string(reply, fmt=ovs.util.OutputFormat.TEXT):
 
         return body
     else:
-        return ovs.json.to_string(reply)
+        return ovs.json.to_string(reply, **fmt_flags)
 
 
 def main():
@@ -65,13 +65,20 @@  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="Format the output in a more readable fashion."
+                             " Requires: --format json.")
     args = parser.parse_args()
 
+    if args.format != ovs.util.OutputFormat.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.util.OutputFormat[args.format.upper()]
+    format_flags = dict(pretty=True) if args.pretty else {}
     client = connect_to_target(target)
 
     if format != ovs.util.OutputFormat.TEXT:
@@ -96,7 +103,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 92f557b67..dffe40d2b 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)","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