aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorVladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>2020-06-06 11:18:02 +0300
committerEric Blake <eblake@redhat.com>2020-06-09 15:47:09 -0500
commit860543f055654017cdb5b25ed06c2bc91b72af70 (patch)
tree4ed118467878b53ff8f78259799ab429df861194 /tests
parent0903e3b3714df385931e4ff844e4aff713aa2411 (diff)
downloadqemu-860543f055654017cdb5b25ed06c2bc91b72af70.zip
qemu-860543f055654017cdb5b25ed06c2bc91b72af70.tar.gz
qemu-860543f055654017cdb5b25ed06c2bc91b72af70.tar.bz2
qcow2_format.py: add field-formatting class
Allow formatter class in structure definition instead of hacking with 'mask'. This will simplify further introduction of new formatters. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Reviewed-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com> Message-Id: <20200606081806.23897-10-vsementsov@virtuozzo.com> Signed-off-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/qemu-iotests/qcow2_format.py35
1 files changed, 25 insertions, 10 deletions
diff --git a/tests/qemu-iotests/qcow2_format.py b/tests/qemu-iotests/qcow2_format.py
index 898d388..74a82f9 100644
--- a/tests/qemu-iotests/qcow2_format.py
+++ b/tests/qemu-iotests/qcow2_format.py
@@ -20,6 +20,25 @@ import struct
import string
+class Qcow2Field:
+
+ def __init__(self, value):
+ self.value = value
+
+ def __str__(self):
+ return str(self.value)
+
+
+class Flags64(Qcow2Field):
+
+ def __str__(self):
+ bits = []
+ for bit in range(64):
+ if self.value & (1 << bit):
+ bits.append(bit)
+ return str(bits)
+
+
class Qcow2StructMeta(type):
# Mapping from c types to python struct format
@@ -70,14 +89,10 @@ class Qcow2Struct(metaclass=Qcow2StructMeta):
def dump(self):
for f in self.fields:
value = self.__dict__[f[2]]
- if f[1] == 'mask':
- bits = []
- for bit in range(64):
- if value & (1 << bit):
- bits.append(bit)
- value_str = str(bits)
- else:
+ if isinstance(f[1], str):
value_str = f[1].format(value)
+ else:
+ value_str = str(f[1](value))
print('{:<25} {}'.format(f[2], value_str))
@@ -117,9 +132,9 @@ class QcowHeader(Qcow2Struct):
('u64', '{:#x}', 'snapshot_offset'),
# Version 3 header fields
- ('u64', 'mask', 'incompatible_features'),
- ('u64', 'mask', 'compatible_features'),
- ('u64', 'mask', 'autoclear_features'),
+ ('u64', Flags64, 'incompatible_features'),
+ ('u64', Flags64, 'compatible_features'),
+ ('u64', Flags64, 'autoclear_features'),
('u32', '{}', 'refcount_order'),
('u32', '{}', 'header_length'),
)