File size: 1,247 Bytes
05b4fca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import warnings
from typing import Callable
class Registry:
def __init__(self, managed_thing: str):
"""
Create a new registry.
Args:
managed_thing: A string describing what type of thing is managed by this registry. Will be used for
warnings and errors, so it's a good idea to keep this string globally unique and easily understood.
"""
self.managed_thing = managed_thing
self._registry = {}
def register(self, name: str) -> Callable:
def inner_wrapper(wrapped_class) -> Callable:
if name in self._registry:
warnings.warn(f"{self.managed_thing} with name '{name}' doubly registered, old class will be replaced.")
self._registry[name] = wrapped_class
return wrapped_class
return inner_wrapper
def get_by_name(self, name: str):
"""Get a managed thing by name."""
if name in self._registry:
return self._registry[name]
else:
raise ValueError(f"{self.managed_thing} with name '{name}' unknown.")
def get_all_names(self):
"""Get the list of things' names registered to this registry."""
return list(self._registry.keys())
|