power_fields: allow slicing mappings
authorDmitry Selyutin <ghostmansd@gmail.com>
Fri, 2 Sep 2022 21:34:29 +0000 (00:34 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Sat, 3 Sep 2022 18:41:28 +0000 (21:41 +0300)
src/openpower/decoder/power_fields.py

index e7660d83acdea4990d20a6c20f8ad76b26dfbddf..48cd23d45dd2f8f02fd265462a76ac1f31ff8278 100644 (file)
@@ -330,6 +330,19 @@ class MappingMeta(type):
             length = max(length, len(field))
         return length
 
+    def __getitem__(cls, selector):
+        best_min = 0
+        best_max = 0
+        for field in cls.__members__.values():
+            length = len(field)
+            best_min = min(best_min, length)
+            best_max = max(best_max, length)
+
+        items = tuple(range(best_min, best_max))
+        field = FieldMeta(cls.__name__, (Field,), {}, items=items)
+
+        return field[selector]
+
     def remap(cls, scheme):
         ns = {}
         annotations = {}