From patchwork Thu Jul 30 14:15:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Shinkevich X-Patchwork-Id: 11693175 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9548013B1 for ; Thu, 30 Jul 2020 14:19:00 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 746CB2070B for ; Thu, 30 Jul 2020 14:19:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 746CB2070B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=virtuozzo.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:44824 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k19Od-0003hu-OJ for patchwork-qemu-devel@patchwork.kernel.org; Thu, 30 Jul 2020 10:18:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54156) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k19LA-00052I-Vb; Thu, 30 Jul 2020 10:15:24 -0400 Received: from relay.sw.ru ([185.231.240.75]:44426 helo=relay3.sw.ru) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k19L4-00052Y-On; Thu, 30 Jul 2020 10:15:24 -0400 Received: from [172.16.25.136] (helo=localhost.sw.ru) by relay3.sw.ru with esmtp (Exim 4.93) (envelope-from ) id 1k19Km-0004Cz-V2; Thu, 30 Jul 2020 17:15:01 +0300 From: Andrey Shinkevich To: qemu-block@nongnu.org Subject: [PATCH v12 09/11] qcow2_format.py: collect fields to dump in JSON format Date: Thu, 30 Jul 2020 17:15:10 +0300 Message-Id: <1596118512-424960-10-git-send-email-andrey.shinkevich@virtuozzo.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1596118512-424960-1-git-send-email-andrey.shinkevich@virtuozzo.com> References: <1596118512-424960-1-git-send-email-andrey.shinkevich@virtuozzo.com> Received-SPF: pass client-ip=185.231.240.75; envelope-from=andrey.shinkevich@virtuozzo.com; helo=relay3.sw.ru X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/30 10:15:15 X-ACL-Warn: Detected OS = Linux 3.11 and newer X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, andrey.shinkevich@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" As __dict__ is being extended with class members we do not want to print, add the to_dict() method to classes that returns a dictionary with desired fields and their values. Extend it in subclass when necessary to print the final dictionary in the JSON output which follows. Suggested-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Andrey Shinkevich --- tests/qemu-iotests/qcow2_format.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/qemu-iotests/qcow2_format.py b/tests/qemu-iotests/qcow2_format.py index 2000de3..a4114cb 100644 --- a/tests/qemu-iotests/qcow2_format.py +++ b/tests/qemu-iotests/qcow2_format.py @@ -119,6 +119,9 @@ class Qcow2Struct(metaclass=Qcow2StructMeta): print('{:<25} {}'.format(f[2], value_str)) + def to_dict(self): + return dict((f[2], self.__dict__[f[2]]) for f in self.fields) + class Qcow2BitmapExt(Qcow2Struct): @@ -151,6 +154,11 @@ class Qcow2BitmapExt(Qcow2Struct): print() entry.dump() + def to_dict(self): + fields_dict = super().to_dict() + fields_dict['bitmap_directory'] = self.bitmap_directory + return fields_dict + class Qcow2BitmapDirEntry(Qcow2Struct): @@ -189,6 +197,14 @@ class Qcow2BitmapDirEntry(Qcow2Struct): super(Qcow2BitmapDirEntry, self).dump() self.bitmap_table.dump() + def to_dict(self): + fields_dict = super().to_dict() + fields_dict['bitmap_table'] = self.bitmap_table.entries + bmp_name = dict(name=self.name) + # Put the name ahead of the dict + bme_dict = {**bmp_name, **fields_dict} + return bme_dict + class Qcow2BitmapTableEntry(Qcow2Struct): @@ -214,6 +230,9 @@ class Qcow2BitmapTableEntry(Qcow2Struct): else: self.type = 'all-zeroes' + def to_dict(self): + return dict(type=self.type, offset=self.offset, reserved=self.reserved) + class Qcow2BitmapTable: @@ -246,6 +265,9 @@ class QcowHeaderExtension(Qcow2Struct): 0x44415441: 'Data file' } + def to_dict(self): + return self.mapping.get(self.value, "") + fields = ( ('u32', Magic, 'magic'), ('u32', '{}', 'length') @@ -308,6 +330,18 @@ class QcowHeaderExtension(Qcow2Struct): else: self.obj.dump() + def to_dict(self): + fields_dict = super().to_dict() + ext_name = dict(name=self.Magic(self.magic)) + # Put the name ahead of the dict + he_dict = {**ext_name, **fields_dict} + if self.obj is not None: + he_dict['data'] = self.obj + else: + he_dict['data_str'] = self.data_str + + return he_dict + @classmethod def create(cls, magic, data): return QcowHeaderExtension(magic, len(data), data)