inprocess.py
115 lines
| 4.1 KiB
| text/x-python
|
PythonLexer
|
r8471 | """ Defines an in-process KernelManager with signals and slots. | ||
""" | ||||
# Local imports. | ||||
|
r19210 | from IPython.external.qt import QtCore | ||
|
r10298 | from IPython.kernel.inprocess import ( | ||
InProcessShellChannel, InProcessIOPubChannel, InProcessStdInChannel, | ||||
|
r10333 | InProcessHBChannel, InProcessKernelClient, InProcessKernelManager, | ||
|
r10298 | ) | ||
|
r8471 | from IPython.utils.traitlets import Type | ||
|
r19210 | from .kernel_mixins import ( ChannelQObject, | ||
QtHBChannelMixin, QtKernelClientMixin, | ||||
|
r10333 | QtKernelManagerMixin, | ||
) | ||||
|
r8471 | |||
|
r19210 | class QtInProcessShellChannel(ChannelQObject, InProcessShellChannel): | ||
# Emitted when a reply has been received for the corresponding request type. | ||||
execute_reply = QtCore.Signal(object) | ||||
complete_reply = QtCore.Signal(object) | ||||
inspect_reply = QtCore.Signal(object) | ||||
history_reply = QtCore.Signal(object) | ||||
kernel_info_reply = QtCore.Signal(object) | ||||
|
r8471 | |||
|
r19210 | def call_handlers(self, msg): | ||
""" Reimplemented to emit signals instead of making callbacks. | ||||
""" | ||||
# Emit the generic signal. | ||||
self.message_received.emit(msg) | ||||
|
r8471 | |||
|
r19210 | # Emit signals for specialized message types. | ||
msg_type = msg['header']['msg_type'] | ||||
if msg_type == 'kernel_info_reply': | ||||
self._handle_kernel_info_reply(msg) | ||||
signal = getattr(self, msg_type, None) | ||||
if signal: | ||||
signal.emit(msg) | ||||
class QtInProcessIOPubChannel(ChannelQObject, InProcessIOPubChannel): | ||||
# Emitted when a message of type 'stream' is received. | ||||
stream_received = QtCore.Signal(object) | ||||
# Emitted when a message of type 'execute_input' is received. | ||||
execute_input_received = QtCore.Signal(object) | ||||
# Emitted when a message of type 'execute_result' is received. | ||||
execute_result_received = QtCore.Signal(object) | ||||
# Emitted when a message of type 'error' is received. | ||||
error_received = QtCore.Signal(object) | ||||
# Emitted when a message of type 'display_data' is received | ||||
display_data_received = QtCore.Signal(object) | ||||
# Emitted when a crash report message is received from the kernel's | ||||
# last-resort sys.excepthook. | ||||
crash_received = QtCore.Signal(object) | ||||
# Emitted when a shutdown is noticed. | ||||
shutdown_reply_received = QtCore.Signal(object) | ||||
#--------------------------------------------------------------------------- | ||||
# 'IOPubChannel' interface | ||||
#--------------------------------------------------------------------------- | ||||
def call_handlers(self, msg): | ||||
""" Reimplemented to emit signals instead of making callbacks. | ||||
""" | ||||
# Emit the generic signal. | ||||
self.message_received.emit(msg) | ||||
# Emit signals for specialized message types. | ||||
msg_type = msg['header']['msg_type'] | ||||
signal = getattr(self, msg_type + '_received', None) | ||||
if signal: | ||||
signal.emit(msg) | ||||
elif msg_type in ('stdout', 'stderr'): | ||||
self.stream_received.emit(msg) | ||||
def flush(self): | ||||
""" Reimplemented to ensure that signals are dispatched immediately. | ||||
""" | ||||
super(QtInProcessIOPubChannel, self).flush() | ||||
QtCore.QCoreApplication.instance().processEvents() | ||||
class QtInProcessStdInChannel(ChannelQObject, InProcessStdInChannel): | ||||
# Emitted when an input request is received. | ||||
input_requested = QtCore.Signal(object) | ||||
def call_handlers(self, msg): | ||||
""" Reimplemented to emit signals instead of making callbacks. | ||||
""" | ||||
# Emit the generic signal. | ||||
self.message_received.emit(msg) | ||||
# Emit signals for specialized message types. | ||||
msg_type = msg['header']['msg_type'] | ||||
if msg_type == 'input_request': | ||||
self.input_requested.emit(msg) | ||||
|
r8471 | |||
|
r9120 | class QtInProcessHBChannel(QtHBChannelMixin, InProcessHBChannel): | ||
|
r8471 | pass | ||
|
r10298 | class QtInProcessKernelClient(QtKernelClientMixin, InProcessKernelClient): | ||
|
r8471 | """ An in-process KernelManager with signals and slots. | ||
""" | ||||
|
r9120 | iopub_channel_class = Type(QtInProcessIOPubChannel) | ||
shell_channel_class = Type(QtInProcessShellChannel) | ||||
stdin_channel_class = Type(QtInProcessStdInChannel) | ||||
hb_channel_class = Type(QtInProcessHBChannel) | ||||
|
r10333 | |||
class QtInProcessKernelManager(QtKernelManagerMixin, InProcessKernelManager): | ||||
client_class = __module__ + '.QtInProcessKernelClient' | ||||