temporary hack-revert, the original is now in branch "paths"
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 11 Jun 2023 12:27:23 +0000 (13:27 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 11 Jun 2023 12:27:23 +0000 (13:27 +0100)
Revert "insndb/core: switch Fields to Dict class"

This reverts commit 963fa4fcadf76740083d9fce3ffe986a81f247fe.

src/openpower/insndb/core.py

index 69e0101b83e2de3fcaa6a4f2018ed9b89f68a8ae..2d0730c769e5a1b17ac125af1ad8f27f1518435b 100644 (file)
@@ -778,7 +778,7 @@ class Fields(Dict, datatype=type("Bits", (Tuple,), {}, datatype=int)):
         yield from self.__mapping.items()
 
 
-class Operands(Dict, datatype=object):
+class Operands:
     __GPR_PAIRS = (
         _Reg.RTp,
         _Reg.RSp,
@@ -836,10 +836,10 @@ class Operands(Dict, datatype=object):
 
             if "=" in operand:
                 (name, value) = operand.split("=")
-                mapping[name] = (StaticOperand, (
-                    ("name", name),
-                    ("value", int(value)),
-                ))
+                mapping[name] = (StaticOperand, {
+                    "name": name,
+                    "value": int(value),
+                })
             else:
                 name = operand
                 if name.endswith(")"):
@@ -873,37 +873,48 @@ class Operands(Dict, datatype=object):
                             cls = CR5Operand
 
                 if imm_name is not None:
-                    mapping[imm_name] = (imm_cls, (("name", imm_name),))
-                mapping[name] = (cls, (("name", name),))
+                    mapping[imm_name] = (imm_cls, {"name": imm_name})
+                mapping[name] = (cls, {"name": name})
+
+        static = []
+        dynamic = []
+        for (name, (cls, kwargs)) in mapping.items():
+            kwargs = dict(kwargs)
+            kwargs["name"] = name
+            if issubclass(cls, StaticOperand):
+                static.append((cls, kwargs))
+            elif issubclass(cls, DynamicOperand):
+                dynamic.append((cls, kwargs))
+            else:
+                raise ValueError(name)
 
-        return super().__init__(mapping)
+        self.__mapping = mapping
+        self.__static = tuple(static)
+        self.__dynamic = tuple(dynamic)
 
-    @walkmethod
-    def walk(clsself, match=None):
-        for (key, (cls, pairs)) in clsself.items():
-            yield ("/".join((key, "class")), cls.__name__)
-            for (subkey, value) in pairs:
-                if subkey == "name":
-                    continue
-                path = "/".join((key, subkey))
-                yield (path, value)
+        return super().__init__()
 
     def __iter__(self):
-        for (key, items) in self.items():
+        for (_, items) in self.__mapping.items():
             (cls, kwargs) = items
-            yield (cls, dict(kwargs))
+            yield (cls, kwargs)
 
-    @staticmethod
-    def filter(cls):
-        return lambda pair: isinstance(pair[0], cls)
+    def __repr__(self):
+        return self.__mapping.__repr__()
 
-    @cached_property
+    def __contains__(self, key):
+        return self.__mapping.__contains__(key)
+
+    def __getitem__(self, key):
+        return self.__mapping.__getitem__(key)
+
+    @property
     def static(self):
-        return filter(self.__class__.filter(StaticOperand), self)
+        return self.__static
 
     @property
     def dynamic(self):
-        return filter(self.__class__.filter(DynamicOperand), self)
+        return self.__dynamic
 
 
 class Arguments(tuple):