From f2ec278528f966a83554d3d23a00cd9a8613144f Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Sat, 10 Jun 2023 22:32:52 +0300 Subject: [PATCH] insndb/core: introduce Dict class --- src/openpower/insndb/core.py | 18 ++++++++++++++++++ src/openpower/insndb/db.py | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/openpower/insndb/core.py b/src/openpower/insndb/core.py index 23e68e39..7ec4788f 100644 --- a/src/openpower/insndb/core.py +++ b/src/openpower/insndb/core.py @@ -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 diff --git a/src/openpower/insndb/db.py b/src/openpower/insndb/db.py index 291e1a61..c7788f8a 100644 --- a/src/openpower/insndb/db.py +++ b/src/openpower/insndb/db.py @@ -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 -- 2.30.2