Package pyxmpp2 :: Package mainloop :: Module events :: Class EventDispatcher
[hide private]

Class EventDispatcher


Dispatches events from an event queue to event handlers.

Events are interfaces.Event instances stored in the event queue (defined by the "event_queue" setting). Event handlers are EventHandler subclass instance methods decorated with the interfaces.event_handler decorator.

Instance Methods [hide private]
 
__init__(self, settings=None, handlers=None)
Initialize the event dispatcher.
 
add_handler(self, handler)
Add a handler object.
 
remove_handler(self, handler)
Remove a handler object.
 
_update_handlers(self)
Update _handler_map after handlers have been modified.
 
dispatch(self, block=False, timeout=None)
Get the next event from the queue and pass it to the appropriate handlers.
 
flush(self, dispatch=True)
Read all events currently in the queue and dispatch them to the handlers unless dispatch is False.
 
loop(self)
Wait for and dispatch events until QUIT is reached.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Instance Variables [hide private]
type -> list of callable mapping _handler_map
mapping of event type to list of handler methods
list of EventHandler handlers
list of handler objects
threading.RLock lock
the thread synchronisation lock
Queue.Queue queue
the event queue
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, settings=None, handlers=None)
(Constructor)

 
Initialize the event dispatcher.
Parameters:
  • settings (XMPPSettings) - the settings. "event_queue" settings provides the event queue object.
  • handlers (iterable of objects) - the initial list of event handler objects.
Overrides: object.__init__

add_handler(self, handler)

 
Add a handler object.
Parameters:
  • handler (EventHandler) - the object providing event handler methods

remove_handler(self, handler)

 
Remove a handler object.
Parameters:
  • handler - the object to remove

dispatch(self, block=False, timeout=None)

 
Get the next event from the queue and pass it to the appropriate handlers.
Parameters:
  • block (bool) - wait for event if the queue is empty
  • timeout (float) - maximum time, in seconds, to wait if block is True
Returns:
the event handled (may be QUIT) or None

flush(self, dispatch=True)

 

Read all events currently in the queue and dispatch them to the handlers unless dispatch is False.

Note: If the queue contains QUIT the events after it won't be removed.

Parameters:
  • dispatch - if the events should be handled (True) or ignored (False)
Returns:
QUIT if the QUIT event was reached.