1 # Copyright (C) 2006-2024 CEA, EDF
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # -*- coding: iso-8859-15 -*-
23 La classe CONNECTOR sert à enregistrer les observateurs d'objets et à délivrer
24 les messages émis à ces objets.
26 Le principe général est le suivant : un objet (subscriber) s'enregistre aupres du
27 connecteur global (theconnector) pour observer un objet emetteur de messages (publisher)
28 sur un canal donné (channel). Il demande à etre notifie par appel d'une fonction (listener).
29 La séquence est donc :
31 - enregistrement du subscriber pour le publisher : theconnector.Connect(publisher,channel,listener,args)
32 - émission du message par le publisher : theconnector.Emit(publisher,channel,cargs)
34 args et cargs sont des tuples contenant les arguments de la fonction listener qui sera appelée
43 class ConnectorError(Exception):
51 def Connect(self, object, channel, function, args):
52 ###print "Connect",object, channel, function, args
54 if idx in self.connections:
55 channels = self.connections[idx]
57 channels = self.connections[idx] = {}
59 if channel in channels:
60 receivers = channels[channel]
62 receivers = channels[channel] = []
64 for funct,fargs in receivers[:]:
66 receivers.remove((funct,fargs))
67 elif (function,args) == (funct(),fargs):
68 receivers.remove((funct,fargs))
70 receivers.append((ref(function),args))
71 ###print "Connect",receivers
74 def Disconnect(self, object, channel, function, args):
76 receivers = self.connections[id(object)][channel]
78 raise ConnectorError('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('receiver %s%s is not connected to channel %s of %s' \
97 % (function, args, channel, object))
100 def Emit(self, object, channel, *args):
101 ###print "Emit",object, channel, args
103 receivers = self.connections[id(object)][channel]
106 ###print "Emit",object, channel, receivers
107 # Attention : copie pour eviter les pbs lies aux deconnexion reconnexion
108 # pendant l'execution des emit
109 for rfunc, fargs in copy(receivers):
115 # Le receveur a disparu
116 if (rfunc,fargs) in receivers:receivers.remove((rfunc,fargs))
118 traceback.print_exc()
120 def ref(target,callback=None):
121 if hasattr(target,"im_self"):
122 return BoundMethodWeakref(target)
124 return weakref.ref(target,callback)
126 class BoundMethodWeakref(object):
127 def __init__(self,callable):
128 self.Self=weakref.ref(callable.__self__)
129 self.Func=weakref.ref(callable.__func__)
133 if not target:return None
136 return func.__get__(self.Self())
138 _the_connector =CONNECTOR()
139 Connect = _the_connector.Connect
140 Emit = _the_connector.Emit
141 Disconnect = _the_connector.Disconnect
143 if __name__ == "__main__":
149 print("__del__",self)
159 Connect(a,"add",b.add,())
160 Connect(a,"add",b.add,())
161 Connect(a,"add",c.add,())
162 Connect(a,"add",f,())
170 Disconnect(a,"add",c.add,())