From patchwork Wed Aug 15 10:33:47 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= X-Patchwork-Id: 177613 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 0FCCA2C0088 for ; Wed, 15 Aug 2012 20:34:01 +1000 (EST) Received: from localhost ([::1]:58736 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T1avI-0006Oy-2o for incoming@patchwork.ozlabs.org; Wed, 15 Aug 2012 06:34:00 -0400 Received: from eggs.gnu.org ([208.118.235.92]:36345) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T1avB-0006O7-1x for qemu-devel@nongnu.org; Wed, 15 Aug 2012 06:33:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T1av9-0008JO-Rq for qemu-devel@nongnu.org; Wed, 15 Aug 2012 06:33:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34155) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T1av9-0008JG-IX for qemu-devel@nongnu.org; Wed, 15 Aug 2012 06:33:51 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q7FAXoG4026835 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 15 Aug 2012 06:33:50 -0400 Received: from mustard.gsslab.fab.redhat.com (mustard.gsslab.fab.redhat.com [10.33.8.112]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q7FAXnEK012701; Wed, 15 Aug 2012 06:33:49 -0400 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Wed, 15 Aug 2012 11:33:47 +0100 Message-Id: <1345026827-7389-1-git-send-email-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: Luiz Capitulino Subject: [Qemu-devel] [PATCH] Add support for pretty-printing response in qmp-shell X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: "Daniel P. Berrange" Add a '-p' arg to the QMP/qmp-shell test program, which uses the python pprint module to pretty-print the dictionary returned from a command $ qmp-shell -p /tmp/qemu Welcome to the QMP low-level shell! Connected to QEMU 1.1.50 (QEMU) query-cpus { u'return': [ { u'CPU': 0, u'current': True, u'halted': True, u'pc': 1048556, u'thread_id': 7108}]} Signed-off-by: Daniel P. Berrange --- QMP/qmp-shell | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/QMP/qmp-shell b/QMP/qmp-shell index 42dabc8..24b665c 100755 --- a/QMP/qmp-shell +++ b/QMP/qmp-shell @@ -33,6 +33,7 @@ import qmp import readline import sys +import pprint class QMPCompleter(list): def complete(self, text, state): @@ -52,10 +53,11 @@ class QMPShellBadPort(QMPShellError): # TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and # _execute_cmd()). Let's design a better one. class QMPShell(qmp.QEMUMonitorProtocol): - def __init__(self, address): + def __init__(self, address, pp=None): qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address)) self._greeting = None self._completer = None + self._pp = pp def __get_address(self, arg): """ @@ -114,7 +116,11 @@ class QMPShell(qmp.QEMUMonitorProtocol): if resp is None: print 'Disconnected' return False - print resp + + if self._pp is not None: + self._pp.pprint(resp) + else: + print resp return True def connect(self): @@ -222,22 +228,36 @@ def die(msg): def fail_cmdline(option=None): if option: sys.stderr.write('ERROR: bad command-line option \'%s\'\n' % option) - sys.stderr.write('qemu-shell [ -H ] < UNIX socket path> | < TCP address:port >\n') + sys.stderr.write('qemu-shell [ -p ] [ -H ] < UNIX socket path> | < TCP address:port >\n') sys.exit(1) def main(): addr = '' + qemu = None + hmp = False + pp = None + try: - if len(sys.argv) == 2: - qemu = QMPShell(sys.argv[1]) - addr = sys.argv[1] - elif len(sys.argv) == 3: - if sys.argv[1] != '-H': - fail_cmdline(sys.argv[1]) - qemu = HMPShell(sys.argv[2]) - addr = sys.argv[2] - else: - fail_cmdline() + for arg in sys.argv[1:]: + if arg == "-H": + if qemu is not None: + fail_cmdline(arg) + hmp = True + elif arg == "-p": + if pp is not None: + fail_cmdline(arg) + pp = pprint.PrettyPrinter(indent=4) + else: + if qemu is not None: + fail_cmdline(arg) + if hmp: + qemu = HMPShell(arg) + else: + qemu = QMPShell(arg, pp) + addr = arg + + if qemu is None: + fail_cmdline() except QMPShellBadPort: die('bad port number in command-line')