Make DeviceLibrary code more similar to Library

libzoo
jan 2 years ago
parent b6f1af6e09
commit c2d7290935

@ -26,15 +26,13 @@ class DeviceLibrary:
relevant `Device` object. relevant `Device` object.
This class largely functions the same way as `Library`, but This class largely functions the same way as `Library`, but
operates on `Device`s rather than `Patterns` and thus has no operates on `Device`s rather than `Patterns`.
need for distinctions between primary/secondary devices (as
there is no inter-`Device` hierarchy).
Each device is cached the first time it is used. The cache can Each device is cached the first time it is used. The cache can
be disabled by setting the `enable_cache` attribute to `False`. be disabled by setting the `enable_cache` attribute to `False`.
""" """
generators: Dict[str, Callable[[], Device]] generators: Dict[str, Callable[[], Device]]
cache: Dict[Union[str, Tuple[str, str]], Device] cache: Dict[str, Device]
enable_cache: bool = True enable_cache: bool = True
def __init__(self) -> None: def __init__(self) -> None:
@ -44,11 +42,16 @@ class DeviceLibrary:
def __setitem__(self, key: str, value: Callable[[], Device]) -> None: def __setitem__(self, key: str, value: Callable[[], Device]) -> None:
self.generators[key] = value self.generators[key] = value
if key in self.cache: if key in self.cache:
logger.warning(f'Replaced library item "{key}" & existing cache entry.'
' Previously-generated Device will *not* be updated!')
del self.cache[key] del self.cache[key]
def __delitem__(self, key: str) -> None: def __delitem__(self, key: str) -> None:
del self.generators[key] del self.generators[key]
if key in self.cache: if key in self.cache:
logger.warning(f'Deleting library item "{key}" & existing cache entry.'
' Previously-generated Device may remain in the wild!')
del self.cache[key] del self.cache[key]
def __getitem__(self, key: str) -> Device: def __getitem__(self, key: str) -> Device:
@ -119,10 +122,10 @@ class DeviceLibrary:
raise DeviceLibraryError('Duplicate keys encountered in DeviceLibrary merge: ' raise DeviceLibraryError('Duplicate keys encountered in DeviceLibrary merge: '
+ pformat(conflicts)) + pformat(conflicts))
for name in set(other.generators.keys()) - keep_ours: for key in set(other.keys()) - keep_ours:
self.generators[name] = other.generators[name] self.generators[key] = other.generators[key]
if name in other.cache: if key in other.cache:
self.cache[name] = other.cache[name] self.cache[key] = other.cache[key]
return self return self
def clear_cache(self: D) -> D: def clear_cache(self: D) -> D:

Loading…
Cancel
Save