Package elisa :: Package plugins :: Package poblesec :: Package base :: Module hierarchy

Source Code for Module elisa.plugins.poblesec.base.hierarchy

  1  # -*- coding: utf-8 -*- 
  2  # Moovida - Home multimedia server 
  3  # Copyright (C) 2006-2009 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Moovida with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Moovida is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Moovida" in the root directory of this distribution package 
 15  # for details on that license. 
 16  # 
 17  # Author: Olivier Tilloy <olivier@fluendo.com> 
 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   
25 -class HierarchyController(PigmentController):
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
47 - def __init__(self):
48 super(HierarchyController, self).__init__() 49 self.deferreds = {}
50
51 - def register_deferred(self, key, deferred):
52 """ 53 Register a deferred call to be associated to a given key. 54 """ 55 self.deferreds.setdefault(key, []).append(deferred)
56
57 - def cancel_deferreds(self, key):
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 # there are 2 cases: 70 # 1) the deferred is a non-cancellable Twisted deferred 71 # (twisted.internet.defer.Deferred) in which case there is no way 72 # to ensure cancellation is happening. 73 # 2) the deferred is a cancellable one 74 # (elisa.core.utils.defer.Deferred) and the 'cancel' method is 75 # called on it. 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 # the whole chain of deferreds was already called and there 84 # is nothing left to cancel 85 pass 86 87 del self.deferreds[key]
88
89 - def cancel_all_deferreds(self):
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
100 - def clean(self):
101 dfr = self.cancel_all_deferreds() 102 103 def parent_clean(result): 104 return super(HierarchyController, self).clean()
105 106 dfr.addCallback(parent_clean) 107 return dfr 108