1 # -*- coding: iso-8859-15 -*-
2 # CONFIGURATION MANAGEMENT OF EDF VERSION
3 # ======================================================================
4 # COPYRIGHT (C) 1991 - 2002 EDF R&D WWW.CODE-ASTER.ORG
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 OF THE LICENSE, OR
8 # (AT YOUR OPTION) ANY LATER VERSION.
10 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
11 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
12 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
13 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
15 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
16 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
17 # 1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
20 # ======================================================================
22 La classe CONNECTOR sert à enregistrer les observateurs d'objets et à délivrer
23 les messages émis à ces objets.
25 Le principe général est le suivant : un objet (subscriber) s'enregistre aupres du
26 connecteur global (theconnector) pour observer un objet emetteur de messages (publisher)
27 sur un canal donné (channel). Il demande à etre notifie par appel d'une fonction (listener).
28 La séquence est donc :
30 - enregistrement du subscriber pour le publisher : theconnector.Connect(publisher,channel,listener,args)
31 - émission du message par le publisher : theconnector.Emit(publisher,channel,cargs)
33 args et cargs sont des tuples contenant les arguments de la fonction listener qui sera appelée
42 class ConnectorError(Exception):
50 def Connect(self, object, channel, function, args):
51 ###print "Connect",object, channel, function, args
53 if self.connections.has_key(idx):
54 channels = self.connections[idx]
56 channels = self.connections[idx] = {}
58 if channels.has_key(channel):
59 receivers = channels[channel]
61 receivers = channels[channel] = []
63 for funct,fargs in receivers[:]:
65 receivers.remove((funct,fargs))
66 elif (function,args) == (funct(),fargs):
67 receivers.remove((funct,fargs))
69 receivers.append((ref(function),args))
70 ###print "Connect",receivers
73 def Disconnect(self, object, channel, function, args):
75 receivers = self.connections[id(object)][channel]
77 raise ConnectorError, \
78 'no receivers for channel %s of %s' % (channel, object)
80 for funct,fargs in receivers[:]:
82 receivers.remove((funct,fargs))
84 for funct,fargs in receivers:
85 if (function,args) == (funct(),fargs):
86 receivers.remove((funct,fargs))
88 # the list of receivers is empty now, remove the channel
89 channels = self.connections[id(object)]
92 # the object has no more channels
93 del self.connections[id(object)]
96 raise ConnectorError,\
97 'receiver %s%s is not connected to channel %s of %s' \
98 % (function, args, channel, object)
101 def Emit(self, object, channel, *args):
102 ###print "Emit",object, channel, args
104 receivers = self.connections[id(object)][channel]
107 ###print "Emit",object, channel, receivers
108 # Attention : copie pour eviter les pbs lies aux deconnexion reconnexion
109 # pendant l'execution des emit
110 for rfunc, fargs in copy(receivers):
114 apply(func, args + fargs)
116 # Le receveur a disparu
117 if (rfunc,fargs) in receivers:receivers.remove((rfunc,fargs))
119 traceback.print_exc()
121 def ref(target,callback=None):
122 if hasattr(target,"im_self"):
123 return BoundMethodWeakref(target)
125 return weakref.ref(target,callback)
127 class BoundMethodWeakref(object):
128 def __init__(self,callable):
129 self.Self=weakref.ref(callable.im_self)
130 self.Func=weakref.ref(callable.im_func)
134 if not target:return None
137 return func.__get__(self.Self())
139 _the_connector =CONNECTOR()
140 Connect = _the_connector.Connect
141 Emit = _the_connector.Emit
142 Disconnect = _the_connector.Disconnect
144 if __name__ == "__main__":
160 Connect(a,"add",b.add,())
161 Connect(a,"add",b.add,())
162 Connect(a,"add",c.add,())
163 Connect(a,"add",f,())
171 Disconnect(a,"add",c.add,())