insndb/core: introduce Dict class
authorDmitry Selyutin <ghostmansd@gmail.com>
Sat, 10 Jun 2023 19:32:52 +0000 (22:32 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Sat, 10 Jun 2023 19:40:23 +0000 (22:40 +0300)
src/openpower/insndb/core.py
src/openpower/insndb/db.py

index 23e68e393d4c53f9efa4496fbb943c8b3cd3888e..7ec4788f888aea9e25da17237976756d6f62555b 100644 (file)
@@ -119,6 +119,24 @@ class Tuple(Node, tuple):
                     yield (str(index), item)
 
 
+class Dict(Node, dict):
+    def __init_subclass__(cls, datatype):
+        cls.__datatype = datatype
+        return super().__init_subclass__()
+
+    @walkmethod
+    def walk(clsself, match=None):
+        if match is None:
+            match = lambda subnode: True
+
+        if isinstance(clsself, type):
+            yield ("{}", clsself.__datatype)
+        else:
+            for (key, value) in clsself.items():
+                if match(value):
+                    yield (key, value)
+
+
 class VisitorMethod:
     def __init__(self, nodecls, method):
         self.__nodecls = nodecls
index 291e1a61521a61ea43f82106b5cc25c162905702..c7788f8a08c58545c215eadb95a4fa37771b1626 100644 (file)
@@ -9,6 +9,7 @@ from openpower.decoder.power_enums import (
 from openpower.insndb.core import (
     Database,
     Dataclass,
+    Dict,
     Record,
     Records,
     Tuple,
@@ -52,7 +53,7 @@ class TreeVisitor(Visitor):
         with super().__call__(path=path, node=node):
             self.__path.append(path)
             print("/".join(self.__path))
-            if not isinstance(node, (Dataclass, Tuple)):
+            if not isinstance(node, (Dataclass, Tuple, Dict)):
                 print("    ", repr(node), sep="")
             self.__depth += 1
             yield node