kernel_mixins.py
219 lines
| 7.2 KiB
| text/x-python
|
PythonLexer
MinRK
|
r16567 | """Defines a KernelManager that provides signals and slots.""" | ||
# Copyright (c) IPython Development Team. | ||||
# Distributed under the terms of the Modified BSD License. | ||||
epatters
|
r8408 | |||
from IPython.external.qt import QtCore | ||||
epatters
|
r8456 | from IPython.utils.traitlets import HasTraits, Type | ||
Thomas Kluyver
|
r13347 | from .util import MetaQObjectHasTraits, SuperQObject | ||
epatters
|
r8408 | |||
class ChannelQObject(SuperQObject): | ||||
# Emitted when the channel is started. | ||||
started = QtCore.Signal() | ||||
# Emitted when the channel is stopped. | ||||
stopped = QtCore.Signal() | ||||
#--------------------------------------------------------------------------- | ||||
# Channel interface | ||||
#--------------------------------------------------------------------------- | ||||
def start(self): | ||||
""" Reimplemented to emit signal. | ||||
""" | ||||
super(ChannelQObject, self).start() | ||||
self.started.emit() | ||||
def stop(self): | ||||
""" Reimplemented to emit signal. | ||||
""" | ||||
super(ChannelQObject, self).stop() | ||||
self.stopped.emit() | ||||
#--------------------------------------------------------------------------- | ||||
epatters
|
r8471 | # InProcessChannel interface | ||
epatters
|
r8408 | #--------------------------------------------------------------------------- | ||
def call_handlers_later(self, *args, **kwds): | ||||
""" Call the message handlers later. | ||||
""" | ||||
do_later = lambda: self.call_handlers(*args, **kwds) | ||||
QtCore.QTimer.singleShot(0, do_later) | ||||
def process_events(self): | ||||
""" Process any pending GUI events. | ||||
""" | ||||
QtCore.QCoreApplication.instance().processEvents() | ||||
class QtShellChannelMixin(ChannelQObject): | ||||
# Emitted when any message is received. | ||||
message_received = QtCore.Signal(object) | ||||
MinRK
|
r10288 | # Emitted when a reply has been received for the corresponding request type. | ||
epatters
|
r8408 | execute_reply = QtCore.Signal(object) | ||
complete_reply = QtCore.Signal(object) | ||||
MinRK
|
r16587 | inspect_reply = QtCore.Signal(object) | ||
epatters
|
r8408 | history_reply = QtCore.Signal(object) | ||
MinRK
|
r16697 | kernel_info_reply = QtCore.Signal(object) | ||
epatters
|
r8408 | |||
#--------------------------------------------------------------------------- | ||||
Brian Granger
|
r9120 | # 'ShellChannel' interface | ||
epatters
|
r8408 | #--------------------------------------------------------------------------- | ||
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'] | ||||
MinRK
|
r16697 | if msg_type == 'kernel_info_reply': | ||
self._handle_kernel_info_reply(msg) | ||||
epatters
|
r8408 | signal = getattr(self, msg_type, None) | ||
if signal: | ||||
signal.emit(msg) | ||||
Brian Granger
|
r9120 | class QtIOPubChannelMixin(ChannelQObject): | ||
epatters
|
r8408 | |||
# Emitted when any message is received. | ||||
message_received = QtCore.Signal(object) | ||||
# Emitted when a message of type 'stream' is received. | ||||
stream_received = QtCore.Signal(object) | ||||
MinRK
|
r16567 | # Emitted when a message of type 'execute_input' is received. | ||
execute_input_received = QtCore.Signal(object) | ||||
epatters
|
r8408 | |||
MinRK
|
r16568 | # Emitted when a message of type 'execute_result' is received. | ||
execute_result_received = QtCore.Signal(object) | ||||
epatters
|
r8408 | |||
MinRK
|
r16569 | # Emitted when a message of type 'error' is received. | ||
error_received = QtCore.Signal(object) | ||||
epatters
|
r8408 | |||
# 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) | ||||
#--------------------------------------------------------------------------- | ||||
Brian Granger
|
r9120 | # 'IOPubChannel' interface | ||
epatters
|
r8408 | #--------------------------------------------------------------------------- | ||
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. | ||||
""" | ||||
Brian Granger
|
r9120 | super(QtIOPubChannelMixin, self).flush() | ||
epatters
|
r8408 | QtCore.QCoreApplication.instance().processEvents() | ||
class QtStdInChannelMixin(ChannelQObject): | ||||
# Emitted when any message is received. | ||||
message_received = QtCore.Signal(object) | ||||
# Emitted when an input request is received. | ||||
input_requested = QtCore.Signal(object) | ||||
#--------------------------------------------------------------------------- | ||||
Brian Granger
|
r9120 | # 'StdInChannel' interface | ||
epatters
|
r8408 | #--------------------------------------------------------------------------- | ||
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) | ||||
class QtHBChannelMixin(ChannelQObject): | ||||
# Emitted when the kernel has died. | ||||
kernel_died = QtCore.Signal(object) | ||||
#--------------------------------------------------------------------------- | ||||
Brian Granger
|
r9120 | # 'HBChannel' interface | ||
epatters
|
r8408 | #--------------------------------------------------------------------------- | ||
def call_handlers(self, since_last_heartbeat): | ||||
""" Reimplemented to emit signals instead of making callbacks. | ||||
""" | ||||
# Emit the generic signal. | ||||
self.kernel_died.emit(since_last_heartbeat) | ||||
Thomas Kluyver
|
r13359 | class QtKernelRestarterMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): | ||
MinRK
|
r10308 | |||
_timer = None | ||||
Thomas Kluyver
|
r13359 | class QtKernelManagerMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): | ||
MinRK
|
r10308 | """ A KernelClient that provides signals and slots. | ||
""" | ||||
kernel_restarted = QtCore.Signal() | ||||
Thomas Kluyver
|
r13359 | class QtKernelClientMixin(MetaQObjectHasTraits('NewBase', (HasTraits, SuperQObject), {})): | ||
MinRK
|
r10288 | """ A KernelClient that provides signals and slots. | ||
epatters
|
r8408 | """ | ||
MinRK
|
r10288 | # Emitted when the kernel client has started listening. | ||
epatters
|
r8408 | started_channels = QtCore.Signal() | ||
MinRK
|
r10288 | # Emitted when the kernel client has stopped listening. | ||
epatters
|
r8408 | stopped_channels = QtCore.Signal() | ||
# Use Qt-specific channel classes that emit signals. | ||||
Brian Granger
|
r9120 | iopub_channel_class = Type(QtIOPubChannelMixin) | ||
epatters
|
r8408 | shell_channel_class = Type(QtShellChannelMixin) | ||
stdin_channel_class = Type(QtStdInChannelMixin) | ||||
hb_channel_class = Type(QtHBChannelMixin) | ||||
#--------------------------------------------------------------------------- | ||||
MinRK
|
r10288 | # 'KernelClient' interface | ||
epatters
|
r8408 | #--------------------------------------------------------------------------- | ||
#------ Channel management ------------------------------------------------- | ||||
def start_channels(self, *args, **kw): | ||||
""" Reimplemented to emit signal. | ||||
""" | ||||
MinRK
|
r10288 | super(QtKernelClientMixin, self).start_channels(*args, **kw) | ||
epatters
|
r8408 | self.started_channels.emit() | ||
def stop_channels(self): | ||||
""" Reimplemented to emit signal. | ||||
""" | ||||
MinRK
|
r10288 | super(QtKernelClientMixin, self).stop_channels() | ||
epatters
|
r8408 | self.stopped_channels.emit() | ||