Package nbxmpp :: Module plugin |
[hide private]
[frames] | no frames]

# Source Code for Module nbxmpp.plugin

     1  ##   plugin.py
     2  ##
     3  ##   Copyright (C) 2003-2005 Alexey "Snake" Nezhdanov
     4  ##
     5  ##   This program is free software; you can redistribute it and/or modify
     6  ##   it under the terms of the GNU General Public License as published by
     7  ##   the Free Software Foundation; either version 2, or (at your option)
     8  ##   any later version.
     9  ##
    10  ##   This program is distributed in the hope that it will be useful,
    11  ##   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  ##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  ##   GNU General Public License for more details.
    14
    15  # $Id: client.py,v 1.52 2006/01/02 19:40:55 normanr Exp $
    16
    17  """
    18  Provides PlugIn class functionality to develop extentions for xmpppy
    19  """
    20
    21  import logging
    22  log = logging.getLogger('nbxmpp.plugin')
    23
    24 -class 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
    36 -    def __init__(self):
    37          self._exported_methods=[]
    38
    39 -    def PlugIn(self, owner):
    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              # FIXME: I need BOSHDispatcher or XMPPDispatcher on .Dispatcher
    57              # there must be a better way..
    58              owner.__dict__['Dispatcher']=self
    59          else:
    60              owner.__dict__[self.__class__.__name__]=self
    61
    62          # Execute hook
    63          if hasattr(self, 'plugin'):
    64              return self.plugin(owner)
    65
    66 -    def PlugOut(self):
    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          # FIXME: Dispatcher workaround
    78          if self.__class__.__name__.endswith('Dispatcher'):
    79              del self._owner.__dict__['Dispatcher']
    80          else:
    81              del self._owner.__dict__[self.__class__.__name__]
    82          # Execute hook
    83          if hasattr(self, 'plugout'):
    84              return self.plugout()
    85          del self._owner
    86
    87      @classmethod
    88 -    def get_instance(cls, *args, **kwargs):
    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