Show More
@@ -1,202 +1,202 b'' | |||||
1 | """ Defines a KernelManager that provides signals and slots. |
|
1 | """ Defines a KernelManager that provides signals and slots. | |
2 | """ |
|
2 | """ | |
3 |
|
3 | |||
4 | # System library imports. |
|
4 | # System library imports. | |
5 | from PyQt4 import QtCore |
|
5 | from PyQt4 import QtCore | |
6 |
|
6 | |||
7 | # IPython imports. |
|
7 | # IPython imports. | |
8 | from IPython.utils.traitlets import Type |
|
8 | from IPython.utils.traitlets import Type | |
9 | from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \ |
|
9 | from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \ | |
10 | XReqSocketChannel, RepSocketChannel, HBSocketChannel |
|
10 | XReqSocketChannel, RepSocketChannel, HBSocketChannel | |
11 | from util import MetaQObjectHasTraits, SuperQObject |
|
11 | from util import MetaQObjectHasTraits, SuperQObject | |
12 |
|
12 | |||
13 |
|
13 | |||
14 | class SocketChannelQObject(SuperQObject): |
|
14 | class SocketChannelQObject(SuperQObject): | |
15 |
|
15 | |||
16 | # Emitted when the channel is started. |
|
16 | # Emitted when the channel is started. | |
17 | started = QtCore.pyqtSignal() |
|
17 | started = QtCore.pyqtSignal() | |
18 |
|
18 | |||
19 | # Emitted when the channel is stopped. |
|
19 | # Emitted when the channel is stopped. | |
20 | stopped = QtCore.pyqtSignal() |
|
20 | stopped = QtCore.pyqtSignal() | |
21 |
|
21 | |||
22 | #--------------------------------------------------------------------------- |
|
22 | #--------------------------------------------------------------------------- | |
23 | # 'ZmqSocketChannel' interface |
|
23 | # 'ZmqSocketChannel' interface | |
24 | #--------------------------------------------------------------------------- |
|
24 | #--------------------------------------------------------------------------- | |
25 |
|
25 | |||
26 | def start(self): |
|
26 | def start(self): | |
27 | """ Reimplemented to emit signal. |
|
27 | """ Reimplemented to emit signal. | |
28 | """ |
|
28 | """ | |
29 | super(SocketChannelQObject, self).start() |
|
29 | super(SocketChannelQObject, self).start() | |
30 | self.started.emit() |
|
30 | self.started.emit() | |
31 |
|
31 | |||
32 | def stop(self): |
|
32 | def stop(self): | |
33 | """ Reimplemented to emit signal. |
|
33 | """ Reimplemented to emit signal. | |
34 | """ |
|
34 | """ | |
35 | super(SocketChannelQObject, self).stop() |
|
35 | super(SocketChannelQObject, self).stop() | |
36 | self.stopped.emit() |
|
36 | self.stopped.emit() | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | class QtXReqSocketChannel(SocketChannelQObject, XReqSocketChannel): |
|
39 | class QtXReqSocketChannel(SocketChannelQObject, XReqSocketChannel): | |
40 |
|
40 | |||
41 | # Emitted when any message is received. |
|
41 | # Emitted when any message is received. | |
42 | message_received = QtCore.pyqtSignal(object) |
|
42 | message_received = QtCore.pyqtSignal(object) | |
43 |
|
43 | |||
44 | # Emitted when a reply has been received for the corresponding request |
|
44 | # Emitted when a reply has been received for the corresponding request | |
45 | # type. |
|
45 | # type. | |
46 | execute_reply = QtCore.pyqtSignal(object) |
|
46 | execute_reply = QtCore.pyqtSignal(object) | |
47 | complete_reply = QtCore.pyqtSignal(object) |
|
47 | complete_reply = QtCore.pyqtSignal(object) | |
48 | object_info_reply = QtCore.pyqtSignal(object) |
|
48 | object_info_reply = QtCore.pyqtSignal(object) | |
49 |
|
49 | |||
50 | # Emitted when the first reply comes back |
|
50 | # Emitted when the first reply comes back | |
51 |
first_reply = QtCore.pyqtSignal( |
|
51 | first_reply = QtCore.pyqtSignal() | |
52 |
|
52 | |||
53 | # Used by the first_reply signal logic to determine if a reply is the |
|
53 | # Used by the first_reply signal logic to determine if a reply is the | |
54 | # first. |
|
54 | # first. | |
55 | _handlers_called = False |
|
55 | _handlers_called = False | |
56 |
|
56 | |||
57 | #--------------------------------------------------------------------------- |
|
57 | #--------------------------------------------------------------------------- | |
58 | # 'XReqSocketChannel' interface |
|
58 | # 'XReqSocketChannel' interface | |
59 | #--------------------------------------------------------------------------- |
|
59 | #--------------------------------------------------------------------------- | |
60 |
|
60 | |||
61 | def call_handlers(self, msg): |
|
61 | def call_handlers(self, msg): | |
62 | """ Reimplemented to emit signals instead of making callbacks. |
|
62 | """ Reimplemented to emit signals instead of making callbacks. | |
63 | """ |
|
63 | """ | |
64 | # Emit the generic signal. |
|
64 | # Emit the generic signal. | |
65 | self.message_received.emit(msg) |
|
65 | self.message_received.emit(msg) | |
66 |
|
66 | |||
67 | # Emit signals for specialized message types. |
|
67 | # Emit signals for specialized message types. | |
68 | msg_type = msg['msg_type'] |
|
68 | msg_type = msg['msg_type'] | |
69 | signal = getattr(self, msg_type, None) |
|
69 | signal = getattr(self, msg_type, None) | |
70 | if signal: |
|
70 | if signal: | |
71 | signal.emit(msg) |
|
71 | signal.emit(msg) | |
72 |
|
72 | |||
73 | if not self._handlers_called: |
|
73 | if not self._handlers_called: | |
74 | self.first_reply.emit() |
|
74 | self.first_reply.emit() | |
75 |
|
75 | |||
76 | self._handlers_called = True |
|
76 | self._handlers_called = True | |
77 |
|
77 | |||
78 | def reset_first_reply(self): |
|
78 | def reset_first_reply(self): | |
79 | """ Reset the first_reply signal to fire again on the next reply. |
|
79 | """ Reset the first_reply signal to fire again on the next reply. | |
80 | """ |
|
80 | """ | |
81 | self._handlers_called = False |
|
81 | self._handlers_called = False | |
82 |
|
82 | |||
83 |
|
83 | |||
84 | class QtSubSocketChannel(SocketChannelQObject, SubSocketChannel): |
|
84 | class QtSubSocketChannel(SocketChannelQObject, SubSocketChannel): | |
85 |
|
85 | |||
86 | # Emitted when any message is received. |
|
86 | # Emitted when any message is received. | |
87 | message_received = QtCore.pyqtSignal(object) |
|
87 | message_received = QtCore.pyqtSignal(object) | |
88 |
|
88 | |||
89 | # Emitted when a message of type 'stream' is received. |
|
89 | # Emitted when a message of type 'stream' is received. | |
90 | stream_received = QtCore.pyqtSignal(object) |
|
90 | stream_received = QtCore.pyqtSignal(object) | |
91 |
|
91 | |||
92 | # Emitted when a message of type 'pyin' is received. |
|
92 | # Emitted when a message of type 'pyin' is received. | |
93 | pyin_received = QtCore.pyqtSignal(object) |
|
93 | pyin_received = QtCore.pyqtSignal(object) | |
94 |
|
94 | |||
95 | # Emitted when a message of type 'pyout' is received. |
|
95 | # Emitted when a message of type 'pyout' is received. | |
96 | pyout_received = QtCore.pyqtSignal(object) |
|
96 | pyout_received = QtCore.pyqtSignal(object) | |
97 |
|
97 | |||
98 | # Emitted when a message of type 'pyerr' is received. |
|
98 | # Emitted when a message of type 'pyerr' is received. | |
99 | pyerr_received = QtCore.pyqtSignal(object) |
|
99 | pyerr_received = QtCore.pyqtSignal(object) | |
100 |
|
100 | |||
101 | # Emitted when a crash report message is received from the kernel's |
|
101 | # Emitted when a crash report message is received from the kernel's | |
102 | # last-resort sys.excepthook. |
|
102 | # last-resort sys.excepthook. | |
103 | crash_received = QtCore.pyqtSignal(object) |
|
103 | crash_received = QtCore.pyqtSignal(object) | |
104 |
|
104 | |||
105 | #--------------------------------------------------------------------------- |
|
105 | #--------------------------------------------------------------------------- | |
106 | # 'SubSocketChannel' interface |
|
106 | # 'SubSocketChannel' interface | |
107 | #--------------------------------------------------------------------------- |
|
107 | #--------------------------------------------------------------------------- | |
108 |
|
108 | |||
109 | def call_handlers(self, msg): |
|
109 | def call_handlers(self, msg): | |
110 | """ Reimplemented to emit signals instead of making callbacks. |
|
110 | """ Reimplemented to emit signals instead of making callbacks. | |
111 | """ |
|
111 | """ | |
112 | # Emit the generic signal. |
|
112 | # Emit the generic signal. | |
113 | self.message_received.emit(msg) |
|
113 | self.message_received.emit(msg) | |
114 |
|
114 | |||
115 | # Emit signals for specialized message types. |
|
115 | # Emit signals for specialized message types. | |
116 | msg_type = msg['msg_type'] |
|
116 | msg_type = msg['msg_type'] | |
117 | signal = getattr(self, msg_type + '_received', None) |
|
117 | signal = getattr(self, msg_type + '_received', None) | |
118 | if signal: |
|
118 | if signal: | |
119 | signal.emit(msg) |
|
119 | signal.emit(msg) | |
120 | elif msg_type in ('stdout', 'stderr'): |
|
120 | elif msg_type in ('stdout', 'stderr'): | |
121 | self.stream_received.emit(msg) |
|
121 | self.stream_received.emit(msg) | |
122 |
|
122 | |||
123 | def flush(self): |
|
123 | def flush(self): | |
124 | """ Reimplemented to ensure that signals are dispatched immediately. |
|
124 | """ Reimplemented to ensure that signals are dispatched immediately. | |
125 | """ |
|
125 | """ | |
126 | super(QtSubSocketChannel, self).flush() |
|
126 | super(QtSubSocketChannel, self).flush() | |
127 | QtCore.QCoreApplication.instance().processEvents() |
|
127 | QtCore.QCoreApplication.instance().processEvents() | |
128 |
|
128 | |||
129 |
|
129 | |||
130 | class QtRepSocketChannel(SocketChannelQObject, RepSocketChannel): |
|
130 | class QtRepSocketChannel(SocketChannelQObject, RepSocketChannel): | |
131 |
|
131 | |||
132 | # Emitted when any message is received. |
|
132 | # Emitted when any message is received. | |
133 | message_received = QtCore.pyqtSignal(object) |
|
133 | message_received = QtCore.pyqtSignal(object) | |
134 |
|
134 | |||
135 | # Emitted when an input request is received. |
|
135 | # Emitted when an input request is received. | |
136 | input_requested = QtCore.pyqtSignal(object) |
|
136 | input_requested = QtCore.pyqtSignal(object) | |
137 |
|
137 | |||
138 | #--------------------------------------------------------------------------- |
|
138 | #--------------------------------------------------------------------------- | |
139 | # 'RepSocketChannel' interface |
|
139 | # 'RepSocketChannel' interface | |
140 | #--------------------------------------------------------------------------- |
|
140 | #--------------------------------------------------------------------------- | |
141 |
|
141 | |||
142 | def call_handlers(self, msg): |
|
142 | def call_handlers(self, msg): | |
143 | """ Reimplemented to emit signals instead of making callbacks. |
|
143 | """ Reimplemented to emit signals instead of making callbacks. | |
144 | """ |
|
144 | """ | |
145 | # Emit the generic signal. |
|
145 | # Emit the generic signal. | |
146 | self.message_received.emit(msg) |
|
146 | self.message_received.emit(msg) | |
147 |
|
147 | |||
148 | # Emit signals for specialized message types. |
|
148 | # Emit signals for specialized message types. | |
149 | msg_type = msg['msg_type'] |
|
149 | msg_type = msg['msg_type'] | |
150 | if msg_type == 'input_request': |
|
150 | if msg_type == 'input_request': | |
151 | self.input_requested.emit(msg) |
|
151 | self.input_requested.emit(msg) | |
152 |
|
152 | |||
153 |
|
153 | |||
154 | class QtHBSocketChannel(SocketChannelQObject, HBSocketChannel): |
|
154 | class QtHBSocketChannel(SocketChannelQObject, HBSocketChannel): | |
155 |
|
155 | |||
156 | # Emitted when the kernel has died. |
|
156 | # Emitted when the kernel has died. | |
157 | kernel_died = QtCore.pyqtSignal(object) |
|
157 | kernel_died = QtCore.pyqtSignal(object) | |
158 |
|
158 | |||
159 | #--------------------------------------------------------------------------- |
|
159 | #--------------------------------------------------------------------------- | |
160 | # 'HBSocketChannel' interface |
|
160 | # 'HBSocketChannel' interface | |
161 | #--------------------------------------------------------------------------- |
|
161 | #--------------------------------------------------------------------------- | |
162 |
|
162 | |||
163 | def call_handlers(self, since_last_heartbeat): |
|
163 | def call_handlers(self, since_last_heartbeat): | |
164 | """ Reimplemented to emit signals instead of making callbacks. |
|
164 | """ Reimplemented to emit signals instead of making callbacks. | |
165 | """ |
|
165 | """ | |
166 | # Emit the generic signal. |
|
166 | # Emit the generic signal. | |
167 | self.kernel_died.emit(since_last_heartbeat) |
|
167 | self.kernel_died.emit(since_last_heartbeat) | |
168 |
|
168 | |||
169 |
|
169 | |||
170 | class QtKernelManager(KernelManager, SuperQObject): |
|
170 | class QtKernelManager(KernelManager, SuperQObject): | |
171 | """ A KernelManager that provides signals and slots. |
|
171 | """ A KernelManager that provides signals and slots. | |
172 | """ |
|
172 | """ | |
173 |
|
173 | |||
174 | __metaclass__ = MetaQObjectHasTraits |
|
174 | __metaclass__ = MetaQObjectHasTraits | |
175 |
|
175 | |||
176 | # Emitted when the kernel manager has started listening. |
|
176 | # Emitted when the kernel manager has started listening. | |
177 | started_channels = QtCore.pyqtSignal() |
|
177 | started_channels = QtCore.pyqtSignal() | |
178 |
|
178 | |||
179 | # Emitted when the kernel manager has stopped listening. |
|
179 | # Emitted when the kernel manager has stopped listening. | |
180 | stopped_channels = QtCore.pyqtSignal() |
|
180 | stopped_channels = QtCore.pyqtSignal() | |
181 |
|
181 | |||
182 | # Use Qt-specific channel classes that emit signals. |
|
182 | # Use Qt-specific channel classes that emit signals. | |
183 | sub_channel_class = Type(QtSubSocketChannel) |
|
183 | sub_channel_class = Type(QtSubSocketChannel) | |
184 | xreq_channel_class = Type(QtXReqSocketChannel) |
|
184 | xreq_channel_class = Type(QtXReqSocketChannel) | |
185 | rep_channel_class = Type(QtRepSocketChannel) |
|
185 | rep_channel_class = Type(QtRepSocketChannel) | |
186 | hb_channel_class = Type(QtHBSocketChannel) |
|
186 | hb_channel_class = Type(QtHBSocketChannel) | |
187 |
|
187 | |||
188 | #--------------------------------------------------------------------------- |
|
188 | #--------------------------------------------------------------------------- | |
189 | # 'KernelManager' interface |
|
189 | # 'KernelManager' interface | |
190 | #--------------------------------------------------------------------------- |
|
190 | #--------------------------------------------------------------------------- | |
191 |
|
191 | |||
192 | def start_channels(self, *args, **kw): |
|
192 | def start_channels(self, *args, **kw): | |
193 | """ Reimplemented to emit signal. |
|
193 | """ Reimplemented to emit signal. | |
194 | """ |
|
194 | """ | |
195 | super(QtKernelManager, self).start_channels(*args, **kw) |
|
195 | super(QtKernelManager, self).start_channels(*args, **kw) | |
196 | self.started_channels.emit() |
|
196 | self.started_channels.emit() | |
197 |
|
197 | |||
198 | def stop_channels(self): |
|
198 | def stop_channels(self): | |
199 | """ Reimplemented to emit signal. |
|
199 | """ Reimplemented to emit signal. | |
200 | """ |
|
200 | """ | |
201 | super(QtKernelManager, self).stop_channels() |
|
201 | super(QtKernelManager, self).stop_channels() | |
202 | self.stopped_channels.emit() |
|
202 | self.stopped_channels.emit() |
General Comments 0
You need to be logged in to leave comments.
Login now