misc fixes

This commit is contained in:
Jan Petykiewicz 2023-02-06 12:11:53 -08:00 committed by jan
parent 4a6584a60a
commit 8687badac5

View File

@ -38,6 +38,15 @@ ML = TypeVar('ML', bound='MutableLibrary')
LL = TypeVar('LL', bound='LazyLibrary') LL = TypeVar('LL', bound='LazyLibrary')
def _rename_patterns(lib: 'Library', name: str) -> str:
# TODO document rename function
if not name.startswith('_'):
return name
stem = name.split('$')[0]
return lib.get_name(stem)
class Library(Mapping[str, 'Pattern'], metaclass=ABCMeta): class Library(Mapping[str, 'Pattern'], metaclass=ABCMeta):
# inherited abstract functions # inherited abstract functions
#def __getitem__(self, key: str) -> 'Pattern': #def __getitem__(self, key: str) -> 'Pattern':
@ -473,7 +482,7 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta)
ref.target = new_target ref.target = new_target
return self return self
def create(self, base_name: str) -> NamedPattern: def create(self, name: str) -> 'NamedPattern':
""" """
Convenience method to create an empty pattern, choose a name Convenience method to create an empty pattern, choose a name
for it, add it with that name, and return both the pattern and name. for it, add it with that name, and return both the pattern and name.
@ -485,14 +494,14 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta)
(name, pattern) tuple (name, pattern) tuple
""" """
from .pattern import Pattern from .pattern import Pattern
name = self.get_name(base_name) #name = self.get_name(base_name)
npat = NamedPattern(name) npat = NamedPattern(name)
self[name] = npat self[name] = npat
return npat return npat
def name_and_set( def set(
self, self,
base_name: str, name: str,
value: Union['Pattern', Callable[[], 'Pattern']], value: Union['Pattern', Callable[[], 'Pattern']],
) -> str: ) -> str:
""" """
@ -506,7 +515,7 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta)
Returns: Returns:
The name of the pattern. The name of the pattern.
""" """
name = self.get_name(base_name) #name = self.get_name(base_name)
self[name] = value self[name] = value
return name return name
@ -796,6 +805,9 @@ class WrapROLibrary(Library):
def __len__(self) -> int: def __len__(self) -> int:
return len(self.mapping) return len(self.mapping)
def __contains__(self, key: str) -> bool:
return key in self.mapping
def __repr__(self) -> str: def __repr__(self) -> str:
return f'<WrapROLibrary ({type(self.mapping)}) with keys\n' + pformat(list(self.keys())) + '>' return f'<WrapROLibrary ({type(self.mapping)}) with keys\n' + pformat(list(self.keys())) + '>'
@ -821,6 +833,9 @@ class WrapLibrary(MutableLibrary):
def __len__(self) -> int: def __len__(self) -> int:
return len(self.mapping) return len(self.mapping)
def __contains__(self, key: str) -> bool:
return key in self.mapping
def __setitem__( def __setitem__(
self, self,
key: str, key: str,
@ -905,6 +920,8 @@ class LazyLibrary(MutableLibrary):
pat = tree[tree.top] pat = tree[tree.top]
del tree[tree.top] del tree[tree.top]
self.add(tree) self.add(tree)
else:
pat = pat_or_tree
self.cache[key] = pat self.cache[key] = pat
return pat return pat
@ -915,6 +932,9 @@ class LazyLibrary(MutableLibrary):
def __len__(self) -> int: def __len__(self) -> int:
return len(self.dict) return len(self.dict)
def __contains__(self, key: str) -> bool:
return key in self.dict
def _merge(self, key_self: str, other: Mapping[str, 'Pattern'], key_other: str) -> None: def _merge(self, key_self: str, other: Mapping[str, 'Pattern'], key_other: str) -> None:
if isinstance(other, LazyLibrary): if isinstance(other, LazyLibrary):
self.dict[key_self] = other.dict[key_other] self.dict[key_self] = other.dict[key_other]
@ -1009,12 +1029,12 @@ class Tree(MutableLibrary):
library: MutableLibrary library: MutableLibrary
@property @property
def pattern(self) -> Pattern: def pattern(self) -> 'Pattern':
return self.library[self.top] return self.library[self.top]
def __init__( def __init__(
self, self,
top: Union[str, NamedPattern], top: Union[str, 'NamedPattern'],
library: Optional[MutableLibrary] = None library: Optional[MutableLibrary] = None
) -> None: ) -> None:
self.top = top if isinstance(top, str) else top.name self.top = top if isinstance(top, str) else top.name
@ -1047,12 +1067,3 @@ class Tree(MutableLibrary):
def _merge(self, key_self: str, other: Mapping[str, 'Pattern'], key_other: str) -> None: def _merge(self, key_self: str, other: Mapping[str, 'Pattern'], key_other: str) -> None:
self.library._merge(key_self, other, key_other) self.library._merge(key_self, other, key_other)
def _rename_patterns(lib: Library, name: str) -> str:
# TODO document rename function
if not name.startswith('_'):
return name
stem = name.split('$')[0]
return lib.get_name(stem)