power_fields: allow slicing fields
authorDmitry Selyutin <ghostmansd@gmail.com>
Fri, 2 Sep 2022 16:34:11 +0000 (19: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 4b7d03654713d518c74ac131d77de7ccd75d9658..e7660d83acdea4990d20a6c20f8ad76b26dfbddf 100644 (file)
@@ -127,6 +127,19 @@ class FieldMeta(type):
     def __len__(cls):
         return len(cls.__members__)
 
+    def __getitem__(cls, selector):
+        if isinstance(selector, int):
+            selector = (selector,)
+
+        items = []
+        for idx in selector:
+            if not isinstance(idx, int):
+                raise ValueError(selector)
+            item = cls.__members__[idx]
+            items.append(item)
+
+        return cls.__class__(cls.__name__, (Field,), {}, items=items)
+
     def remap(cls, scheme):
         if isinstance(scheme, type) and issubclass(scheme, Mapping):
             scheme = range(len(scheme))