selectable_int: introduce SelectableIntMapping class
authorDmitry Selyutin <dmitry.selyutin@3mdeb.com>
Tue, 19 Apr 2022 14:39:05 +0000 (14:39 +0000)
committerDmitry Selyutin <dmitry.selyutin@3mdeb.com>
Tue, 19 Apr 2022 17:02:21 +0000 (17:02 +0000)
src/openpower/decoder/selectable_int.py

index 81e5a9f4eecce7b0ee14eb03c3fe6155a073c9f2..810d24cc53e789b7db7bca8228accc79e232df15 100644 (file)
@@ -438,6 +438,28 @@ class SelectableInt:
         return struct.unpack('<d', data)[0]
 
 
+class SelectableIntMapping:
+    def __init__(self, si, fields=None):
+        def field(item):
+            (key, value) = item
+            if isinstance(value, dict):
+                value = dict(map(field, value.items()))
+            else:
+                value = FieldSelectableInt(self.__si, value)
+            return (key, value)
+
+        self.__si = si
+        self.__fields = dict(map(field, fields.items()))
+
+    def __getattr__(self, attr):
+        if attr == f"_SelectableIntMapping__fields":
+            return self.__dict__["_SelectableIntMapping__fields"]
+        try:
+            return self.__fields[attr]
+        except KeyError as error:
+            raise AttributeError from error
+
+
 def onebit(bit):
     return SelectableInt(1 if bit else 0, 1)