##// END OF EJS Templates
set parent header properly in comm messages
MinRK -
Show More
@@ -1,135 +1,136 b''
1 1 """Base class for a Comm"""
2 2
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (C) 2013 The IPython Development Team
5 5 #
6 6 # Distributed under the terms of the BSD License. The full license is in
7 7 # the file COPYING, distributed as part of this software.
8 8 #-----------------------------------------------------------------------------
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Imports
12 12 #-----------------------------------------------------------------------------
13 13
14 14 import uuid
15 15
16 16 from IPython.config import LoggingConfigurable
17 17 from IPython.core.getipython import get_ipython
18 18
19 19 from IPython.utils.traitlets import Instance, Unicode, Bytes, Bool, Dict, Any
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Code
23 23 #-----------------------------------------------------------------------------
24 24
25 25 class Comm(LoggingConfigurable):
26 26
27 27 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
28 28 def _shell_default(self):
29 29 return get_ipython()
30 30
31 31 iopub_socket = Any()
32 32 def _iopub_socket_default(self):
33 33 return self.shell.kernel.iopub_socket
34 34 session = Instance('IPython.kernel.zmq.session.Session')
35 35 def _session_default(self):
36 36 if self.shell is None:
37 37 return
38 38 return self.shell.kernel.session
39 39
40 40 target_name = Unicode('comm')
41 41
42 42 topic = Bytes()
43 43 def _topic_default(self):
44 44 return ('comm-%s' % self.comm_id).encode('ascii')
45 45
46 46 _open_data = Dict(help="data dict, if any, to be included in comm_open")
47 47 _close_data = Dict(help="data dict, if any, to be included in comm_close")
48 48
49 49 _msg_callback = Any()
50 50 _close_callback = Any()
51 51
52 52 _closed = Bool(False)
53 53 comm_id = Unicode()
54 54 def _comm_id_default(self):
55 55 return uuid.uuid4().hex
56 56
57 57 primary = Bool(True, help="Am I the primary or secondary Comm?")
58 58
59 59 def __init__(self, data=None, **kwargs):
60 60 super(Comm, self).__init__(**kwargs)
61 61 get_ipython().comm_manager.register_comm(self)
62 62 if self.primary:
63 63 # I am primary, open my peer.
64 64 self.open(data)
65 65
66 66 def _publish_msg(self, msg_type, data=None, **keys):
67 67 """Helper for sending a comm message on IOPub"""
68 68 data = {} if data is None else data
69 69 self.session.send(self.iopub_socket, msg_type,
70 70 dict(data=data, comm_id=self.comm_id, **keys),
71 parent=self.shell.get_parent(),
71 72 ident=self.topic,
72 73 )
73 74
74 75 def __del__(self):
75 76 """trigger close on gc"""
76 77 self.close()
77 78
78 79 # publishing messages
79 80
80 81 def open(self, data=None):
81 82 """Open the frontend-side version of this comm"""
82 83 if data is None:
83 84 data = self._open_data
84 85 self._publish_msg('comm_open', data, target_name=self.target_name)
85 86
86 87 def close(self, data=None):
87 88 """Close the frontend-side version of this comm"""
88 89 if self._closed:
89 90 # only close once
90 91 return
91 92 if data is None:
92 93 data = self._close_data
93 94 self._publish_msg('comm_close', data)
94 95 self._closed = True
95 96
96 97 def send(self, data=None):
97 98 """Send a message to the frontend-side version of this comm"""
98 99 self._publish_msg('comm_msg', data)
99 100
100 101 # registering callbacks
101 102
102 103 def on_close(self, callback):
103 104 """Register a callback for comm_close
104 105
105 106 Will be called with the `data` of the close message.
106 107
107 108 Call `on_close(None)` to disable an existing callback.
108 109 """
109 110 self._close_callback = callback
110 111
111 112 def on_msg(self, callback):
112 113 """Register a callback for comm_msg
113 114
114 115 Will be called with the `data` of any comm_msg messages.
115 116
116 117 Call `on_msg(None)` to disable an existing callback.
117 118 """
118 119 self._msg_callback = callback
119 120
120 121 # handling of incoming messages
121 122
122 123 def handle_close(self, msg):
123 124 """Handle a comm_close message"""
124 125 self.log.debug("handle_close[%s](%s)", self.comm_id, msg)
125 126 if self._close_callback:
126 127 self._close_callback(msg)
127 128
128 129 def handle_msg(self, msg):
129 130 """Handle a comm_msg message"""
130 131 self.log.debug("handle_msg[%s](%s)", self.comm_id, msg)
131 132 if self._msg_callback:
132 133 self._msg_callback(msg)
133 134
134 135
135 136 __all__ = ['Comm']
General Comments 0
You need to be logged in to leave comments. Login now