def register(self, key, func):
"""
Registration.
For a specific registry instance, a function can only be registered
ONCE, otherwise a KeyError will be raised.
"""
if hasattr(func, self.func_property_name):
raise KeyError('Function %r has been registered.' % func)
if key not in self.signal_mapping:
self.signal_mapping[key] = Signal(providing_args=self.providing_args) # noqa
# The receiver function to be connected to a signal
def _receiver(sender, **kwargs):
args = (kwargs.get(k, None) for k in self.providing_args)
func(*args)
# Attach the receiver onto the function to avoid garbage collection.
setattr(func, self.func_property_name, (key, _receiver))
self.signal_mapping[key].connect(_receiver)
self.perform_register(key, func)