File: signals.py

package info (click to toggle)
frescobaldi 3.0.0~git20161001.0.eec60717%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,792 kB
  • ctags: 5,843
  • sloc: python: 37,853; sh: 180; makefile: 69
file content (319 lines) | stat: -rw-r--r-- 10,700 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 2014 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

"""
A simple signal/slot implementation.

Functions or methods can be connected to Signal instances, and when the
Signal instance is called (or its emit() method is called, which is equivalent),
all connected methods or function are automatically called.

When a Signal is created as a class attribute and accessed via an instance of
that class, it creates a Signal instance specifically for that object.

When methods are connected, no reference is kept to the method's object. When
the object is garbage collected, the signal is automatically disconnected.

A special Signal variation is also available, the SignalContext. Methods or
functions connected to this signal should return context managers which are
entered when the signal is entered in a context (with) block.

"""

import bisect
import contextlib
import types
import weakref
import sys


__all__ = ["Signal", "SignalContext"]


class Signal(object):
    """A Signal can be emitted and receivers (slots) can be connected to it.
    
    An example:
    
    class MyObject(object):
    
        somethingChanged = Signal()
        
        def __init__(self):
            pass # etc
            
        def doSomething(self):
            ... do things ...
            self.somethingChanged("Hi there!")     # emit the signal
    
    def receiver(arg):
        print("Received message:", arg)
    
    
    >>> o = MyObject()
    >>> o.somethingChanged.connect(receiver)
    >>> o.doSomething()
    Received message: Hi there!
    
    A Signal() can be used directly or as a class attribute, but can also be
    accessed as an attribute of an instance, in which case it creates a Signal
    instance for that instance.

    The signal is emitted by the emit() method or by simply invoking it.
    
    It is currently not possible to enforce argument types that should be used
    when emitting the signal. But if called methods or functions expect fewer
    arguments than were given on emit(), the superfluous arguments are left out.
    
    Methods or functions are connected using connect() and disconnected using
    disconnect(). It is no problem to call connect() or disconnect() more than
    once for the same function or method. Only one connection to the same method
    or function can exist.
    
    """
    
    def __init__(self, owner=None):
        """Creates the Signal.
        
        If owner is given (must be a keyword argument) a weak reference to it is
        kept, and this allows a Signal to be connected to another Signal. When
        the owner dies, the connection is removed.
        
        """
        self.listeners = []
        self._blocked = False
        self._owner = weakref.ref(owner) if owner else lambda: None
        
    def __get__(self, instance, cls):
        """Called when accessing as a descriptor: returns another instance."""
        if instance is None:
            return self
        try:
            return self._instances[instance]
        except AttributeError:
            self._instances = weakref.WeakKeyDictionary()
        except KeyError:
            pass
        ret = self._instances[instance] = type(self)(owner=instance)
        return ret
    
    def owner(self):
        """Returns the owner of this Signal, if any."""
        return self._owner()
        
    def connect(self, slot, priority=0, owner=None):
        """Connects a method or function ('slot') to this Signal.
        
        The priority argument determines the order the connected slots are
        called. A lower value calls the slot earlier.
        If owner is given, the connection will be removed if owner is garbage
        collected.
        
        A slot that is already connected will not be connected twice.
        
        If slot is an instance method (bound method), the Signal keeps no
        reference to the object the method belongs to. So if the object is
        garbage collected, the signal is automatically disconnected.
        
        If slot is a (normal or lambda) function, the Signal will keep a
        reference to the function. If you want to have the function disconnected
        automatically when some object dies, you should provide that object
        through the owner argument. Be sure that the connected function does not
        keep a reference to that object in that case!
        
        """
        key = self.makeListener(slot, owner)
        if key not in self.listeners:
            key.add(self, priority)
            
    def disconnect(self, func):
        """Disconnects the method or function.
        
        No exception is raised if there wasn't a connection.
        
        """
        key = self.makeListener(func)
        try:
            self.listeners.remove(key)
        except ValueError:
            pass
    
    def clear(self):
        """Removes all connected slots."""
        del self.listeners[:]
    
    @contextlib.contextmanager
    def blocked(self):
        """Returns a contextmanager that suppresses the signal.
        
        An example (continued from the class documentation):
        
        >>> o = MyObject()
        >>> o.somethingChanged.connect(receiver)
        >>> with o.somethingChanged.blocked():
        ...     o.doSomething()
        (no output)
        
        The doSomething() method will emit the signal but the connected slots
        will not be called.
        
        """
        blocked, self._blocked = self._blocked, True
        try:
            yield
        finally:
            self._blocked = blocked

    def emit(self, *args, **kwargs):
        """Emits the signal.
        
        Unless blocked, all slots will be called with the supplied arguments.
        
        """
        if not self._blocked:
            for l in self.listeners[:]:
                l.call(args, kwargs)
    
    __call__ = emit

    def makeListener(self, func, owner=None):
        """Returns a suitable listener for the given method or function."""
        if isinstance(func, (types.MethodType, types.BuiltinMethodType)):
            return MethodListener(func)
        elif isinstance(func, Signal):
            return FunctionListener(func, owner or func.owner())
        else:
            return FunctionListener(func, owner)


