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