|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""Registry utilities.""" |
|
|
|
import collections |
|
import functools |
|
|
|
|
|
class Registry(object): |
|
"""Registry class.""" |
|
|
|
def __init__(self, name): |
|
self.name = name |
|
self.registry = collections.OrderedDict() |
|
|
|
def has(self, key): |
|
return key in self.registry |
|
|
|
def register(self, name, func=None, **kwargs): |
|
def decorated(inner_function): |
|
for key in name if isinstance(name, (tuple, list)) else [name]: |
|
self.registry[key] = functools.partial(inner_function, **kwargs) |
|
return inner_function |
|
|
|
if func is not None: |
|
return decorated(func) |
|
return decorated |
|
|
|
def get(self, name, default=None): |
|
if name is None: |
|
return None |
|
if not self.has(name): |
|
if default is not None: |
|
return default |
|
raise KeyError("`%s` is not registered in <%s>." % (name, self.name)) |
|
return self.registry[name] |
|
|
|
def try_get(self, name): |
|
if self.has(name): |
|
return self.get(name) |
|
return None |
|
|