From a6cb276468a499cbbfa65572b257d3f060c89679 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 23 Jan 2023 22:48:31 -0800 Subject: [PATCH] add AbstractView --- masque/__init__.py | 2 +- masque/library.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/masque/__init__.py b/masque/__init__.py index 5a715c9..3f74975 100644 --- a/masque/__init__.py +++ b/masque/__init__.py @@ -33,7 +33,7 @@ from .label import Label from .ref import Ref from .pattern import Pattern from .utils import layer_t, annotations_t -from .library import Library, MutableLibrary, WrapROLibrary, WrapLibrary, LazyLibrary +from .library import Library, MutableLibrary, WrapROLibrary, WrapLibrary, LazyLibrary, AbstractView from .ports import Port, PortList from .builder import Builder, Abstract, Tool diff --git a/masque/library.py b/masque/library.py index 1a707a2..a645bb8 100644 --- a/masque/library.py +++ b/masque/library.py @@ -42,6 +42,9 @@ class Library(Mapping[str, Pattern], metaclass=ABCMeta): #__contains__, keys, items, values, get, __eq__, __ne__ supplied by Mapping + def abstract_view(self) -> 'AbstractView': + return AbstractView(self) + def abstract(self, name: str) -> Abstract: """ Return an `Abstract` (name & ports) for the pattern in question. @@ -766,3 +769,19 @@ class LazyLibrary(MutableLibrary): def __deepcopy__(self, memo: Optional[Dict] = None) -> 'LazyLibrary': raise LibraryError('LazyLibrary cannot be deepcopied (deepcopy doesn\'t descend into closures)') + + +class AbstractView(Mapping[str, Abstract]): + library: Library + + def __init__(self, library: Library) -> None: + self.library = library + + def __getitem__(self, key: str) -> Abstract: + return self.library.abstract(key) + + def __iter__(self) -> Iterator[str]: + return self.library.__iter__() + + def __len__(self) -> int: + return self.library.__len__()