Package horizons :: Package messaging :: Module message :: Class Message
[hide private]
[frames] | no frames]

Class Message

source code

Message class for the MessageBus. Every Message that is supposed to be sent through the MessageBus has to subclass this base class, to ensure proper setting of base attributes and inheriting the interface.

The first argument in each message is always a reference to the sender, additional expected arguments are defined on the class-level attribute `arguments`, these will be stored on the instance.

Instance Methods [hide private]
 
__init__(self, sender, *args) source code
Class Methods [hide private]
 
subscribe(cls, callback, sender=None)
Register a callback to be called whenever a message of this type is send.
source code
 
unsubscribe(cls, callback, sender=None)
Stop your subscription of this message type for the specified callback.
source code
 
discard(cls, callback, sender=None)
Similar to `Message.unsubscribe`, but does not raise an error if the callback has not been registered before.
source code
 
broadcast(cls, *args)
Send a message that is initialized with `args`.
source code
Class Variables [hide private]
  arguments = tuple()
  bus = MessageBus
Method Details [hide private]

subscribe(cls, callback, sender=None)
Class Method

source code 
Register a callback to be called whenever a message of this type is send.

callback - Callable that receives an instance of a message as only argument.

sender  -       If specified, the callback receives only messages that originated
                        from sender. By default, all messages are received.

Example:

        >>> def cb(msg):
        ...     print 'Received', msg

        >>> MessageClass.subscribe(cb)  # Global
        >>> MessageClass.subscribe(cb, sender=foo) # Specific sender

unsubscribe(cls, callback, sender=None)
Class Method

source code 
Stop your subscription of this message type for the specified callback.

callback -      Callable that receives an instance of a message as only argument.
                        The same you've been using with `Message.subscribe`.

sender  -       If specified, the subscription will only be stopped for messages
                        from this sender. By default, all subscriptions are ended.

Note: There has to be a subscription, otherwise an error will be raised.

Example:

        >>> MessageClass.subscribe(cb)
        >>> MessageClass.broadcast('sender')
        message received
        >>> MessageClass.unsubscribe(cb)
        >>> MessageClass.broadcast('sender')

broadcast(cls, *args)
Class Method

source code 

Send a message that is initialized with `args`.

The first argument is always a sender, the number of arguments has to be N + 1, with N being the number of arguments defined on the message class.

Example:

>>> class Foo(Message):
...     arguments = ('a', 'b', )
>>> Foo.broadcast('sender', 1, 2)