Package _Framework :: Module Disconnectable
[hide private]
[frames] | no frames]

Source Code for Module _Framework.Disconnectable

 1  #Embedded file name: /Users/versonator/Hudson/live/Projects/AppLive/Resources/MIDI Remote Scripts/_Framework/Disconnectable.py 
 2  """ 
 3  Interface for items that adquire resources. 
 4  """ 
 5  from Util import find_if 
 6   
7 -class Disconnectable(object):
8 """ 9 Represents an entity that holds connections to other objects that 10 should be explicitly cleared to avoid object lifetime problems or 11 leaking listeners. 12 """ 13
14 - def disconnect(self):
15 pass
16 17
18 -class CompoundDisconnectable(Disconnectable):
19 """ 20 Compound disconnectable. Collects other disconnectables and 21 disconnects them recursively. 22 """ 23
24 - def __init__(self, *a, **k):
25 super(CompoundDisconnectable, self).__init__(*a, **k) 26 self._registered_disconnectables = []
27
28 - def register_disconnectable(self, slot):
29 if slot not in self._registered_disconnectables: 30 self._registered_disconnectables.append(slot) 31 return slot
32
33 - def unregister_disconnectable(self, slot):
34 if slot in self._registered_disconnectables: 35 self._registered_disconnectables.remove(slot)
36
37 - def disconnect_disconnectable(self, slot):
38 if slot in self._registered_disconnectables: 39 self._registered_disconnectables.remove(slot) 40 slot.disconnect()
41
42 - def find_disconnectable(self, predicate):
43 return find_if(predicate, self._registered_disconnectables)
44
45 - def has_disconnectable(self, slot):
46 return slot in self._registered_disconnectables
47
48 - def disconnect(self):
49 for slot in self._registered_disconnectables: 50 slot.disconnect() 51 52 self._registered_disconnectables = [] 53 super(CompoundDisconnectable, self).disconnect()
54 55
56 -class disconnectable(object):
57 """ 58 Context manager that will disconnect the given disconnectable when 59 the context is exited. It returns the original disconnectable. 60 """ 61
62 - def __init__(self, managed = None, *a, **k):
63 super(disconnectable, self).__init__(*a, **k) 64 self._managed = managed
65
66 - def __enter__(self):
67 managed = self._managed 68 return managed
69
70 - def __exit__(self, *a, **k):
71 if self._managed is not None: 72 self._managed.disconnect()
73