##// END OF EJS Templates
no longer need separate v4.compose...
no longer need separate v4.compose put it back in nbbase, and work around circular import another way

File last commit:

r18467:8414f38e
r18575:6dc1fae8
Show More
comm.py
162 lines | 5.4 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
Thomas Kluyver
Make comm_manager a property of kernel, not shell
r17987 from IPython.kernel.zmq.kernelbase import Kernel
MinRK
rename widget to comm
r13195
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):
Thomas Kluyver
Make comm manager (mostly) independent of InteractiveShell...
r17964 # If this is instantiated by a non-IPython kernel, shell will be None
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
allow_none=True)
kernel = Instance('IPython.kernel.zmq.kernelbase.Kernel')
Thomas Kluyver
Make comm_manager a property of kernel, not shell
r17987 def _kernel_default(self):
if Kernel.initialized():
return Kernel.instance()
MinRK
zmqshell has handle on Kernel
r13199
MinRK
rename widget to comm
r13195 iopub_socket = Any()
def _iopub_socket_default(self):
Thomas Kluyver
Make comm manager (mostly) independent of InteractiveShell...
r17964 return self.kernel.iopub_socket
MinRK
rename widget to comm
r13195 session = Instance('IPython.kernel.zmq.session.Session')
def _session_default(self):
Thomas Kluyver
Safeguard against comm instantiation with both session and kernel (corner case)
r18081 if self.kernel is not None:
return self.kernel.session
MinRK
rename widget to comm
r13195
MinRK
s/target/target_name
r13204 target_name = Unicode('comm')
Thomas Kluyver
Support specifying requirejs modules for comm targets
r18467 target_module = Unicode(None, allow_none=True, help="""requirejs module from
which to load comm target.""")
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()
Jason Grout
Handle some tricky Comm lifecycle issues...
r18454 _closed = Bool(True)
MinRK
rename widget to comm
r13195 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)
Jason Grout
Handle some tricky Comm lifecycle issues...
r18454 else:
self._closed = False
MinRK
rename widget to comm
r13195
MinRK
support buffers in comm messages...
r18329 def _publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys):
MinRK
rename widget to comm
r13195 """Helper for sending a comm message on IOPub"""
Thomas Kluyver
Make comm_manager a property of kernel, not shell
r17987 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.kernel._parent_header,
ident=self.topic,
MinRK
support buffers in comm messages...
r18329 buffers=buffers,
Thomas Kluyver
Make comm_manager a property of kernel, not shell
r17987 )
MinRK
rename widget to comm
r13195
def __del__(self):
"""trigger close on gc"""
self.close()
# publishing messages
MinRK
support buffers in comm messages...
r18329 def open(self, data=None, metadata=None, buffers=None):
MinRK
rename widget to comm
r13195 """Open the frontend-side version of this comm"""
if data is None:
data = self._open_data
Thomas Kluyver
Make comm_manager a property of kernel, not shell
r17987 comm_manager = getattr(self.kernel, 'comm_manager', None)
if comm_manager is None:
raise RuntimeError("Comms cannot be opened without a kernel "
"and a comm_manager attached to that kernel.")
comm_manager.register_comm(self)
Jason Grout
Unregister comm if there is an error in publishing the comm open message
r18456 try:
self._publish_msg('comm_open',
data=data, metadata=metadata, buffers=buffers,
Thomas Kluyver
Support specifying requirejs modules for comm targets
r18467 target_name=self.target_name,
target_module=self.target_module,
)
Jason Grout
Unregister comm if there is an error in publishing the comm open message
r18456 self._closed = False
except:
comm_manager.unregister_comm(self)
raise
MinRK
rename widget to comm
r13195
MinRK
support buffers in comm messages...
r18329 def close(self, data=None, metadata=None, buffers=None):
MinRK
rename widget to comm
r13195 """Close the frontend-side version of this comm"""
if self._closed:
# only close once
return
Jason Grout
Handle some tricky Comm lifecycle issues...
r18454 self._closed = True
MinRK
rename widget to comm
r13195 if data is None:
data = self._close_data
MinRK
support buffers in comm messages...
r18329 self._publish_msg('comm_close',
data=data, metadata=metadata, buffers=buffers,
)
Thomas Kluyver
Make comm_manager a property of kernel, not shell
r17987 self.kernel.comm_manager.unregister_comm(self)
MinRK
rename widget to comm
r13195
MinRK
support buffers in comm messages...
r18329 def send(self, data=None, metadata=None, buffers=None):
MinRK
pass whole message to Comm handlers
r13197 """Send a message to the frontend-side version of this comm"""
MinRK
support buffers in comm messages...
r18329 self._publish_msg('comm_msg',
data=data, metadata=metadata, buffers=buffers,
)
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
Make comm manager (mostly) independent of InteractiveShell...
r17964 if self.shell:
self.shell.events.trigger('pre_execute')
MinRK
pass whole message to Comm handlers
r13197 self._msg_callback(msg)
Thomas Kluyver
Make comm manager (mostly) independent of InteractiveShell...
r17964 if self.shell:
self.shell.events.trigger('post_execute')
MinRK
rename widget to comm
r13195
__all__ = ['Comm']