Beispiel #1
0
    def __init__(self, key=None, handlers=None):
        HashableImpl.__init__(self)
        self.signalers = dict()
        self._signalers_lock = RLock()
        self.key = key

        if handlers is not None:
            for handler in handlers:
                self.registerHandler(handler)
Beispiel #2
0
    def __init__(self, key=None, handlers=None):
        HashableImpl.__init__(self)
        self.signalers = dict()
        self._signalers_lock = RLock()
        self.key = key

        if handlers is not None:
            for handler in handlers:
                self.registerHandler(handler)
Beispiel #3
0
    def __init__(self, key=None, handlers=None, onNoHandlers=None):
        HashableImpl.__init__(self)
        self.handlers = set()
        self._lock = Lock()
        self.key = key
        self.on_no_handlers = onNoHandlers

        if handlers is not None:
            for handler in handlers:
                self.registerHandler(handler)
Beispiel #4
0
    def __init__(self, key=None, handlers=None, onNoHandlers=None):
        HashableImpl.__init__(self)
        self.handlers = set()
        self._lock = Lock()
        self.key = key
        self.on_no_handlers = onNoHandlers

        if handlers is not None:
            for handler in handlers:
                self.registerHandler(handler)
Beispiel #5
0
    def __init__(self,
                 processSignalFunc,
                 onRegisteredFunc=None,
                 onUnregisteredFunc=None,
                 shouldHandleSignalFunc=None,
                 signalers=None,
                 instant=True,
                 preprocessSignal=None):
        """ Initialize event handler.

            @param processSignalFunc            Called when signal is processed either from cache or directly from
                                                the signaler, depending on the instant parameter. This should be a
                                                function with signature: processSignal(signaler, data).

            @param onRegisteredFunc             Called when this handler is registered to a signaler. This should be
                                                a function with signature: onRegisteredFunc(handler, signaler) where
                                                handler is a reference to this object.

            @param onUnregisteredFunc           Same as onRegisteredFunc but called when unregistered from signaler.

            @param shouldHandleSignalFunc       Called when a signal is initially received, before caching or
                                                processing. This should be a function with signature:
                                                shouldHandleSignalFunc(signaler, data). If true is returned
                                                the signal continues to caching or processing, if false is returned
                                                the signal is discarded silently.

                                                Note: the signal passed to this method will have passed through
                                                the preprocess stage.

            @param instant                      If true signals are cached - one signal per signaler with newer signals
                                                overwriting older ones. Signals are cached until a call to
                                                processCachedSignals is made, at which point they are processed as
                                                normal. If false signals are not cached and are passed straight to
                                                the processSignalFunc as they are received.

            @param preprocessSignal             Called when a signal is initially received (before any caching).
                                                This should be a function with signature:
                                                preprocessSignal(handler, signaler, oldData, newData) which returns the
                                                modified signal.

                                                Using this method a signaler can modify the default behaviour
                                                of overwriting existing cached signals.
                                                For example, it can join multiple signals together so that no
                                                data loss occurs between processCachedSignals calls. Note that this
                                                function is thread safe and can safely modify the contents of oldData
                                                and newData within the context of the handler/signaler paradigm. """
        HashableImpl.__init__(self)

        if onRegisteredFunc is None:
            onRegisteredFunc = lambda x, y: None

        if onUnregisteredFunc is None:
            onUnregisteredFunc = lambda x, y: None

        if shouldHandleSignalFunc is None:
            shouldHandleSignalFunc = lambda x, y: True

        if preprocessSignal is None:
            preprocessSignal = lambda handler, signaler, oldData, newData: newData

        self.registered_to_signalers = set()
        self.on_registered_func = onRegisteredFunc
        self.on_unregistered_func = onUnregisteredFunc
        self.should_handle_signal_func = shouldHandleSignalFunc
        self.instant = instant
        self.preprocess_signal = preprocessSignal
        self.on_signal_func = self._handleSignalDecider

        if not self.instant:
            self._signals_cache_lock = Lock()
            self._signals_cache = dict()

        self.on_process_func = processSignalFunc

        if signalers is not None:
            for signaler in signalers:
                extractSignaler(signaler).registerHandler(self)
Beispiel #6
0
    def __init__(self, processSignalFunc,
                       onRegisteredFunc=None,
                       onUnregisteredFunc=None,
                       shouldHandleSignalFunc=None,
                       signalers=None,
                       instant=True,
                       preprocessSignal=None):
        """ Initialize event handler.

            @param processSignalFunc            Called when signal is processed either from cache or directly from
                                                the signaler, depending on the instant parameter. This should be a
                                                function with signature: processSignal(signaler, data).

            @param onRegisteredFunc             Called when this handler is registered to a signaler. This should be
                                                a function with signature: onRegisteredFunc(handler, signaler) where
                                                handler is a reference to this object.

            @param onUnregisteredFunc           Same as onRegisteredFunc but called when unregistered from signaler.

            @param shouldHandleSignalFunc       Called when a signal is initially received, before caching or
                                                processing. This should be a function with signature:
                                                shouldHandleSignalFunc(signaler, data). If true is returned
                                                the signal continues to caching or processing, if false is returned
                                                the signal is discarded silently.

                                                Note: the signal passed to this method will have passed through
                                                the preprocess stage.

            @param instant                      If true signals are cached - one signal per signaler with newer signals
                                                overwriting older ones. Signals are cached until a call to
                                                processCachedSignals is made, at which point they are processed as
                                                normal. If false signals are not cached and are passed straight to
                                                the processSignalFunc as they are received.

            @param preprocessSignal             Called when a signal is initially received (before any caching).
                                                This should be a function with signature:
                                                preprocessSignal(handler, signaler, oldData, newData) which returns the
                                                modified signal.

                                                Using this method a signaler can modify the default behaviour
                                                of overwriting existing cached signals.
                                                For example, it can join multiple signals together so that no
                                                data loss occurs between processCachedSignals calls. Note that this
                                                function is thread safe and can safely modify the contents of oldData
                                                and newData within the context of the handler/signaler paradigm. """
        HashableImpl.__init__(self)

        if onRegisteredFunc is None:
            onRegisteredFunc = lambda x, y: None

        if onUnregisteredFunc is None:
            onUnregisteredFunc = lambda x, y: None

        if shouldHandleSignalFunc is None:
            shouldHandleSignalFunc = lambda x, y: True

        if preprocessSignal is None:
            preprocessSignal = lambda handler, signaler, oldData, newData: newData

        self.registered_to_signalers = set()
        self.on_registered_func = onRegisteredFunc
        self.on_unregistered_func = onUnregisteredFunc
        self.should_handle_signal_func = shouldHandleSignalFunc
        self.instant = instant
        self.preprocess_signal = preprocessSignal
        self.on_signal_func = self._handleSignalDecider

        if not self.instant:
            self._signals_cache_lock = Lock()
            self._signals_cache = dict()

        self.on_process_func = processSignalFunc

        if signalers is not None:
            for signaler in signalers:
                extractSignaler(signaler).registerHandler(self)