Source code for coherence.extern.louie

"""
    Wrapper module for the louie implementation
"""

import warnings

from coherence.dispatcher import Dispatcher

warnings.warn(
    "extern.louie will soon be deprecated in favor of coherence.dispatcher.")


[docs]class Any(object): pass
[docs]class All(object): pass
[docs]class Anonymous(object): pass
# fake the API
[docs]class Dummy(object): pass
signal = Dummy() sender = Dummy() # senders sender.Anonymous = Anonymous sender.Any = Any # signals signal.All = All # a slightly less raise-y-ish implementation as louie was not so picky, too
[docs]class GlobalDispatcher(Dispatcher):
[docs] def connect(self, signal, callback, *args, **kw): if signal not in self.receivers: # ugly hack self.receivers[signal] = [] return Dispatcher.connect(self, signal, callback, *args, **kw)
[docs] def _get_receivers(self, signal): try: return self.receivers[signal] except KeyError: return []
global _global_dispatcher _global_dispatcher = GlobalDispatcher() _global_receivers_pool = {}
[docs]def reset(): """Reset the state of Louie. Useful during unit testing. Should be avoided otherwise. """ global _global_dispatcher, _global_receivers_pool _global_dispatcher = GlobalDispatcher() _global_receivers_pool = {}
[docs]def connect(receiver, signal=All, sender=Any, weak=True): callback = receiver if signal in (Any, All): raise NotImplementedError( "This is not allowed. Signal HAS to be something") receiver = _global_dispatcher.connect(signal, callback) _global_receivers_pool[(callback, signal)] = receiver return receiver
[docs]def disconnect(receiver, signal=All, sender=Any, weak=True): callback = receiver if signal in (Any, All): raise NotImplementedError( "This is not allowed. Signal HAS to be something") receiver = _global_receivers_pool.pop((callback, signal)) return _global_dispatcher.disconnect(receiver)
[docs]def send(signal=All, sender=Anonymous, *arguments, **named): if signal in (Any, All): raise NotImplementedError( "This is not allowed. Signal HAS to be something") # the first value of the callback shall always be the signal: return _global_dispatcher.save_emit(signal, *arguments, **named)
[docs]def send_minimal(signal=All, sender=Anonymous, *arguments, **named): return send(signal, sender, *arguments, **named)
[docs]def send_exact(signal=All, sender=Anonymous, *arguments, **named): return send(signal, sender, *arguments, **named)
[docs]def send_robust(signal=All, sender=Anonymous, *arguments, **named): return send(signal, sender, *arguments, **named)