Salome HOME
*** empty log message ***
[tools/eficas.git] / Ihm / CONNECTOR.py
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.
9 #
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.
14 #
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.
18 #
19 #
20 # ======================================================================
21 """
22   La classe CONNECTOR sert à enregistrer les observateurs d'objets et à délivrer
23   les messages émis à ces objets.
24
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 :
29
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)
32
33   args et cargs sont des tuples contenant les arguments de la fonction listener qui sera appelée
34   comme suit::
35
36      listener(cargs+args)
37 """
38 import traceback
39 from copy import copy
40 import weakref
41
42 class ConnectorError(Exception):
43     pass
44
45 class CONNECTOR:
46
47   def __init__(self):
48     self.connections={}
49
50   def Connect(self, object, channel, function, args):
51     ###print "Connect",object, channel, function, args
52     idx = id(object)
53     if self.connections.has_key(idx):
54        channels = self.connections[idx]
55     else:
56        channels = self.connections[idx] = {}
57
58     if channels.has_key(channel):
59        receivers = channels[channel]
60     else:
61        receivers = channels[channel] = []
62
63     for funct,fargs in receivers[:]:
64         if funct() is None:
65            receivers.remove((funct,fargs))
66         elif (function,args) == (funct(),fargs):
67            receivers.remove((funct,fargs))
68
69     receivers.append((ref(function),args))
70     ###print "Connect",receivers
71     
72
73   def Disconnect(self, object, channel, function, args):
74     try:
75        receivers = self.connections[id(object)][channel]
76     except KeyError:
77        raise ConnectorError, \
78             'no receivers for channel %s of %s' % (channel, object)
79
80     for funct,fargs in receivers[:]:
81         if funct() is None:
82            receivers.remove((funct,fargs))
83
84     for funct,fargs in receivers:
85         if (function,args) == (funct(),fargs):
86            receivers.remove((funct,fargs))
87            if not receivers:
88               # the list of receivers is empty now, remove the channel
89               channels = self.connections[id(object)]
90               del channels[channel]
91               if not channels:
92                  # the object has no more channels
93                  del self.connections[id(object)]
94            return
95
96     raise ConnectorError,\
97           'receiver %s%s is not connected to channel %s of %s' \
98           % (function, args, channel, object)
99
100
101   def Emit(self, object, channel, *args):
102     ###print "Emit",object, channel, args
103     try:
104        receivers = self.connections[id(object)][channel]
105     except KeyError:
106        return
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):
111        try:
112           func=rfunc()
113           if func:
114              apply(func, args + fargs)
115           else:
116              # Le receveur a disparu
117              if (rfunc,fargs) in receivers:receivers.remove((rfunc,fargs))
118        except:
119           traceback.print_exc()
120
121 def ref(target,callback=None):
122    if hasattr(target,"im_self"):
123       return BoundMethodWeakref(target)
124    else:
125       return weakref.ref(target,callback)
126
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)
131
132    def __call__(self):
133        target=self.Self()
134        if not target:return None
135        func=self.Func()
136        if func:
137           return func.__get__(self.Self())
138
139 _the_connector =CONNECTOR()
140 Connect = _the_connector.Connect
141 Emit = _the_connector.Emit 
142 Disconnect = _the_connector.Disconnect
143
144 if __name__ == "__main__":
145    class A:pass
146    class B:
147      def add(self,a):
148        print "add",self,a
149      def __del__(self):
150        print "__del__",self
151
152    def f(a):
153      print f,a
154    print "a=A()"
155    a=A()
156    print "b=B()"
157    b=B()
158    print "c=B()"
159    c=B()
160    Connect(a,"add",b.add,())
161    Connect(a,"add",b.add,())
162    Connect(a,"add",c.add,())
163    Connect(a,"add",f,())
164    Emit(a,"add",1)
165    print "del b"
166    del b
167    Emit(a,"add",1)
168    print "del f"
169    del f
170    Emit(a,"add",1)
171    Disconnect(a,"add",c.add,())
172    Emit(a,"add",1)
173
174