insndb: provide pysvp64db script
authorDmitry Selyutin <ghostmansd@gmail.com>
Sat, 3 Jun 2023 09:23:20 +0000 (12:23 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Sat, 3 Jun 2023 09:23:20 +0000 (12:23 +0300)
setup.py
src/openpower/insndb/db.py [new file with mode: 0644]
src/openpower/insndb/types.py

index 827d39be604f9cbb5e5700a89b23034d80664c41..a6b32207e9d73bec1238f20fa5f18c0c9222575e 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -83,7 +83,8 @@ setup(
             'sv_analysis=openpower.sv.sv_analysis:main',
             'pypowersim=openpower.decoder.isa.pypowersim:run_simulation',
             'pysvp64asm=openpower.insndb.asm:main',
-            'pysvp64dis=openpower.insndb.dis:main'
-        ]
-    }
+            'pysvp64db=openpower.insndb.db:main',
+            'pysvp64dis=openpower.insndb.dis:main',
+        ],
+    },
 )
diff --git a/src/openpower/insndb/db.py b/src/openpower/insndb/db.py
new file mode 100644 (file)
index 0000000..989bcef
--- /dev/null
@@ -0,0 +1,36 @@
+import argparse
+import contextlib
+import sys
+
+from openpower.decoder.power_enums import (
+    find_wiki_dir,
+)
+from openpower.insndb.types import (
+    Database,
+    Visitor,
+)
+
+
+def main():
+    class ListVisitor(Visitor):
+        @contextlib.contextmanager
+        def db(self, db):
+            yield db
+
+        @contextlib.contextmanager
+        def record(self, record):
+            print(record.name)
+            yield record
+
+    visitors = {
+        "list": ListVisitor,
+    }
+    parser = argparse.ArgumentParser()
+    subparser = parser.add_subparsers(dest="command", required=True)
+    parser_list = subparser.add_parser("list")
+    args = vars(parser.parse_args())
+    command = args.pop("command")
+    visitor = visitors[command]()
+
+    db = Database(find_wiki_dir())
+    db.visit(visitor=visitor)
index ca540386560906d0c6a538f648fa793741b714ce..e135a663ddd0ea0313fc06af56c02934464129d5 100644 (file)
@@ -1,4 +1,5 @@
 import collections as _collections
+import contextlib as _contextlib
 import csv as _csv
 import dataclasses as _dataclasses
 import enum as _enum
@@ -54,6 +55,16 @@ from openpower.decoder.power_fields import (
 from openpower.decoder.pseudo.pagereader import ISA as _ISA
 
 
+class Visitor:
+    @_contextlib.contextmanager
+    def db(self, db):
+        yield db
+
+    @_contextlib.contextmanager
+    def record(self, record):
+        yield record
+
+
 @_functools.total_ordering
 class Style(_enum.Enum):
     LEGACY = _enum.auto()
@@ -811,6 +822,10 @@ class Record:
     mdwn: MarkdownRecord
     svp64: SVP64Record = None
 
+    def visit(self, visitor):
+        with visitor.record(record=self) as record:
+            pass
+
     def __lt__(self, other):
         if not isinstance(other, Record):
             return NotImplemented
@@ -3665,6 +3680,11 @@ class Database:
 
         return super().__init__()
 
+    def visit(self, visitor):
+        with visitor.db(db=self) as db:
+            for record in self.__db:
+                record.visit(visitor=visitor)
+
     def __repr__(self):
         return repr(self.__db)