Add move_references() and auto-move references during add()-with-rename
Also remove enable_cache, since we now rely on the cache.
This commit is contained in:
parent
3be4da3e7c
commit
7191e5f62c
@ -447,6 +447,23 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta)
|
|||||||
del self[old_name]
|
del self[old_name]
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def move_references(self: ML, old_target: str, new_target: str) -> ML:
|
||||||
|
"""
|
||||||
|
Change all references pointing at `old_target` into references pointing at `new_target`.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
old_target: Current reference target
|
||||||
|
new_target: New target for the reference
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
self
|
||||||
|
"""
|
||||||
|
for pattern in self.values():
|
||||||
|
for ref in pattern.refs:
|
||||||
|
if ref.target == old_target:
|
||||||
|
ref.target = new_target
|
||||||
|
return self
|
||||||
|
|
||||||
def create(self, base_name: str) -> NamedPattern:
|
def create(self, base_name: str) -> NamedPattern:
|
||||||
"""
|
"""
|
||||||
Convenience method to create an empty pattern, choose a name
|
Convenience method to create an empty pattern, choose a name
|
||||||
@ -492,6 +509,8 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta)
|
|||||||
"""
|
"""
|
||||||
Add keys from another library into this one.
|
Add keys from another library into this one.
|
||||||
|
|
||||||
|
# TODO explain reference renaming
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
other: The library to insert keys from
|
other: The library to insert keys from
|
||||||
rename_theirs: Called as rename_theirs(self, name) for each duplicate name
|
rename_theirs: Called as rename_theirs(self, name) for each duplicate name
|
||||||
@ -517,9 +536,16 @@ class MutableLibrary(Library, MutableMapping[str, 'Pattern'], metaclass=ABCMeta)
|
|||||||
if conflicts:
|
if conflicts:
|
||||||
raise LibraryError('Unresolved duplicate keys encountered in library merge: ' + pformat(conflicts))
|
raise LibraryError('Unresolved duplicate keys encountered in library merge: ' + pformat(conflicts))
|
||||||
|
|
||||||
for key in other.keys():
|
if rename_map:
|
||||||
new_key = rename_map.get(key, key)
|
temp = WrapLibrary(copy.deepcopy(dict(other))) # Copy and turn into a mutable library
|
||||||
self._merge(new_key, other, key)
|
|
||||||
|
for old_name, new_name in rename_map.items():
|
||||||
|
temp.rename(old_name, new_name)
|
||||||
|
temp.move_references(old_name, new_name)
|
||||||
|
|
||||||
|
else:
|
||||||
|
for key in other.keys():
|
||||||
|
self._merge(key, other, key)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@ -776,13 +802,10 @@ class LazyLibrary(MutableLibrary):
|
|||||||
"""
|
"""
|
||||||
This class is usually used to create a library of Patterns by mapping names to
|
This class is usually used to create a library of Patterns by mapping names to
|
||||||
functions which generate or load the relevant `Pattern` object as-needed.
|
functions which generate or load the relevant `Pattern` object as-needed.
|
||||||
|
|
||||||
The cache can be disabled by setting the `enable_cache` attribute to `False`.
|
|
||||||
"""
|
"""
|
||||||
dict: Dict[str, Callable[[], 'Pattern']]
|
dict: Dict[str, Callable[[], 'Pattern']]
|
||||||
cache: Dict[str, 'Pattern']
|
cache: Dict[str, 'Pattern']
|
||||||
_lookups_in_progress: Set[str]
|
_lookups_in_progress: Set[str]
|
||||||
enable_cache: bool = True
|
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.dict = {}
|
self.dict = {}
|
||||||
@ -813,7 +836,7 @@ class LazyLibrary(MutableLibrary):
|
|||||||
|
|
||||||
def __getitem__(self, key: str) -> 'Pattern':
|
def __getitem__(self, key: str) -> 'Pattern':
|
||||||
logger.debug(f'loading {key}')
|
logger.debug(f'loading {key}')
|
||||||
if self.enable_cache and key in self.cache:
|
if key in self.cache:
|
||||||
logger.debug(f'found {key} in cache')
|
logger.debug(f'found {key} in cache')
|
||||||
return self.cache[key]
|
return self.cache[key]
|
||||||
|
|
||||||
@ -865,6 +888,24 @@ class LazyLibrary(MutableLibrary):
|
|||||||
del self[old_name]
|
del self[old_name]
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def move_references(self: LL, old_target: str, new_target: str) -> LL:
|
||||||
|
"""
|
||||||
|
Change all references pointing at `old_target` into references pointing at `new_target`.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
old_target: Current reference target
|
||||||
|
new_target: New target for the reference
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
self
|
||||||
|
"""
|
||||||
|
self.precache()
|
||||||
|
for pattern in self.cache.values():
|
||||||
|
for ref in pattern.refs:
|
||||||
|
if ref.target == old_target:
|
||||||
|
ref.target = new_target
|
||||||
|
return self
|
||||||
|
|
||||||
def precache(self: LL) -> LL:
|
def precache(self: LL) -> LL:
|
||||||
"""
|
"""
|
||||||
Force all patterns into the cache
|
Force all patterns into the cache
|
||||||
@ -876,18 +917,6 @@ class LazyLibrary(MutableLibrary):
|
|||||||
_ = self.dict.__getitem__(key)
|
_ = self.dict.__getitem__(key)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def clear_cache(self: LL) -> LL:
|
|
||||||
"""
|
|
||||||
Clear the cache of this library.
|
|
||||||
This is usually used before modifying or deleting patterns, e.g. when merging
|
|
||||||
with another library.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
self
|
|
||||||
"""
|
|
||||||
self.cache.clear()
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __deepcopy__(self, memo: Optional[Dict] = None) -> 'LazyLibrary':
|
def __deepcopy__(self, memo: Optional[Dict] = None) -> 'LazyLibrary':
|
||||||
raise LibraryError('LazyLibrary cannot be deepcopied (deepcopy doesn\'t descend into closures)')
|
raise LibraryError('LazyLibrary cannot be deepcopied (deepcopy doesn\'t descend into closures)')
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user