class SignalContext(Signal):
    """A Signal variant where the connected methods or functions should return
    a context manager.
    
    You should use the SignalContext itself also as a context manager, e.g.:
    
    sig = signals.SignalContext()
    
    with sig(args):
        do_something()
    
    This will first call all the connected methods or functions, and then
    enter all the returned context managers. When the context ends,
    all context managers will be exited.
    
    """
    def emit(self, *args, **kwargs):
        if self._blocked:
            managers = []
        else:
            managers = [l.call(args, kwargs) for l in self.listeners]
        return self.signalcontextmanager(managers)
    
    __call__ = emit

    @contextlib.contextmanager
    def signalcontextmanager(self, managers):
        """A context manager handling all contextmanagers from the listeners."""
        # ideas taken from Python's contextlib.nested()
        exits = []
        exc = (None, None, None)
        try:
            for m in managers:
                m.__enter__()
                exits.append(m.__exit__)
            yield
        except:
            exc = sys.exc_info()
        finally:
            while exits:
                exit = exits.pop()
                try:
                    if exit(*exc):
                        exc = (None, None, None)
                except:
                    exc = sys.exc_info()
            if exc != (None, None, None):
                raise # exc[0], exc[1], exc[2]


class ListenerBase(object):

    removeargs = 0

    def __init__(self, func, owner=None):
        self.func = func
        self.obj = owner

    def __lt__(self, other):
        return self.priority < other.priority
    
    def add(self, signal, priority):
        self.priority = priority
        bisect.insort_right(signal.listeners, self)
        if self.obj is not None:
            def remove(wr, selfref=weakref.ref(self), sigref=weakref.ref(signal)):
                self, signal = selfref(), sigref()
                if self and signal:
                    signal.listeners.remove(self)
            self.obj = weakref.ref(self.obj, remove)
        
        # determine the number of arguments allowed
        end = None
        try:
            co = self.func.__code__
            if not co.co_flags & 12:
                # no *args or **kwargs are used, cut off the unwanted arguments
                end = co.co_argcount - self.removeargs
        except AttributeError:
            pass
        self.argslice = slice(0, end)


class MethodListener(ListenerBase):

    removeargs = 1

    def __init__(self, meth):
        obj = meth.__self__
        self.objid = id(meth.__self__)
        try:
            func = meth.__func__
        except AttributeError:
            # c++ methods from PyQt5 object sometimes do not have the __func__ attribute
            func = getattr(meth.__self__.__class__, meth.__name__)
        super(MethodListener, self).__init__(func, obj)

    def __eq__(self, other):
        return self.__class__ is other.__class__ and self.objid == other.objid and self.func is other.func

    def call(self, args, kwargs):
        obj = self.obj()
        if obj is not None:
            return self.func(obj, *args[self.argslice], **kwargs)


class FunctionListener(ListenerBase):

    def __eq__(self, other):
        return self.__class__ is other.__class__ and self.func is other.func

    def call(self, args, kwargs):
        return self.func(*args[self.argslice], **kwargs)