Module signalslot
[hide private]
[frames] | no frames]

Source Code for Module signalslot

 1  from weakref import WeakSet, WeakKeyDictionary 
 2  import inspect 
 3   
 4   
5 -class Signal(object):
6 """ 7 Simple class to emit signals to connected callable receivers. 8 """ 9
10 - def __init__(self):
11 """ 12 Instantiate a new object 13 """ 14 self.funcs = WeakSet() 15 self.meths = WeakKeyDictionary()
16
17 - def connect(self, c):
18 """ 19 Connect a callable as receiver for the signal 20 @param c: signal receiver 21 @type c: Callable 22 """ 23 if inspect.ismethod(c): 24 if c.__self__ not in self.meths: 25 self.meths[c.__self__] = set() 26 27 self.meths[c.__self__].add(c.__func__) 28 else: 29 if c not in self.funcs: 30 self.funcs.add(c)
31
32 - def disconnect(self, c):
33 """ 34 Disconnect the callable from receiving the signal 35 @param c: signal receiver 36 @type c: Callable 37 """ 38 if inspect.ismethod(c): 39 if c.__self__ in self.meths: 40 self.meths[c.__self__].remove(c.__func__) 41 else: 42 if c in self.funcs: 43 self.funcs.remove(c)
44
45 - def disconnectAll(self):
46 """ 47 Disconnects all signal receivers 48 """ 49 self.funcs.clear() 50 self.meths.clear()
51
52 - def emit(self, *args, **kwargs):
53 """ 54 Fires the signal to all connected receivers 55 """ 56 for c in self.funcs: 57 c(*args, **kwargs) 58 59 for obj, funcs in self.meths.items(): 60 for func in funcs: 61 func(obj, *args, **kwargs)
62