From patchwork Thu Jul 4 14:09:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1956895 X-Patchwork-Delegate: i.maximets@samsung.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=K6IOK2Gm; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WFJSg07mbz1xqq for ; Fri, 5 Jul 2024 00:10:10 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 219AA408C4; Thu, 4 Jul 2024 14:10:08 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id F7VW-Z4v24d8; Thu, 4 Jul 2024 14:10:06 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=140.211.9.56; helo=lists.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 508D640604 Authentication-Results: smtp2.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=K6IOK2Gm Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp2.osuosl.org (Postfix) with ESMTPS id 508D640604; Thu, 4 Jul 2024 14:10:06 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 164ABC0A9F; Thu, 4 Jul 2024 14:10:06 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 9912AC0A97 for ; Thu, 4 Jul 2024 14:10:01 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 59455847B3 for ; Thu, 4 Jul 2024 14:10:00 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id PPFtaoucO3Hh for ; Thu, 4 Jul 2024 14:09:59 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=170.10.129.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=jmeng@redhat.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp1.osuosl.org 69DEE847BF Authentication-Results: smtp1.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 69DEE847BF Authentication-Results: smtp1.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=K6IOK2Gm Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp1.osuosl.org (Postfix) with ESMTPS id 69DEE847BF for ; Thu, 4 Jul 2024 14:09:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720102198; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+aoT/Q/7avfPfiAvEzzW6XlbLG3r7WpykZb9wi/w1fQ=; b=K6IOK2GmAmyEb83A7UJOxuR+iRImvlPfe2NvCCr0wcQSZ7C9u84BueyYTyzsSJ5mq7m6q9 TN1wIq5NXjTzC1CfjelojY/HSaaxVv/xiz+oCk2KR0FVUloD9On6rm3/UpqFIRULKiLYKS bVVgvKEPqgD2cwxI5Vl8HGwZ0AhzfJQ= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-647-OiNJTZ6ZP8CgehzCb4gOKA-1; Thu, 04 Jul 2024 10:09:57 -0400 X-MC-Unique: OiNJTZ6ZP8CgehzCb4gOKA-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4258675a531so4678605e9.3 for ; Thu, 04 Jul 2024 07:09:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720102196; x=1720706996; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+aoT/Q/7avfPfiAvEzzW6XlbLG3r7WpykZb9wi/w1fQ=; b=QbjSchP7lOtsghZtE4XG1nSFfWzJ3/cZ0f8exPcQQ4N5knD9dhl8McERTvYz01JJPi uoM2za2L+picH/Kw1DAzvL0yJ61OV0nnWqSoltIeJHu3kuMTlxOdwlQQTZiSXhkKpRgS PJFgXNlyKnNy8sXc2ViKfx3Ajmia03h2MxCbC8kWSv113Zgdrd0pVVRCe6DrBcUhlLOG gwxYWE7h5owJdpGtZfLpH5CiEpMGDhXPOHjxmAXIqpJ7tNdGCpafEzJd80H2RpAOeOxO v1DTF/d8Az/G5ffGt+N62SyfTqeixUrCqfw/zJDWT1O5+fTynjtn+xLHLGhsHjOqeebh 6x9Q== X-Gm-Message-State: AOJu0YyBs8WKCkPDfuO/FllsVfkyQaCjhlsCeL6T3L5n+9dsMKfD4hZl w7pLUarn28TqAjMPrbdcwqwpc3ojIgMgYx4RQEHlES/J7toqERwXW9IvBA9c/zu+F85W+IeG8b6 nL3scUY+opV0Jg8Gv2wbcb1N7yvQjPFIyX8c9j5R9IjRIcBNJp3kDo5juNrTHuX8kIMP4eumelC 4Nyg6wcTep0+lKuQrdLxnVClDgMUzo X-Received: by 2002:a05:600c:364c:b0:425:7a99:e6f2 with SMTP id 5b1f17b1804b1-4264a3e31bfmr15545615e9.14.1720102196095; Thu, 04 Jul 2024 07:09:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE5OYFHayvkFRz9GPNdM9bK6ACdBqW5OC1ZYOMOnTgnxFC+/HRJY4Cr9A8ZcLwziuQxcTyYww== X-Received: by 2002:a05:600c:364c:b0:425:7a99:e6f2 with SMTP id 5b1f17b1804b1-4264a3e31bfmr15545455e9.14.1720102195778; Thu, 04 Jul 2024 07:09:55 -0700 (PDT) Received: from positronik4lide.redhat.com ([87.122.59.144]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4264a1d50b0sm26565645e9.7.2024.07.04.07.09.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jul 2024 07:09:55 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org Date: Thu, 4 Jul 2024 16:09:44 +0200 Message-Id: <20240704140946.838801-5-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240704140946.838801-1-jmeng@redhat.com> References: <20240704140946.838801-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v13 4/6] python: Add option for pretty-printing JSON output to appctl.py. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Jakob Meng 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 --- 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