##// END OF EJS Templates
Make comm manager (mostly) independent of InteractiveShell...
Thomas Kluyver -
Show More
@@ -14,18 +14,17 b' from IPython.utils.traitlets import Instance, Unicode, Bytes, Bool, Dict, Any'
14
14
15 class Comm(LoggingConfigurable):
15 class Comm(LoggingConfigurable):
16
16
17 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
17 # If this is instantiated by a non-IPython kernel, shell will be None
18 def _shell_default(self):
18 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
19 return get_ipython()
19 allow_none=True)
20 kernel = Instance('IPython.kernel.zmq.kernelbase.Kernel')
20
21
21 iopub_socket = Any()
22 iopub_socket = Any()
22 def _iopub_socket_default(self):
23 def _iopub_socket_default(self):
23 return self.shell.kernel.iopub_socket
24 return self.kernel.iopub_socket
24 session = Instance('IPython.kernel.zmq.session.Session')
25 session = Instance('IPython.kernel.zmq.session.Session')
25 def _session_default(self):
26 def _session_default(self):
26 if self.shell is None or not hasattr(self.shell, 'kernel'):
27 return self.kernel.session
27 return
28 return self.shell.kernel.session
29
28
30 target_name = Unicode('comm')
29 target_name = Unicode('comm')
31
30
@@ -63,7 +62,7 b' class Comm(LoggingConfigurable):'
63 self.session.send(self.iopub_socket, msg_type,
62 self.session.send(self.iopub_socket, msg_type,
64 content,
63 content,
65 metadata=json_clean(metadata),
64 metadata=json_clean(metadata),
66 parent=self.shell.get_parent(),
65 parent=self.kernel._parent_header,
67 ident=self.topic,
66 ident=self.topic,
68 )
67 )
69
68
@@ -132,8 +131,10 b' class Comm(LoggingConfigurable):'
132 """Handle a comm_msg message"""
131 """Handle a comm_msg message"""
133 self.log.debug("handle_msg[%s](%s)", self.comm_id, msg)
132 self.log.debug("handle_msg[%s](%s)", self.comm_id, msg)
134 if self._msg_callback:
133 if self._msg_callback:
134 if self.shell:
135 self.shell.events.trigger('pre_execute')
135 self.shell.events.trigger('pre_execute')
136 self._msg_callback(msg)
136 self._msg_callback(msg)
137 if self.shell:
137 self.shell.events.trigger('post_execute')
138 self.shell.events.trigger('post_execute')
138
139
139
140
@@ -28,17 +28,17 b' def lazy_keys(dikt):'
28 class CommManager(LoggingConfigurable):
28 class CommManager(LoggingConfigurable):
29 """Manager for Comms in the Kernel"""
29 """Manager for Comms in the Kernel"""
30
30
31 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
31 # If this is instantiated by a non-IPython kernel, shell will be None
32 def _shell_default(self):
32 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
33 return get_ipython()
33 allow_none=True)
34 kernel = Instance('IPython.kernel.zmq.kernelbase.Kernel')
35
34 iopub_socket = Any()
36 iopub_socket = Any()
35 def _iopub_socket_default(self):
37 def _iopub_socket_default(self):
36 return self.shell.kernel.iopub_socket
38 return self.kernel.iopub_socket
37 session = Instance('IPython.kernel.zmq.session.Session')
39 session = Instance('IPython.kernel.zmq.session.Session')
38 def _session_default(self):
40 def _session_default(self):
39 if self.shell is None:
41 return self.kernel.session
40 return
41 return self.shell.kernel.session
42
42
43 comms = Dict()
43 comms = Dict()
44 targets = Dict()
44 targets = Dict()
@@ -68,6 +68,7 b' class CommManager(LoggingConfigurable):'
68 """Register a new comm"""
68 """Register a new comm"""
69 comm_id = comm.comm_id
69 comm_id = comm.comm_id
70 comm.shell = self.shell
70 comm.shell = self.shell
71 comm.kernel = self.kernel
71 comm.iopub_socket = self.iopub_socket
72 comm.iopub_socket = self.iopub_socket
72 self.comms[comm_id] = comm
73 self.comms[comm_id] = comm
73 return comm_id
74 return comm_id
@@ -102,6 +103,7 b' class CommManager(LoggingConfigurable):'
102 f = self.targets.get(target_name, None)
103 f = self.targets.get(target_name, None)
103 comm = Comm(comm_id=comm_id,
104 comm = Comm(comm_id=comm_id,
104 shell=self.shell,
105 shell=self.shell,
106 kernel=self.kernel,
105 iopub_socket=self.iopub_socket,
107 iopub_socket=self.iopub_socket,
106 primary=False,
108 primary=False,
107 )
109 )
@@ -565,7 +565,8 b' class ZMQInteractiveShell(InteractiveShell):'
565 self.magics_manager.register_alias('ed', 'edit')
565 self.magics_manager.register_alias('ed', 'edit')
566
566
567 def init_comms(self):
567 def init_comms(self):
568 self.comm_manager = CommManager(shell=self, parent=self)
568 self.comm_manager = CommManager(shell=self, parent=self,
569 kernel=self.kernel)
569 self.configurables.append(self.comm_manager)
570 self.configurables.append(self.comm_manager)
570
571
571
572
General Comments 0
You need to be logged in to leave comments. Login now