2005-04-26 20:45:54 +02:00
|
|
|
## client.py
|
|
|
|
##
|
|
|
|
## Copyright (C) 2003-2005 Alexey "Snake" Nezhdanov
|
|
|
|
##
|
|
|
|
## This program is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published by
|
|
|
|
## the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
## any later version.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
|
2006-01-18 21:46:29 +01:00
|
|
|
# $Id: client.py,v 1.52 2006/01/02 19:40:55 normanr Exp $
|
2005-04-26 20:45:54 +02:00
|
|
|
|
2008-07-18 02:34:49 +02:00
|
|
|
'''
|
2005-04-26 20:45:54 +02:00
|
|
|
Provides PlugIn class functionality to develop extentions for xmpppy.
|
2008-07-18 02:34:49 +02:00
|
|
|
'''
|
2005-04-26 20:45:54 +02:00
|
|
|
|
2008-07-03 01:29:10 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger('gajim.c.x.plugin')
|
2005-04-26 20:45:54 +02:00
|
|
|
|
|
|
|
class PlugIn:
|
2008-12-24 14:25:57 +01:00
|
|
|
'''
|
|
|
|
Abstract xmpppy plugin infrastructure code, providing plugging in/out and
|
|
|
|
debugging functionality.
|
|
|
|
|
|
|
|
Inherit to develop pluggable objects. No code change on the owner class
|
|
|
|
required (the object where we plug into)
|
|
|
|
'''
|
2008-06-09 02:32:02 +02:00
|
|
|
def __init__(self):
|
|
|
|
self._exported_methods=[]
|
|
|
|
|
2008-12-24 14:25:57 +01:00
|
|
|
def PlugIn(self, owner):
|
|
|
|
'''
|
|
|
|
Attach to owner and register ourself and our _exported_methods in it.
|
|
|
|
If defined by a subclass, call self.plugin(owner) to execute hook
|
|
|
|
code after plugging.
|
|
|
|
'''
|
2008-06-09 02:32:02 +02:00
|
|
|
self._owner=owner
|
2008-12-24 14:25:57 +01:00
|
|
|
log.info('Plugging %s __INTO__ %s' % (self, self._owner))
|
2008-12-15 22:06:08 +01:00
|
|
|
if self.__class__.__name__ in owner.__dict__:
|
2008-07-03 01:29:10 +02:00
|
|
|
log.debug('Plugging ignored: another instance already plugged.')
|
|
|
|
return
|
2008-06-09 02:32:02 +02:00
|
|
|
self._old_owners_methods=[]
|
|
|
|
for method in self._exported_methods:
|
2008-12-15 22:06:08 +01:00
|
|
|
if method.__name__ in owner.__dict__:
|
2008-06-09 02:32:02 +02:00
|
|
|
self._old_owners_methods.append(owner.__dict__[method.__name__])
|
|
|
|
owner.__dict__[method.__name__]=method
|
2008-07-08 01:04:10 +02:00
|
|
|
if self.__class__.__name__.endswith('Dispatcher'):
|
2008-12-15 22:06:08 +01:00
|
|
|
# FIXME: I need BOSHDispatcher or XMPPDispatcher on .Dispatcher
|
2008-07-08 01:04:10 +02:00
|
|
|
# there must be a better way..
|
|
|
|
owner.__dict__['Dispatcher']=self
|
|
|
|
else:
|
|
|
|
owner.__dict__[self.__class__.__name__]=self
|
2008-12-24 14:25:57 +01:00
|
|
|
|
|
|
|
# Execute hook
|
|
|
|
if hasattr(self,'plugin'):
|
|
|
|
return self.plugin(owner)
|
2008-12-15 22:06:08 +01:00
|
|
|
|
2008-06-09 02:32:02 +02:00
|
|
|
def PlugOut(self):
|
2008-12-24 14:25:57 +01:00
|
|
|
'''
|
|
|
|
Unregister our _exported_methods from owner and detach from it.
|
|
|
|
If defined by a subclass, call self.plugout() after unplugging to execute
|
|
|
|
hook code.
|
|
|
|
'''
|
2008-12-15 22:06:08 +01:00
|
|
|
log.info('Plugging %s __OUT__ of %s.' % (self, self._owner))
|
2008-12-24 14:25:57 +01:00
|
|
|
for method in self._exported_methods:
|
|
|
|
del self._owner.__dict__[method.__name__]
|
|
|
|
for method in self._old_owners_methods:
|
|
|
|
self._owner.__dict__[method.__name__]=method
|
|
|
|
# FIXME: Dispatcher workaround
|
2008-07-08 01:04:10 +02:00
|
|
|
if self.__class__.__name__.endswith('Dispatcher'):
|
|
|
|
del self._owner.__dict__['Dispatcher']
|
|
|
|
else:
|
|
|
|
del self._owner.__dict__[self.__class__.__name__]
|
2008-12-24 14:25:57 +01:00
|
|
|
# Execute hook
|
|
|
|
if hasattr(self,'plugout'):
|
|
|
|
return self.plugout()
|
2008-06-09 02:32:02 +02:00
|
|
|
del self._owner
|
2008-07-29 21:49:31 +02:00
|
|
|
|
2008-10-11 12:06:49 +02:00
|
|
|
# vim: se ts=3:
|