EventSubject.py 1.25 KB
"""
Methodology to craete Events, that can be uniquely identified.

@Author: Georg Hopp <ghopp@spamtitan.com>
"""
from Event import Event

class EventSubject(object):
    _EVENTS = {}

    @classmethod
    def eventId(cls, ident):
        """
        Get a unique event identifier based on the class of the event source
        and the found map value of ident. If there is no mapping in the
        current class its EventSource bases super classes are queried until
        an event id can be found... if you derive one event source from
        multiple others that provide the same event identifier this means that
        you can't predict which one will be created.
        I guess that there might be a more pythonic way to do this with
        something like a generator expression.
        """
        if ident in cls._EVENTS:
            return (id(cls) << 8) | cls._EVENTS[ident]
        else:
            for base in [b for b in cls.__bases__ if issubclass(b, EventSubject)]:
                event_id = base.eventId(ident)
                if event_id: return event_id

    def emit(self, ident, data = None):
        event = Event(ident, type(self).eventId(ident), self)
        if data: event.setData(data)
        return event

# vim: set ft=python et ts=8 sw=4 sts=4: