EventHandler.py 825 Bytes
"""
Base event handler

Author: Georg Hopp <ghopp@spamtitan.com>
"""
class EventHandler(object):
    def __init__(self):
        self._dispatcher    = []
        self._event_methods = {}

    def setDispatcher(self, dispatcher):
        self._dispatcher.append(dispatcher)

    def getHandledIds(self):
        return self._event_methods.keys()

    def issueEvent(self, eventSource, ident, data = None):
        event = eventSource.emit(ident, data)
        #print 'issue %s(%d) on %s: %s' % (
        #    ident, event.sno, hex(id(event.subject)), self.__class__.__name__)
        for dispatcher in self._dispatcher:
            dispatcher.queueEvent(event)

    def handleEvent(self, event):
        if event.type not in self._event_methods:
            return False

        return self._event_methods[event.type](event)