コード例 #1
0
ファイル: bus.py プロジェクト: geektophe/sitebuilder
    def subscribe(self, klass, callback):
        """
        Subscribe a callable to an event type (class).

        An already subscribed callback is ignored.

        @param klass    The event type to subscribe to
        @param callback The callable object to pass event when an event is
                        dispatched.
        """
        if not IEvent.implementedBy(klass):
            raise BusError('Invalid unsubscription, klass should implenent IEvent')

        if not self.subscribers.has_key(klass):
            self.subscribers[klass] = []

        if not callback in self.subscribers[klass]:
            self.subscribers[klass].append(callback)
コード例 #2
0
ファイル: bus.py プロジェクト: geektophe/sitebuilder
    def unsubscribe(self, klass, callback):
        """
        Unsubscribe a callable from an event type (class).

        An already subscribed callback is ignored.

        @param klass    The event type to subscribe to
        @param callback The callable object to pass event when an event is
                        dispatched.
        """
        if not IEvent.implementedBy(klass):
            raise BusError('Invalid unsubscription, klass should implenent IEvent')

        if self.subscribers.has_key(klass):
            # Removes subscriber callback
            if callback in self.subscribers[klass]:
                self.subscribers[klass].remove(callback)

            # Cleans subscriber disctionnary
            if not len(self.subscribers[klass]):
                del self.subscribers[klass]