def __init__(self): self.deferred: typing.Dict[str, str] = {} self.changed = blinker.Signal() self.errored = blinker.Signal() # Options must be the last attribute here - after that, we raise an # error for attribute assigment to unknown options. self._options: typing.Dict[str, typing.Any] = {}
class FakeReader(object): """ A fake `gnsq.Reader` class. This is swapped out automatically for every test in this module by the `fake_reader` fixture. """ on_exception = blinker.Signal() on_error = blinker.Signal() on_giving_up = blinker.Signal() def __init__(self, topic, channel, **kwargs): self.topic = topic self.channel = channel self.kwargs = kwargs def simulate_exception(self, message=None, error=None): if message is None: message = FakeMessage(body="a message") if error is None: error = RuntimeError("explosion!") self.on_exception.send(self, message=message, error=error) def simulate_error(self, error=None): if error is None: error = RuntimeError("explosion!") self.on_error.send(self, error=error) def simulate_giving_up(self, message=None): if message is None: message = FakeMessage(body="a message") self.on_giving_up.send(self, message=message)
def __init__(self): super().__init__() self._store = {} self.filter = matchall # Should we show only marked flows? self.show_marked = False self.default_order = OrderRequestStart(self) self.orders = dict( time = self.default_order, method = OrderRequestMethod(self), url = OrderRequestURL(self), size = OrderKeySize(self), ) self.order_key = self.default_order self.order_reversed = False self.focus_follow = False self._view = sortedcontainers.SortedListWithKey(key = self.order_key) # These signals broadcast events that affect the view. That is, an # update to a flow in the store but not in the view does not trigger a # signal. All signals are called after the view has been updated. self.sig_update = blinker.Signal() self.sig_add = blinker.Signal() self.sig_remove = blinker.Signal() # Signals that the view should be refreshed completely self.sig_refresh = blinker.Signal() self.focus = Focus(self) self.settings = Settings(self)
def __init__(self, callback=None): self._default_callback = callback self._actions = {DEFAULT_CONTEXT: {}} self._bindunbind_callbacks = blinker.Signal() self._keychain_callbacks = blinker.Signal() self._keychain_partial = [] self._keychain_context = NO_CONTEXT
def __init__(self, request, *args, interval=1, loop=None, **kwargs): self.loop = loop if loop is not None else asyncio.get_event_loop() self._on_response = blinker.Signal() self._on_error = blinker.Signal() self._prev_error = None self._interval = interval self._poll_task = None self._poll_loop_task = None self._sleep = SleepUneasy(loop=loop) self._skip_ongoing_request = False self.set_request(request, *args, **kwargs)
def __init__(self): self.pending = HashPathQueue() self.running = set() self.running_lock = threading.Lock() self.cancel = dict() self.cancel_lock = threading.Lock() self.task_acked = blinker.Signal() self.task_putted = blinker.Signal()
def __init__(self, request, *args, interval=1, **kwargs): self._on_response = blinker.Signal() self._on_error = blinker.Signal() self._prev_error = None self._interval = interval self._poll_task = None self._poll_loop_task = None self._sleep = SleepUneasy() self._skip_ongoing_request = False self._debug_info = {'request': 'No request specified yet', 'update_cbs': [], 'error_cbs': []} self.set_request(request, *args, **kwargs)
class MotionDetector: start = blinker.Signal() end = blinker.Signal() def __init__(self, pin): self._sensor = gpiozero.MotionSensor(pin) def run(self): while True: self._sensor.wait_for_motion() self.start.send(self) self._sensor.wait_for_no_motion() self.end.send(self)
def test_signal_weak_receiver_vanishes(): # non-edge-case path for weak receivers is exercised in the ANY sender # test above. sentinel = Sentinel() sig = blinker.Signal() connected = sentinel.make_receiver('receiver_connected') disconnected = sentinel.make_receiver('receiver_disconnected') receiver1 = sentinel.make_receiver('receiver1') receiver2 = sentinel.make_receiver('receiver2') sig.receiver_connected.connect(connected) sig.receiver_disconnected.connect(disconnected) # explicit disconnect on a weak does emit the signal sig.connect(receiver1, weak=True) sig.disconnect(receiver1) assert len(sentinel) == 2 assert sentinel[-1][2]['receiver'] is receiver1 del sentinel[:] sig.connect(receiver2, weak=True) assert len(sentinel) == 1 del sentinel[:] # holds a ref to receiver2 del receiver2 collect_acyclic_refs() # no disconnect signal is fired assert len(sentinel) == 0 # and everything really is disconnected sig.send('abc') assert len(sentinel) == 0
def on_error(self): """Emitted when an error is received. The signal sender is the consumer and the ``error`` is sent as an argument. """ return blinker.Signal(doc='Emitted when a error is received.')
def on_auth(self): """Emitted after a connection is successfully authenticated. The signal sender is the consumer and the ``conn`` and parsed ``response`` are sent as arguments. """ return blinker.Signal(doc='Emitted when a response is received.')
def on_finish(self): """Emitted after a message is successfully finished. The signal sender is the consumer and the ``message_id`` is sent as an argument. """ return blinker.Signal(doc='Emitted after the a message is finished.')
def on_response(self): """Emitted when a response is received. The signal sender is the consumer and the ` ` is sent as an argument. """ return blinker.Signal(doc='Emitted when a response is received.')
def test_filtered_receiver(): sentinel = [] def received(sender): sentinel.append(sender) sig = blinker.Signal() sig.connect(received, 123) assert not sentinel sig.send() assert not sentinel sig.send(123) assert sentinel == [123] sig.send() assert sentinel == [123] sig.disconnect(received, 123) sig.send(123) assert sentinel == [123] sig.connect(received, 123) sig.send(123) assert sentinel == [123, 123] sig.disconnect(received) sig.send(123) assert sentinel == [123, 123]
def test_decorated_receiver(): sentinel = [] class Object(object): pass obj = Object() sig = blinker.Signal() @sig.connect_via(obj) def receiver(sender, **kw): sentinel.append(kw) assert not sentinel sig.send() assert not sentinel sig.send(1) assert not sentinel sig.send(obj) assert sig.receivers del receiver collect_acyclic_refs() assert sig.receivers
def __init__(self, path_getters, rpc, settings): self._path_getters = tuple(path_getters) self._rpc = rpc self._on_update = blinker.Signal() self._info = defaultdict( lambda: SimpleNamespace(path=None, free=None, error=None)) settings.on_update(self._gather_info_wrapper)
def __init__(self, name=None, value=None): if value is None: value = copy.deepcopy(self.default) self._signal = blinker.Signal() self._name = name self._value = value self._secure = False
class AbstractPipe(object, metaclass=abc.ABCMeta): """ The base class for all valid pipes. """ signal = blinker.Signal() @abc.abstractmethod def accept(self, record: Record) -> None: """ Accepts a record for placement in the pipe .. note:: Does not ensure placement in the pipe if engine is closing :param record: The record to place in the pipe :type record: Record :returns: Does not return :rtype: None """ raise NotImplementedError() @abc.abstractmethod def validate(self) -> bool: """ Self validates the pipe. :returns: True if the pipe is value, otherwise False :rtype: bool """ raise NotImplementedError()
def on_requeue(self): """Emitted after :meth:`requeue`. The signal sender is the message instance and sends the ``timeout`` and a ``backoff`` flag as arguments. """ return blinker.Signal(doc='Emitted after message is requeued.')
class AbstractTranslator(object, metaclass=abc.ABCMeta): """ The base class for all valid translators. """ signal = blinker.Signal() supported_requesters = () @abc.abstractmethod def validate(self, data: str) -> bool: """ Self validates the data of a supported requester. :param data: The data from a supported requester :type data: str :returns: True if data is valid, otherwise False :rtype: bool """ raise NotImplementedError() @abc.abstractmethod def translate(self, data: str, meta: dict = {}) -> None: """ Translates some given data into a record. :param data: The data returned from a supported requester :type data: str :param meta: Any additional fields given to the requester :type meta: dict :returns: Does not return :rtype: bool """ raise NotImplementedError()
def on_exception(self): """Emitted when an exception is caught while handling a message. The signal sender is the consumer and the ``message`` and ``error`` are sent as arguments. """ return blinker.Signal(doc='Emitted when an exception is caught.')
def on_requeue(self): """Emitted after a message is requeued. The signal sender is the consumer and the ``message_id`` and ``timeout`` are sent as arguments. """ return blinker.Signal(doc='Emitted after the a message is requeued.')
def test_empty_bucket_growth(): sentinel = Sentinel() sig = blinker.Signal() senders = lambda: (len(sig._by_sender), sum(len(i) for i in sig._by_sender.values())) receivers = lambda: (len(sig._by_receiver), sum(len(i) for i in sig._by_receiver.values())) receiver1 = sentinel.make_receiver('receiver1') receiver2 = sentinel.make_receiver('receiver2') class Sender(object): """A weakref-able object.""" sender = Sender() sig.connect(receiver1, sender=sender) sig.connect(receiver2, sender=sender) assert senders() == (1, 2) assert receivers() == (2, 2) sig.disconnect(receiver1, sender=sender) assert senders() == (1, 1) assert receivers() == (2, 1) sig.disconnect(receiver2, sender=sender) assert senders() == (1, 0) assert receivers() == (2, 0) sig._cleanup_bookkeeping() assert senders() == (0, 0) assert receivers() == (0, 0)
def on_message(self): """Emitted when a message frame is received. The signal sender is the connection and the `message` is sent as an argument. """ return blinker.Signal(doc='Emitted when a message frame is received.')
def test_weak_receiver(): sentinel = [] def received(sender, **kw): sentinel.append(kw) sig = blinker.Signal() # XXX: weirdly, under jython an explicit weak=True causes this test # to fail, leaking a strong ref to the receiver somewhere. # http://bugs.jython.org/issue1586 if jython: sig.connect(received) # weak=True by default. else: sig.connect(received, weak=True) del received collect_acyclic_refs() assert not sentinel sig.send() assert not sentinel assert not sig.receivers values_are_empty_sets_(sig._by_receiver) values_are_empty_sets_(sig._by_sender)
def on_close(self): """Emitted after :meth:`close_stream`. Sent after the connection socket has closed. The signal sender is the connection. """ return blinker.Signal(doc='Emitted after the connection is closed.')
def test_filtered_receiver_weakref(): sentinel = [] def received(sender): sentinel.append(sender) class Object(object): pass obj = Object() sig = blinker.Signal() sig.connect(received, obj) assert not sentinel sig.send(obj) assert sentinel == [obj] del sentinel[:] del obj collect_acyclic_refs() # general index isn't cleaned up assert sig.receivers # but receiver/sender pairs are values_are_empty_sets_(sig._by_receiver) values_are_empty_sets_(sig._by_sender)
def on_touch(self): """Emitted after :meth:`touch`. The signal sender is the message instance. """ # ditto, like the others return blinker.Signal(doc='Emitted after message is touched.')
def test_has_receivers(): received = lambda sender: None sig = blinker.Signal() assert not sig.has_receivers_for(None) assert not sig.has_receivers_for(blinker.ANY) sig.connect(received, 'xyz') assert not sig.has_receivers_for(None) assert not sig.has_receivers_for(blinker.ANY) assert sig.has_receivers_for('xyz') class Object(object): pass o = Object() sig.connect(received, o) assert sig.has_receivers_for(o) del received collect_acyclic_refs() assert not sig.has_receivers_for('xyz') assert list(sig.receivers_for('xyz')) == [] assert list(sig.receivers_for(o)) == [] sig.connect(lambda sender: None, weak=False) assert sig.has_receivers_for('xyz') assert sig.has_receivers_for(o) assert sig.has_receivers_for(None) assert sig.has_receivers_for(blinker.ANY) assert sig.has_receivers_for('xyz')
def on_message(self): """Emitted when a message is received. The signal sender is the consumer and the ``message`` is sent as an argument. The ``message_handler`` param is connected to this signal. """ return blinker.Signal(doc='Emitted when a message is received.')