1   
 2   
 3   
 4   
 5   
 6   
 7   
 8   
 9   
10   
11   
12   
13   
14   
15   
16   
17  """ 
18  Provides PlugIn class functionality to develop extentions for xmpppy 
19  """ 
20   
21  import logging 
22  log = logging.getLogger('nbxmpp.plugin') 
25      """ 
26      Abstract xmpppy plugin infrastructure code, providing plugging in/out and 
27      debugging functionality 
28   
29      Inherit to develop pluggable objects. No code change on the owner class 
30      required (the object where we plug into) 
31   
32      For every instance of PlugIn class the 'owner' is the class in what the plug 
33      was plugged. 
34      """ 
35   
37          self._exported_methods=[] 
 38   
40          """ 
41          Attach to owner and register ourself and our _exported_methods in it. 
42          If defined by a subclass, call self.plugin(owner) to execute hook 
43          code after plugging 
44          """ 
45          self._owner=owner 
46          log.info('Plugging %s __INTO__ %s' % (self, self._owner)) 
47          if self.__class__.__name__ in owner.__dict__: 
48              log.debug('Plugging ignored: another instance already plugged.') 
49              return 
50          self._old_owners_methods=[] 
51          for method in self._exported_methods: 
52              if method.__name__ in owner.__dict__: 
53                  self._old_owners_methods.append(owner.__dict__[method.__name__]) 
54              owner.__dict__[method.__name__]=method 
55          if self.__class__.__name__.endswith('Dispatcher'): 
56               
57               
58              owner.__dict__['Dispatcher']=self 
59          else: 
60              owner.__dict__[self.__class__.__name__]=self 
61   
62           
63          if hasattr(self, 'plugin'): 
64              return self.plugin(owner) 
 65   
67          """ 
68          Unregister our _exported_methods from owner and detach from it. 
69          If defined by a subclass, call self.plugout() after unplugging to execute 
70          hook code 
71          """ 
72          log.info('Plugging %s __OUT__ of %s.' % (self, self._owner)) 
73          for method in self._exported_methods: 
74              del self._owner.__dict__[method.__name__] 
75          for method in self._old_owners_methods: 
76              self._owner.__dict__[method.__name__]=method 
77           
78          if self.__class__.__name__.endswith('Dispatcher'): 
79              del self._owner.__dict__['Dispatcher'] 
80          else: 
81              del self._owner.__dict__[self.__class__.__name__] 
82           
83          if hasattr(self, 'plugout'): 
84              return self.plugout() 
85          del self._owner 
 86   
87      @classmethod 
89          """ 
90          Factory Method for object creation 
91   
92          Use this instead of directly initializing the class in order to make 
93          unit testing easier. For testing, this method can be patched to inject 
94          mock objects. 
95          """ 
96          return cls(*args, **kwargs) 
  97