From patchwork Sun May 23 10:59:30 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kiszka X-Patchwork-Id: 53313 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B1576B7D47 for ; Sun, 23 May 2010 21:16:20 +1000 (EST) Received: from localhost ([127.0.0.1]:56093 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OG9AH-0002Ka-LJ for incoming@patchwork.ozlabs.org; Sun, 23 May 2010 07:16:17 -0400 Received: from [140.186.70.92] (port=48720 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OG8uW-0004Kd-FT for qemu-devel@nongnu.org; Sun, 23 May 2010 07:00:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OG8uF-0004RU-50 for qemu-devel@nongnu.org; Sun, 23 May 2010 07:00:00 -0400 Received: from fmmailgate02.web.de ([217.72.192.227]:37084) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OG8uD-0004Qu-JW for qemu-devel@nongnu.org; Sun, 23 May 2010 06:59:41 -0400 Received: from smtp01.web.de ( [172.20.0.243]) by fmmailgate02.web.de (Postfix) with ESMTP id E5689162176C0; Sun, 23 May 2010 12:59:40 +0200 (CEST) Received: from [88.65.39.229] (helo=localhost.localdomain) by smtp01.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #4) id 1OG8uB-0003cZ-00; Sun, 23 May 2010 12:59:39 +0200 From: Jan Kiszka To: qemu-devel@nongnu.org, Anthony Liguori Date: Sun, 23 May 2010 12:59:30 +0200 Message-Id: <9c831d494ccab2d068fc711afdc657cc90a96d25.1274612367.git.jan.kiszka@web.de> X-Mailer: git-send-email 1.6.0.2 In-Reply-To: References: In-Reply-To: References: X-Sender: jan.kiszka@web.de X-Provags-ID: V01U2FsdGVkX1/rIQqCwVtjd8j63BJP0CVQVu45fHt1z7v6HEn2 J8hU1rql7qCZmPk5QrP5TUaCS1uREq8VyI76m/XE8QXfAyhXC3 j06bCvL1A= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4-2.6 Cc: Juan Quintela , Jan Kiszka , Markus Armbruster , Luiz Capitulino , Blue Swirl , Avi Kivity Subject: [Qemu-devel] [PATCH v3 17/17] QMP: Add support for buffer class to qmp python helper X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Jan Kiszka This demonstrates the conversion of QMP buffer objects and does some minimalistic pretty-printing. Signed-off-by: Jan Kiszka --- QMP/qmp.py | 25 +++++++++++++++++++++++-- 1 files changed, 23 insertions(+), 2 deletions(-) diff --git a/QMP/qmp.py b/QMP/qmp.py index 4062f84..4650918 100644 --- a/QMP/qmp.py +++ b/QMP/qmp.py @@ -8,7 +8,7 @@ # This work is licensed under the terms of the GNU GPL, version 2. See # the COPYING file in the top-level directory. -import socket, json +import socket, json, binascii class QMPError(Exception): pass @@ -16,6 +16,18 @@ class QMPError(Exception): class QMPConnectError(QMPError): pass +class QMPBuffer: + def __init__(self, data): + self.data = binascii.a2b_base64(data) + + def __repr__(self): + str = '' + for i in range(0, len(self.data)): + if i > 0: + str += ' ' + str += binascii.b2a_hex(self.data[i]) + return str + class QEMUMonitorProtocol: def connect(self): self.sock.connect(self.filename) @@ -61,10 +73,19 @@ class QEMUMonitorProtocol: # the Server won't read our input self.sock.send(json.dumps(cmd) + ' ') + def __json_obj_hook(self, dct): + if '__class__' in dct: + if dct['__class__'] == 'buffer': + return QMPBuffer(dct['data']) + else: + return + return dct + def __json_read(self): try: while True: - line = json.loads(self.sockfile.readline()) + line = json.loads(self.sockfile.readline(), + object_hook=self.__json_obj_hook) if not 'event' in line: return line except ValueError: