Salome HOME
CCAR: modifications pour :
[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
41 class CONNECTOR:
42
43   def __init__(self):
44     self.connections={}
45
46   def Connect(self, object, channel, function, args):
47     ###print "Connect",object, channel, function, args
48     idx = id(object)
49     if self.connections.has_key(idx):
50        channels = self.connections[idx]
51     else:
52        channels = self.connections[idx] = {}
53
54     if channels.has_key(channel):
55        receivers = channels[channel]
56     else:
57        receivers = channels[channel] = []
58
59     info = (function, args)
60     if info in receivers:
61        receivers.remove(info)
62     receivers.append(info)
63     ###print "Connect",receivers
64     
65
66   def Disconnect(self, object, channel, function, args):
67     try:
68        receivers = self.connections[id(object)][channel]
69     except KeyError:
70        raise ConnectorError, \
71             'no receivers for channel %s of %s' % (channel, object)
72     try:
73        receivers.remove((function, args))
74     except ValueError:
75        raise ConnectorError,\
76           'receiver %s%s is not connected to channel %s of %s' \
77           % (function, args, channel, object)
78
79     if not receivers:
80        # the list of receivers is empty now, remove the channel
81        channels = self.connections[id(object)]
82        del channels[channel]
83        if not channels:
84           # the object has no more channels
85           del self.connections[id(object)]
86
87   def Emit(self, object, channel, *args):
88     ###print "Emit",object, channel, args
89     try:
90        receivers = self.connections[id(object)][channel]
91     except KeyError:
92        return
93     ###print "Emit",object, channel, receivers
94     # Attention : copie pour eviter les pbs lies aux deconnexion reconnexion
95     # pendant l'execution des emit
96     for func, fargs in copy(receivers):
97        try:
98           apply(func, args + fargs)
99        except:
100           traceback.print_exc()
101
102 _the_connector =CONNECTOR()
103 Connect = _the_connector.Connect
104 Emit = _the_connector.Emit 
105 Disconnect = _the_connector.Disconnect
106
107 if __name__ == "__main__":
108    class A:pass
109    class B:
110      def add(self,a):
111        print "add",a
112
113    a=A()
114    b=B()
115    Connect(a,"add",b.add,())
116    Emit(a,"add",1)