1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 from elisa.core.utils.defer import AlreadyCalledError, CancelledError
20 from elisa.plugins.pigment.pigment_controller import PigmentController
21
22 from twisted.internet import task
23 from twisted.python import failure
24
26
27 """
28 Generic base controller that provides some asynchronous loading facilities.
29
30 Every deferred call done in the context of this controller should be
31 registered in the internal dictionary of deferreds so as to keep track of
32 them and allow their cancellation if needed (e.g. when cleaning the
33 controller). Each value of this internal dictionary is a list, meaning that
34 several deferred calls can be associated to one key.
35
36 >>> self.register_deferred(key, deferred)
37
38 To cancel all the deferred calls associated to a given key, do:
39
40 >>> self.cancel_deferreds(key)
41
42 @ivar deferreds: currently pending deferred calls
43 @type deferreds: C{dict} of any immutable type -> C{list} of
44 {elisa.core.utils.defer.Deferred}
45 """
46
50
52 """
53 Register a deferred call to be associated to a given key.
54 """
55 self.deferreds.setdefault(key, []).append(deferred)
56
58 """
59 Cancel all the currently pending deferred calls associated to one given
60 key in the internal dictionary.
61
62 @param key: the key in the internal deferreds dictionary
63 @type key: any immutable type
64 """
65 if not key in self.deferreds:
66 return
67
68 for deferred in self.deferreds[key]:
69
70
71
72
73
74
75
76 if not hasattr(deferred, "cancel"):
77 self.warning("Use of non cancellable deferreds is deprecated. " \
78 "Please use 'elisa.core.utils.defer.Deferred'.")
79 else:
80 try:
81 deferred.cancel()
82 except AlreadyCalledError:
83
84
85 pass
86
87 del self.deferreds[key]
88
90 """
91 Cancel all the currently pending deferred calls.
92 """
93 def cancel_all():
94 for key in self.deferreds.keys():
95 self.cancel_deferreds(key)
96 yield None
97
98 return task.coiterate(cancel_all())
99
105
106 dfr.addCallback(parent_clean)
107 return dfr
108