From 78231140441402bec1dedbe4e97238cb9597b96f Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Sat, 3 Jun 2023 12:23:20 +0300 Subject: [PATCH] insndb: provide pysvp64db script --- setup.py | 7 ++++--- src/openpower/insndb/db.py | 36 +++++++++++++++++++++++++++++++++++ src/openpower/insndb/types.py | 20 +++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/openpower/insndb/db.py diff --git a/setup.py b/setup.py index 827d39be..a6b32207 100644 --- 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 index 00000000..989bcef7 --- /dev/null +++ b/src/openpower/insndb/db.py @@ -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) diff --git a/src/openpower/insndb/types.py b/src/openpower/insndb/types.py index ca540386..e135a663 100644 --- a/src/openpower/insndb/types.py +++ b/src/openpower/insndb/types.py @@ -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) -- 2.30.2