From patchwork Thu May 16 15:41:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1936054 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=EiJv4bCw; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (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 4VgDpx4R8fz1ymw for ; Fri, 17 May 2024 01:41:45 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 233AE83A4A; Thu, 16 May 2024 15:41:43 +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 3KFXg__GxJu0; Thu, 16 May 2024 15:41:40 +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 smtp1.osuosl.org F3DFD838DE Authentication-Results: smtp1.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=EiJv4bCw Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id F3DFD838DE; Thu, 16 May 2024 15:41:36 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 2BEE0C0DD8; Thu, 16 May 2024 15:41:36 +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 07014C007C for ; Thu, 16 May 2024 15:41:31 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id E0553836F6 for ; Thu, 16 May 2024 15:41:30 +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 PbnVIDNDqFwT for ; Thu, 16 May 2024 15:41:30 +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 D02BC818D4 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 D02BC818D4 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 D02BC818D4 for ; Thu, 16 May 2024 15:41:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715874088; 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=aj7h841kSax5+zyjtfQL96Z7hWMdn3+0BPP8WXM04Ms=; b=EiJv4bCw+lWt6FJqppn4oWLyBuV6+vmvHokLqddPVG6PeDrfbwUocpGjSwfp8s8mxTNBdD H3id4q/06yo3oZMwWcTW1yMJ34RiyNwqr5b5GN7TS2lJEbRAwp/abMWZvtrqyvXxbpsThC tihIOji1Qrnxq+pJJyC0yesNQHmX5K4= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-153-D7EqAsbdNlmYU0ZKz8v7Qg-1; Thu, 16 May 2024 11:41:27 -0400 X-MC-Unique: D7EqAsbdNlmYU0ZKz8v7Qg-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4200ebdbf55so28770705e9.1 for ; Thu, 16 May 2024 08:41:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715874086; x=1716478886; 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=aj7h841kSax5+zyjtfQL96Z7hWMdn3+0BPP8WXM04Ms=; b=cNcLC3MSnBbnV/RSADSUDZ/aEK/fWMM/U3yWHSoz2gF18Z/TM+gGx13R7PkZL+4Nvb IHnXn79HuL5unKdgIoym6UUNdGNi2gwA8fuHpegju7EJkUabkNufXdOUvrJIoh7zgWkV 1BAP2PXuHGa4ap30f5dKI2dhaBx69Gw2H4ndfp/7NIt0lZrPkm+dgGXajXfNoqkTmFaR ExEpPrZdJM2j+HmxRDw2oqwpHQvINAzkiXRQ03B1bNhKbsWW9vg0iT9rUK8uhOvU0vxe Az6xp6FebQcvXG5gbGgtIg3uQZRawYVbUgQAv3weC9/JhUjiGgv1dCnLs84pqrBNqn+T Ydkw== X-Gm-Message-State: AOJu0YwgQqlMaWrqmQZaO6L4xSgQ7IZmO6wTXXY4R3cYrfLvyubFnTmi 8bvVwuRR1gaAmoo+sxdATD6Q0dMKTImtheNqkli5xHaTAt8mqERBA9zYubN9xebpBQyrYh2YjN1 cV/N2MKHm6z5If5yYKdl7hdyHDJkMxMFho72NfuUMWfXQxzbMmmDO7A0txWRCv1jv/OwCqTy6QH 4P2DK93fTT1gT0i1ZeB0oy6J3m4tok X-Received: by 2002:a5d:5b86:0:b0:34d:85e4:7639 with SMTP id ffacd0b85a97d-3504a737d26mr10466036f8f.35.1715874085956; Thu, 16 May 2024 08:41:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHAK3dfWTq7w7/b59DbfgILBsj2Ns29KN2vIdmpsJPRHRk1N81zuRHU3iHsdXDb+PjW8IeapQ== X-Received: by 2002:a5d:5b86:0:b0:34d:85e4:7639 with SMTP id ffacd0b85a97d-3504a737d26mr10466027f8f.35.1715874085637; Thu, 16 May 2024 08:41:25 -0700 (PDT) Received: from positronik4lide.redhat.com ([87.122.57.114]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3502bbbbf9esm19177266f8f.91.2024.05.16.08.41.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 08:41:25 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org Date: Thu, 16 May 2024 17:41:15 +0200 Message-Id: <20240516154117.2306789-5-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240516154117.2306789-1-jmeng@redhat.com> References: <20240516154117.2306789-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v12 4/6] python: Add option for pretty-printing JSON output to appctl.py. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 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 | 13 ++++++++++--- tests/unixctl-py.at | 5 +++++ 2 files changed, 15 insertions(+), 3 deletions(-) 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