##// END OF EJS Templates
Merge pull request #6370 from minrk/handle-no-checkpoint...
Merge pull request #6370 from minrk/handle-no-checkpoint fix handling of no-checkpoint-defined

File last commit:

r17614:1162e9e7
r17717:5b68572e merge
Show More
comm.py
140 lines | 4.5 KiB | text/x-python | PythonLexer
MinRK
rename widget to comm
r13195 """Base class for a Comm"""
MinRK
apply json_clean to comm_msg content...
r16623 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
MinRK
rename widget to comm
r13195
import uuid
from IPython.config import LoggingConfigurable
from IPython.core.getipython import get_ipython
MinRK
apply json_clean to comm_msg content...
r16623 from IPython.utils.jsonutil import json_clean
MinRK
rename widget to comm
r13195 from IPython.utils.traitlets import Instance, Unicode, Bytes, Bool, Dict, Any
class Comm(LoggingConfigurable):
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
def _shell_default(self):
return get_ipython()
MinRK
zmqshell has handle on Kernel
r13199
MinRK
rename widget to comm
r13195 iopub_socket = Any()
def _iopub_socket_default(self):
MinRK
zmqshell has handle on Kernel
r13199 return self.shell.kernel.iopub_socket
MinRK
rename widget to comm
r13195 session = Instance('IPython.kernel.zmq.session.Session')
def _session_default(self):
Jonathan Frederic
Check for null session in _publish_msg and _session)default
r17614 if self.shell is None or not hasattr(self.shell, 'kernel'):
MinRK
rename widget to comm
r13195 return
MinRK
zmqshell has handle on Kernel
r13199 return self.shell.kernel.session
MinRK
rename widget to comm
r13195
MinRK
s/target/target_name
r13204 target_name = Unicode('comm')
MinRK
rename widget to comm
r13195
topic = Bytes()
def _topic_default(self):
return ('comm-%s' % self.comm_id).encode('ascii')
MinRK
open is not an event...
r13205 _open_data = Dict(help="data dict, if any, to be included in comm_open")
MinRK
rename widget to comm
r13195 _close_data = Dict(help="data dict, if any, to be included in comm_close")
_msg_callback = Any()
_close_callback = Any()
_closed = Bool(False)
comm_id = Unicode()
def _comm_id_default(self):
return uuid.uuid4().hex
primary = Bool(True, help="Am I the primary or secondary Comm?")
MinRK
add target_name as first arg to Comm constructor
r13228 def __init__(self, target_name='', data=None, **kwargs):
if target_name:
kwargs['target_name'] = target_name
MinRK
rename widget to comm
r13195 super(Comm, self).__init__(**kwargs)
if self.primary:
MinRK
open is not an event...
r13205 # I am primary, open my peer.
MinRK
pass whole message to Comm handlers
r13197 self.open(data)
MinRK
rename widget to comm
r13195
MinRK
allow metadata in Kernel-side Comm messages.
r13224 def _publish_msg(self, msg_type, data=None, metadata=None, **keys):
MinRK
rename widget to comm
r13195 """Helper for sending a comm message on IOPub"""
Jonathan Frederic
Check for null session in _publish_msg and _session)default
r17614 if self.session is not None:
data = {} if data is None else data
metadata = {} if metadata is None else metadata
content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
self.session.send(self.iopub_socket, msg_type,
content,
metadata=json_clean(metadata),
parent=self.shell.get_parent(),
ident=self.topic,
)
MinRK
rename widget to comm
r13195
def __del__(self):
"""trigger close on gc"""
self.close()
# publishing messages
MinRK
allow metadata in Kernel-side Comm messages.
r13224 def open(self, data=None, metadata=None):
MinRK
rename widget to comm
r13195 """Open the frontend-side version of this comm"""
if data is None:
data = self._open_data
Sylvain Corlay
comm_manager.comms correspond to the open comms.
r17450 self._closed = False
U-Jon-PC\Jon
Don't register comm with comm_manager if comm_manager is undefined
r17613 ip = get_ipython()
if hasattr(ip, 'comm_manager'):
ip.comm_manager.register_comm(self)
MinRK
allow metadata in Kernel-side Comm messages.
r13224 self._publish_msg('comm_open', data, metadata, target_name=self.target_name)
MinRK
rename widget to comm
r13195
MinRK
allow metadata in Kernel-side Comm messages.
r13224 def close(self, data=None, metadata=None):
MinRK
rename widget to comm
r13195 """Close the frontend-side version of this comm"""
if self._closed:
# only close once
return
if data is None:
data = self._close_data
MinRK
allow metadata in Kernel-side Comm messages.
r13224 self._publish_msg('comm_close', data, metadata)
U-Jon-PC\Jon
Don't register comm with comm_manager if comm_manager is undefined
r17613 ip = get_ipython()
if hasattr(ip, 'comm_manager'):
ip.comm_manager.unregister_comm(self)
MinRK
rename widget to comm
r13195 self._closed = True
MinRK
allow metadata in Kernel-side Comm messages.
r13224 def send(self, data=None, metadata=None):
MinRK
pass whole message to Comm handlers
r13197 """Send a message to the frontend-side version of this comm"""
MinRK
allow metadata in Kernel-side Comm messages.
r13224 self._publish_msg('comm_msg', data, metadata)
MinRK
rename widget to comm
r13195
# registering callbacks
def on_close(self, callback):
"""Register a callback for comm_close
Will be called with the `data` of the close message.
Call `on_close(None)` to disable an existing callback.
"""
self._close_callback = callback
def on_msg(self, callback):
"""Register a callback for comm_msg
Will be called with the `data` of any comm_msg messages.
Call `on_msg(None)` to disable an existing callback.
"""
self._msg_callback = callback
# handling of incoming messages
MinRK
pass whole message to Comm handlers
r13197 def handle_close(self, msg):
MinRK
rename widget to comm
r13195 """Handle a comm_close message"""
MinRK
pass whole message to Comm handlers
r13197 self.log.debug("handle_close[%s](%s)", self.comm_id, msg)
MinRK
rename widget to comm
r13195 if self._close_callback:
MinRK
pass whole message to Comm handlers
r13197 self._close_callback(msg)
MinRK
rename widget to comm
r13195
MinRK
pass whole message to Comm handlers
r13197 def handle_msg(self, msg):
MinRK
rename widget to comm
r13195 """Handle a comm_msg message"""
MinRK
pass whole message to Comm handlers
r13197 self.log.debug("handle_msg[%s](%s)", self.comm_id, msg)
MinRK
rename widget to comm
r13195 if self._msg_callback:
Thomas Kluyver
Rename callbacks -> events (mostly), fire -> trigger
r15605 self.shell.events.trigger('pre_execute')
MinRK
pass whole message to Comm handlers
r13197 self._msg_callback(msg)
Thomas Kluyver
Rename callbacks -> events (mostly), fire -> trigger
r15605 self.shell.events.trigger('post_execute')
MinRK
rename widget to comm
r13195
__all__ = ['Comm']