dispatcher.py
Go to the documentation of this file.
1 from . import event
2 
3 
4 class signal(object):
5  All = event.Event('*')
6 
7 
8 signals = {}
9 
10 
11 def connect(receiver, sig=signal.All):
12  if sig in signals:
13  receivers = signals[sig]
14  else:
15  receivers = signals[sig] = []
16  receivers.append(receiver)
17 
18 
19 def disconnect(receiver, sig=signal.All):
20  if sig is signal.All:
21  for sig in signals:
22  if receiver in signals[sig]:
23  signals[sig].remove(receiver)
24  elif sig in signals:
25  if receiver in signals[sig]:
26  signals[sig].remove(receiver)
27 
28 
29 def send(sig, **named):
30  if sig in signals:
31  receivers = signals[sig] + signals[signal.All]
32  else:
33  receivers = signals[signal.All]
34  for receiver in receivers:
35  receiver(event=sig, **named)
36 
37 
38 if __name__ == '__main__':
39  def handler0(event, sender, **args):
40  recvs.append(0)
41  print('handler0: event=%s sender=%s' % (str(event), str(sender)))
42  print(args)
43 
44  def handler1(event, sender, **args):
45  recvs.append(1)
46  print('handler1: event=%s sender=%s' % (str(event), str(sender)))
47  print(args)
48 
49  test_signal0 = event.Event('test signal0')
50  test_signal1 = event.Event('test signal1')
51  connect(handler0, signal.All)
52  connect(handler1, test_signal0)
53 
54  recvs = []
55  send(test_signal0, sender=None)
56  assert len(recvs) == 2 and 0 in recvs and 1 in recvs
57 
58  recvs = []
59  send(test_signal1, sender=None, data='test data')
60  assert len(recvs) == 1 and 0 in recvs
61 
62  disconnect(handler1)
63 
64  recvs = []
65  send(test_signal0, sender=None, arg0=0, arg1=1, arg2=2)
66  assert len(recvs) == 1 and 0 in recvs
def handler0(event, sender, args)
Definition: dispatcher.py:39
def handler1(event, sender, args)
Definition: dispatcher.py:44
def connect(receiver, sig=signal.All)
Definition: dispatcher.py:11
def disconnect(receiver, sig=signal.All)
Definition: dispatcher.py:19


tello_driver
Author(s): Jordy van Appeven
autogenerated on Wed May 13 2020 03:34:54