##// END OF EJS Templates
s/destroy/close
MinRK -
Show More
@@ -1,142 +1,142 b''
1 1 """Base class to manage comms"""
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 from IPython.config import LoggingConfigurable
15 15 from IPython.core.prompts import LazyEvaluate
16 16 from IPython.core.getipython import get_ipython
17 17
18 18 from IPython.utils.importstring import import_item
19 19 from IPython.utils.traitlets import Instance, Unicode, Dict, Any
20 20
21 21 from .comm import Comm
22 22
23 23 #-----------------------------------------------------------------------------
24 24 # Code
25 25 #-----------------------------------------------------------------------------
26 26
27 27 def lazy_keys(dikt):
28 28 """Return lazy-evaluated string representation of a dictionary's keys
29 29
30 30 Key list is only constructed if it will actually be used.
31 31 Used for debug-logging.
32 32 """
33 33 return LazyEvaluate(lambda d: list(d.keys()))
34 34
35 35
36 36 class CommManager(LoggingConfigurable):
37 37 """Manager for Comms in the Kernel"""
38 38
39 39 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
40 40 def _shell_default(self):
41 41 return get_ipython()
42 42 iopub_socket = Any()
43 43 def _iopub_socket_default(self):
44 44 return self.shell.parent.iopub_socket
45 45 session = Instance('IPython.kernel.zmq.session.Session')
46 46 def _session_default(self):
47 47 if self.shell is None:
48 48 return
49 49 return self.shell.parent.session
50 50
51 51 comms = Dict()
52 52 targets = Dict()
53 53
54 54 # Public APIs
55 55
56 56 def register_target(self, target, f):
57 57 """Register a callable f for a given target
58 58
59 59 f will be called with a Comm object as its only argument
60 60 when a comm_open message is received with `target`.
61 61
62 62 f can be a Python callable or an import string for one.
63 63 """
64 64 if isinstance(f, basestring):
65 65 f = import_item(f)
66 66
67 67 self.targets[target] = f
68 68
69 69 def register_comm(self, comm):
70 70 """Register a new comm"""
71 71 comm_id = comm.comm_id
72 72 comm.shell = self.shell
73 73 comm.iopub_socket = self.iopub_socket
74 74 self.comms[comm_id] = comm
75 75 return comm_id
76 76
77 77 def unregister_comm(self, comm_id):
78 78 """Unregister a comm, and close its counterpart"""
79 79 # unlike get_comm, this should raise a KeyError
80 80 comm = self.comms.pop(comm_id)
81 81 comm.close()
82 82
83 83 def get_comm(self, comm_id):
84 84 """Get a comm with a particular id
85 85
86 86 Returns the comm if found, otherwise None.
87 87
88 88 This will not raise an error,
89 89 it will log messages if the comm cannot be found.
90 90 """
91 91 if comm_id not in self.comms:
92 92 self.log.error("No such comm: %s", comm_id)
93 93 self.log.debug("Current comms: %s", lazy_keys(self.comms))
94 94 return
95 95 # call, because we store weakrefs
96 96 comm = self.comms[comm_id]
97 97 return comm
98 98
99 99 # Message handlers
100 100
101 101 def comm_open(self, stream, ident, msg):
102 102 """Handler for comm_open messages"""
103 103 content = msg['content']
104 104 comm_id = content['comm_id']
105 105 target = content['target']
106 106 callback = self.targets.get(target, None)
107 107 comm = Comm(comm_id=comm_id,
108 108 shell=self.shell,
109 109 iopub_socket=self.iopub_socket,
110 110 _open_data=content['data'],
111 111 primary=False,
112 112 )
113 113 if callback is None:
114 114 self.log.error("No such comm target registered: %s", target)
115 comm.destroy()
115 comm.close()
116 116 return
117 117 callback(comm)
118 118 self.register_comm(comm)
119 119
120 120 def comm_msg(self, stream, ident, msg):
121 121 """Handler for comm_msg messages"""
122 122 content = msg['content']
123 123 comm_id = content['comm_id']
124 124 comm = self.get_comm(comm_id)
125 125 if comm is None:
126 126 # no such comm
127 127 return
128 128 comm.handle_msg(content['data'])
129 129
130 130 def comm_close(self, stream, ident, msg):
131 131 """Handler for comm_close messages"""
132 132 content = msg['content']
133 133 comm_id = content['comm_id']
134 134 comm = self.get_comm(comm_id)
135 135 if comm is None:
136 136 # no such comm
137 137 return
138 138 comm.handle_close(content['data'])
139 139 del self.comms[comm_id]
140 140
141 141
142 142 __all__ = ['CommManager']
General Comments 0
You need to be logged in to leave comments. Login now