Show More
@@ -1,217 +1,196 | |||||
1 | """Defines a KernelManager that provides signals and slots.""" |
|
1 | """Defines a KernelManager that provides signals and slots.""" | |
2 |
|
2 | |||
3 | # Copyright (c) IPython Development Team. |
|
3 | # Copyright (c) IPython Development Team. | |
4 | # Distributed under the terms of the Modified BSD License. |
|
4 | # Distributed under the terms of the Modified BSD License. | |
5 |
|
5 | |||
6 | from IPython.external.qt import QtCore |
|
6 | from IPython.external.qt import QtCore | |
7 |
|
7 | |||
8 | from IPython.utils.traitlets import HasTraits, Type |
|
8 | from IPython.utils.traitlets import HasTraits, Type | |
9 | from .util import MetaQObjectHasTraits, SuperQObject |
|
9 | from .util import MetaQObjectHasTraits, SuperQObject | |
10 |
|
10 | |||
11 |
|
11 | |||
12 | class ChannelQObject(SuperQObject): |
|
12 | class ChannelQObject(SuperQObject): | |
13 |
|
13 | |||
14 | # Emitted when the channel is started. |
|
14 | # Emitted when the channel is started. | |
15 | started = QtCore.Signal() |
|
15 | started = QtCore.Signal() | |
16 |
|
16 | |||
17 | # Emitted when the channel is stopped. |
|
17 | # Emitted when the channel is stopped. | |
18 | stopped = QtCore.Signal() |
|
18 | stopped = QtCore.Signal() | |
19 |
|
19 | |||
20 | #--------------------------------------------------------------------------- |
|
|||
21 | # Channel interface |
|
|||
22 | #--------------------------------------------------------------------------- |
|
|||
23 |
|
||||
24 | def start(self): |
|
20 | def start(self): | |
25 | """ Reimplemented to emit signal. |
|
21 | """ Reimplemented to emit signal. | |
26 | """ |
|
22 | """ | |
27 | super(ChannelQObject, self).start() |
|
23 | super(ChannelQObject, self).start() | |
28 | self.started.emit() |
|
24 | self.started.emit() | |
29 |
|
25 | |||
30 | def stop(self): |
|
26 | def stop(self): | |
31 | """ Reimplemented to emit signal. |
|
27 | """ Reimplemented to emit signal. | |
32 | """ |
|
28 | """ | |
33 | super(ChannelQObject, self).stop() |
|
29 | super(ChannelQObject, self).stop() | |
34 | self.stopped.emit() |
|
30 | self.stopped.emit() | |
35 |
|
31 | |||
36 | #--------------------------------------------------------------------------- |
|
32 | #--------------------------------------------------------------------------- | |
37 | # InProcessChannel interface |
|
33 | # InProcessChannel interface | |
38 | #--------------------------------------------------------------------------- |
|
34 | #--------------------------------------------------------------------------- | |
39 |
|
35 | |||
40 | def call_handlers_later(self, *args, **kwds): |
|
36 | def call_handlers_later(self, *args, **kwds): | |
41 | """ Call the message handlers later. |
|
37 | """ Call the message handlers later. | |
42 | """ |
|
38 | """ | |
43 | do_later = lambda: self.call_handlers(*args, **kwds) |
|
39 | do_later = lambda: self.call_handlers(*args, **kwds) | |
44 | QtCore.QTimer.singleShot(0, do_later) |
|
40 | QtCore.QTimer.singleShot(0, do_later) | |
45 |
|
41 | |||
46 | def process_events(self): |
|
42 | def process_events(self): | |
47 | """ Process any pending GUI events. |
|
43 | """ Process any pending GUI events. | |
48 | """ |
|
44 | """ | |
49 | QtCore.QCoreApplication.instance().processEvents() |
|
45 | QtCore.QCoreApplication.instance().processEvents() | |
50 |
|
46 | |||
51 |
|
47 | |||
52 | class QtShellChannelMixin(ChannelQObject): |
|
48 | class QtShellChannelMixin(ChannelQObject): | |
53 |
|
49 | |||
54 | # Emitted when any message is received. |
|
50 | # Emitted when any message is received. | |
55 | message_received = QtCore.Signal(object) |
|
51 | message_received = QtCore.Signal(object) | |
56 |
|
52 | |||
57 | # Emitted when a reply has been received for the corresponding request type. |
|
53 | # Emitted when a reply has been received for the corresponding request type. | |
58 | execute_reply = QtCore.Signal(object) |
|
54 | execute_reply = QtCore.Signal(object) | |
59 | complete_reply = QtCore.Signal(object) |
|
55 | complete_reply = QtCore.Signal(object) | |
60 | inspect_reply = QtCore.Signal(object) |
|
56 | inspect_reply = QtCore.Signal(object) | |
61 | history_reply = QtCore.Signal(object) |
|
57 | history_reply = QtCore.Signal(object) | |
62 | kernel_info_reply = QtCore.Signal(object) |
|
58 | kernel_info_reply = QtCore.Signal(object) | |
63 |
|
59 | |||
64 | #--------------------------------------------------------------------------- |
|
|||
65 | # 'ShellChannel' interface |
|
|||
66 | #--------------------------------------------------------------------------- |
|
|||
67 |
|
||||
68 | def call_handlers(self, msg): |
|
60 | def call_handlers(self, msg): | |
69 | """ Reimplemented to emit signals instead of making callbacks. |
|
61 | """ Reimplemented to emit signals instead of making callbacks. | |
70 | """ |
|
62 | """ | |
71 | # Emit the generic signal. |
|
63 | # Emit the generic signal. | |
72 | self.message_received.emit(msg) |
|
64 | self.message_received.emit(msg) | |
73 |
|
65 | |||
74 | # Emit signals for specialized message types. |
|
66 | # Emit signals for specialized message types. | |
75 | msg_type = msg['header']['msg_type'] |
|
67 | msg_type = msg['header']['msg_type'] | |
76 | if msg_type == 'kernel_info_reply': |
|
68 | if msg_type == 'kernel_info_reply': | |
77 | self._handle_kernel_info_reply(msg) |
|
69 | self._handle_kernel_info_reply(msg) | |
78 |
|
70 | |||
79 | signal = getattr(self, msg_type, None) |
|
71 | signal = getattr(self, msg_type, None) | |
80 | if signal: |
|
72 | if signal: | |
81 | signal.emit(msg) |
|
73 | signal.emit(msg) | |
82 |
|
74 | |||
83 |
|
75 | |||
84 | class QtIOPubChannelMixin(ChannelQObject): |
|
76 | class QtIOPubChannelMixin(ChannelQObject): | |
85 |
|
77 | |||
86 | # Emitted when any message is received. |
|
78 | # Emitted when any message is received. | |
87 | message_received = QtCore.Signal(object) |
|
79 | message_received = QtCore.Signal(object) | |
88 |
|
80 | |||
89 | # Emitted when a message of type 'stream' is received. |
|
81 | # Emitted when a message of type 'stream' is received. | |
90 | stream_received = QtCore.Signal(object) |
|
82 | stream_received = QtCore.Signal(object) | |
91 |
|
83 | |||
92 | # Emitted when a message of type 'execute_input' is received. |
|
84 | # Emitted when a message of type 'execute_input' is received. | |
93 | execute_input_received = QtCore.Signal(object) |
|
85 | execute_input_received = QtCore.Signal(object) | |
94 |
|
86 | |||
95 | # Emitted when a message of type 'execute_result' is received. |
|
87 | # Emitted when a message of type 'execute_result' is received. | |
96 | execute_result_received = QtCore.Signal(object) |
|
88 | execute_result_received = QtCore.Signal(object) | |
97 |
|
89 | |||
98 | # Emitted when a message of type 'error' is received. |
|
90 | # Emitted when a message of type 'error' is received. | |
99 | error_received = QtCore.Signal(object) |
|
91 | error_received = QtCore.Signal(object) | |
100 |
|
92 | |||
101 | # Emitted when a message of type 'display_data' is received |
|
93 | # Emitted when a message of type 'display_data' is received | |
102 | display_data_received = QtCore.Signal(object) |
|
94 | display_data_received = QtCore.Signal(object) | |
103 |
|
95 | |||
104 | # Emitted when a crash report message is received from the kernel's |
|
96 | # Emitted when a crash report message is received from the kernel's | |
105 | # last-resort sys.excepthook. |
|
97 | # last-resort sys.excepthook. | |
106 | crash_received = QtCore.Signal(object) |
|
98 | crash_received = QtCore.Signal(object) | |
107 |
|
99 | |||
108 | # Emitted when a shutdown is noticed. |
|
100 | # Emitted when a shutdown is noticed. | |
109 | shutdown_reply_received = QtCore.Signal(object) |
|
101 | shutdown_reply_received = QtCore.Signal(object) | |
110 |
|
102 | |||
111 | #--------------------------------------------------------------------------- |
|
|||
112 | # 'IOPubChannel' interface |
|
|||
113 | #--------------------------------------------------------------------------- |
|
|||
114 |
|
||||
115 | def call_handlers(self, msg): |
|
103 | def call_handlers(self, msg): | |
116 | """ Reimplemented to emit signals instead of making callbacks. |
|
104 | """ Reimplemented to emit signals instead of making callbacks. | |
117 | """ |
|
105 | """ | |
118 | # Emit the generic signal. |
|
106 | # Emit the generic signal. | |
119 | self.message_received.emit(msg) |
|
107 | self.message_received.emit(msg) | |
120 | # Emit signals for specialized message types. |
|
108 | # Emit signals for specialized message types. | |
121 | msg_type = msg['header']['msg_type'] |
|
109 | msg_type = msg['header']['msg_type'] | |
122 | signal = getattr(self, msg_type + '_received', None) |
|
110 | signal = getattr(self, msg_type + '_received', None) | |
123 | if signal: |
|
111 | if signal: | |
124 | signal.emit(msg) |
|
112 | signal.emit(msg) | |
125 |
|
113 | |||
126 | def flush(self): |
|
114 | def flush(self): | |
127 | """ Reimplemented to ensure that signals are dispatched immediately. |
|
115 | """ Reimplemented to ensure that signals are dispatched immediately. | |
128 | """ |
|
116 | """ | |
129 | super(QtIOPubChannelMixin, self).flush() |
|
117 | super(QtIOPubChannelMixin, self).flush() | |
130 | QtCore.QCoreApplication.instance().processEvents() |
|
118 | QtCore.QCoreApplication.instance().processEvents() | |
131 |
|
119 | |||
132 |
|
120 | |||
133 | class QtStdInChannelMixin(ChannelQObject): |
|
121 | class QtStdInChannelMixin(ChannelQObject): | |
134 |
|
122 | |||
135 | # Emitted when any message is received. |
|
123 | # Emitted when any message is received. | |
136 | message_received = QtCore.Signal(object) |
|
124 | message_received = QtCore.Signal(object) | |
137 |
|
125 | |||
138 | # Emitted when an input request is received. |
|
126 | # Emitted when an input request is received. | |
139 | input_requested = QtCore.Signal(object) |
|
127 | input_requested = QtCore.Signal(object) | |
140 |
|
128 | |||
141 | #--------------------------------------------------------------------------- |
|
|||
142 | # 'StdInChannel' interface |
|
|||
143 | #--------------------------------------------------------------------------- |
|
|||
144 |
|
||||
145 | def call_handlers(self, msg): |
|
129 | def call_handlers(self, msg): | |
146 | """ Reimplemented to emit signals instead of making callbacks. |
|
130 | """ Reimplemented to emit signals instead of making callbacks. | |
147 | """ |
|
131 | """ | |
148 | # Emit the generic signal. |
|
132 | # Emit the generic signal. | |
149 | self.message_received.emit(msg) |
|
133 | self.message_received.emit(msg) | |
150 |
|
134 | |||
151 | # Emit signals for specialized message types. |
|
135 | # Emit signals for specialized message types. | |
152 | msg_type = msg['header']['msg_type'] |
|
136 | msg_type = msg['header']['msg_type'] | |
153 | if msg_type == 'input_request': |
|
137 | if msg_type == 'input_request': | |
154 | self.input_requested.emit(msg) |
|
138 | self.input_requested.emit(msg) | |
155 |
|
139 | |||
156 |
|
140 | |||
157 | class QtHBChannelMixin(ChannelQObject): |
|
141 | class QtHBChannelMixin(ChannelQObject): | |
158 |
|
142 | |||
159 | # Emitted when the kernel has died. |
|
143 | # Emitted when the kernel has died. | |
160 | kernel_died = QtCore.Signal(object) |
|
144 | kernel_died = QtCore.Signal(object) | |
161 |
|
145 | |||
162 | #--------------------------------------------------------------------------- |
|
|||
163 | # 'HBChannel' interface |
|
|||
164 | #--------------------------------------------------------------------------- |
|
|||
165 |
|
||||
166 | def call_handlers(self, since_last_heartbeat): |
|
146 | def call_handlers(self, since_last_heartbeat): | |
167 | """ Reimplemented to emit signals instead of making callbacks. |
|
147 | """ Reimplemented to emit signals instead of making callbacks. | |
168 | """ |
|
148 | """ | |
169 | # Emit the generic signal. |
|
|||
170 | self.kernel_died.emit(since_last_heartbeat) |
|
149 | self.kernel_died.emit(since_last_heartbeat) | |
171 |
|
150 | |||
172 |
|
151 | |||
173 | class QtKernelRestarterMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): |
|
152 | class QtKernelRestarterMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): | |
174 |
|
153 | |||
175 | _timer = None |
|
154 | _timer = None | |
176 |
|
155 | |||
177 |
|
156 | |||
178 | class QtKernelManagerMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): |
|
157 | class QtKernelManagerMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): | |
179 | """ A KernelClient that provides signals and slots. |
|
158 | """ A KernelClient that provides signals and slots. | |
180 | """ |
|
159 | """ | |
181 |
|
160 | |||
182 | kernel_restarted = QtCore.Signal() |
|
161 | kernel_restarted = QtCore.Signal() | |
183 |
|
162 | |||
184 |
|
163 | |||
185 | class QtKernelClientMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): |
|
164 | class QtKernelClientMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): | |
186 | """ A KernelClient that provides signals and slots. |
|
165 | """ A KernelClient that provides signals and slots. | |
187 | """ |
|
166 | """ | |
188 |
|
167 | |||
189 | # Emitted when the kernel client has started listening. |
|
168 | # Emitted when the kernel client has started listening. | |
190 | started_channels = QtCore.Signal() |
|
169 | started_channels = QtCore.Signal() | |
191 |
|
170 | |||
192 | # Emitted when the kernel client has stopped listening. |
|
171 | # Emitted when the kernel client has stopped listening. | |
193 | stopped_channels = QtCore.Signal() |
|
172 | stopped_channels = QtCore.Signal() | |
194 |
|
173 | |||
195 | # Use Qt-specific channel classes that emit signals. |
|
174 | # Use Qt-specific channel classes that emit signals. | |
196 | iopub_channel_class = Type(QtIOPubChannelMixin) |
|
175 | iopub_channel_class = Type(QtIOPubChannelMixin) | |
197 | shell_channel_class = Type(QtShellChannelMixin) |
|
176 | shell_channel_class = Type(QtShellChannelMixin) | |
198 | stdin_channel_class = Type(QtStdInChannelMixin) |
|
177 | stdin_channel_class = Type(QtStdInChannelMixin) | |
199 | hb_channel_class = Type(QtHBChannelMixin) |
|
178 | hb_channel_class = Type(QtHBChannelMixin) | |
200 |
|
179 | |||
201 | #--------------------------------------------------------------------------- |
|
180 | #--------------------------------------------------------------------------- | |
202 | # 'KernelClient' interface |
|
181 | # 'KernelClient' interface | |
203 | #--------------------------------------------------------------------------- |
|
182 | #--------------------------------------------------------------------------- | |
204 |
|
183 | |||
205 | #------ Channel management ------------------------------------------------- |
|
184 | #------ Channel management ------------------------------------------------- | |
206 |
|
185 | |||
207 | def start_channels(self, *args, **kw): |
|
186 | def start_channels(self, *args, **kw): | |
208 | """ Reimplemented to emit signal. |
|
187 | """ Reimplemented to emit signal. | |
209 | """ |
|
188 | """ | |
210 | super(QtKernelClientMixin, self).start_channels(*args, **kw) |
|
189 | super(QtKernelClientMixin, self).start_channels(*args, **kw) | |
211 | self.started_channels.emit() |
|
190 | self.started_channels.emit() | |
212 |
|
191 | |||
213 | def stop_channels(self): |
|
192 | def stop_channels(self): | |
214 | """ Reimplemented to emit signal. |
|
193 | """ Reimplemented to emit signal. | |
215 | """ |
|
194 | """ | |
216 | super(QtKernelClientMixin, self).stop_channels() |
|
195 | super(QtKernelClientMixin, self).stop_channels() | |
217 | self.stopped_channels.emit() |
|
196 | self.stopped_channels.emit() |
General Comments 0
You need to be logged in to leave comments.
Login now