/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Lib/connector.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Thuban/Lib/connector.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 374 - (hide annotations)
Mon Jan 27 14:20:02 2003 UTC (22 years, 1 month ago) by jan
Original Path: trunk/thuban/Thuban/Lib/connector.py
File MIME type: text/x-python
File size: 6110 byte(s)
Replace user string by _() for i18n.

1 bh 6 # This module was copied almost verbatim from Sketch. The only change is
2     # the base class of ConnectorError which was a Sketch specific exception
3     # class.
4    
5     # Sketch - A Python-based interactive drawing program
6     # Copyright (C) 1997, 1998, 2000, 2001 by Bernhard Herzog
7     #
8     # This library is free software; you can redistribute it and/or
9     # modify it under the terms of the GNU Library General Public
10     # License as published by the Free Software Foundation; either
11     # version 2 of the License, or (at your option) any later version.
12     #
13     # This library is distributed in the hope that it will be useful,
14     # but WITHOUT ANY WARRANTY; without even the implied warranty of
15     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16     # Library General Public License for more details.
17     #
18     # You should have received a copy of the GNU Library General Public
19     # License along with this library; if not, write to the Free Software
20     # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21    
22     __version__ = "$Revision$"
23    
24     #
25     # The Connector
26     #
27    
28     import sys
29     from types import MethodType
30    
31 jan 374 from Thuban import _
32    
33 bh 6 class ConnectorError(Exception):
34     pass
35    
36     class Connector:
37    
38     def __init__(self):
39     self.connections = {}
40    
41     def Connect(self, object, channel, function, args):
42     idx = id(object)
43     if self.connections.has_key(idx):
44     channels = self.connections[idx]
45     else:
46     channels = self.connections[idx] = {}
47    
48     if channels.has_key(channel):
49     receivers = channels[channel]
50     else:
51     receivers = channels[channel] = []
52    
53     info = (function, args)
54     try:
55     receivers.remove(info)
56     except ValueError:
57     pass
58     receivers.append(info)
59    
60     def Disconnect(self, object, channel, function, args):
61     try:
62     receivers = self.connections[id(object)][channel]
63     except KeyError:
64     raise ConnectorError, \
65 jan 374 _('no receivers for channel %s of %s') % (channel, object)
66 bh 6 try:
67     receivers.remove((function, args))
68     except ValueError:
69     raise ConnectorError,\
70 jan 374 _('receiver %s%s is not connected to channel %s of %s') \
71 bh 6 % (function, args, channel, object)
72    
73     if not receivers:
74     # the list of receivers is empty now, remove the channel
75     channels = self.connections[id(object)]
76     del channels[channel]
77     if not channels:
78     # the object has no more channels
79     del self.connections[id(object)]
80    
81     def Issue(self, object, channel, *args):
82     #print object, channel, args
83     try:
84     receivers = self.connections[id(object)][channel]
85     except KeyError:
86     return
87     for func, fargs in receivers:
88     try:
89     apply(func, args + fargs)
90     except:
91 jan 374 sys.stderr.write(_("Warning: %s.%s: %s%s\n")
92 bh 6 % (object, channel, func, fargs))
93     import traceback
94     traceback.print_exc()
95    
96     def RemovePublisher(self, object):
97     i = id(object)
98     if self.connections.has_key(i):
99     del self.connections[i]
100     # don't use try: del ... ; except KeyError here. That would create a
101     # new reference of object in a traceback object and this method should
102     # be callable from a __del__ method (at least for versions prior
103     # Python 1.5)
104    
105     def HasSubscribers(self, object):
106     return self.connections.has_key(id(object))
107    
108     def print_connections(self):
109     # for debugging
110     for id, channels in self.connections.items():
111     for name, subscribers in channels.items():
112 bh 62 print hex(id), name
113 bh 6 for func, args in subscribers:
114     if type(func) == MethodType:
115 jan 374 print _('\tmethod %s of %s') % (func.im_func.func_name,
116     func.im_self)
117 bh 6 else:
118     print '\t', func
119    
120    
121    
122     _the_connector = Connector()
123    
124     Connect = _the_connector.Connect
125     Issue = _the_connector.Issue
126     RemovePublisher = _the_connector.RemovePublisher
127     Disconnect = _the_connector.Disconnect
128     def Subscribe(channel, function, *args):
129     return Connect(None, channel, function, args)
130    
131    
132     class Publisher:
133    
134     def __del__(self):
135     # the new finalization code in 1.5.1 might bind RemovePublisher
136     # to None before all objects derived from Publisher are deleted...
137     if RemovePublisher is not None:
138     RemovePublisher(self)
139    
140     def Subscribe(self, channel, func, *args):
141     Connect(self, channel, func, args)
142    
143     def Unsubscribe(self, channel, func, *args):
144     Disconnect(self, channel, func, args)
145    
146     def issue(self, channel, *args):
147     apply(Issue, (self, channel,) + args)
148    
149     def Destroy(self):
150     RemovePublisher(self)
151    
152    
153     class QueueingPublisher(Publisher):
154    
155     def __init__(self):
156     self.clear_message_queue()
157    
158     def queue_message(self, channel, *args):
159     # Put message in the queue. If it is already queued put it at
160     # the end. This is done to make certain that no channel gets
161     # called twice between two calls to flush_message_queue. If the
162     # order of channel invocation is important two or more queues
163     # should be used.
164     message = (channel, args)
165     if message not in self.message_queue:
166     self.message_queue.append(message)
167    
168     def flush_message_queue(self):
169     # Issue all queued messages and make the queue empty
170     #
171     # Issueing messages might result in new messages being queued.
172     # This does not happen in sketch yet (Jul 1997) but let's hope
173     # that we don't get infinite loops here...
174     while self.message_queue:
175     queue = self.message_queue
176     self.message_queue = []
177     for channel, args in queue:
178     apply(Issue, (self, channel) + args)
179    
180     def clear_message_queue(self):
181     self.message_queue = []
182    

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26