/usr/lib/python2.7/dist-packages/notebook/static/services/kernels/comm.js is in python-notebook 5.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | // Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'jquery',
'base/js/utils',
], function($, utils) {
"use strict";
//-----------------------------------------------------------------------
// CommManager class
//-----------------------------------------------------------------------
var CommManager = function (kernel) {
this.comms = {};
this.targets = {};
if (kernel !== undefined) {
this.init_kernel(kernel);
}
};
CommManager.prototype.init_kernel = function (kernel) {
/**
* connect the kernel, and register message handlers
*/
this.kernel = kernel;
var msg_types = ['comm_open', 'comm_msg', 'comm_close'];
for (var i = 0; i < msg_types.length; i++) {
var msg_type = msg_types[i];
kernel.register_iopub_handler(msg_type, $.proxy(this[msg_type], this));
}
};
CommManager.prototype.new_comm = function (target_name, data, callbacks, metadata, comm_id, buffers) {
/**
* Create a new Comm, register it, and open its Kernel-side counterpart
* Mimics the auto-registration in `Comm.__init__` in the Jupyter Comm.
*
* argument comm_id is optional
*/
var comm = new Comm(target_name, comm_id);
this.register_comm(comm);
comm.open(data, callbacks, metadata, buffers);
return comm;
};
CommManager.prototype.register_target = function (target_name, f) {
/**
* Register a target function for a given target name
*/
this.targets[target_name] = f;
};
CommManager.prototype.unregister_target = function (target_name, f) {
/**
* Unregister a target function for a given target name
*/
delete this.targets[target_name];
};
CommManager.prototype.register_comm = function (comm) {
/**
* Register a comm in the mapping
*/
this.comms[comm.comm_id] = Promise.resolve(comm);
comm.kernel = this.kernel;
return comm.comm_id;
};
CommManager.prototype.unregister_comm = function (comm) {
/**
* Remove a comm from the mapping
*/
delete this.comms[comm.comm_id];
};
// comm message handlers
CommManager.prototype.comm_open = function (msg) {
var content = msg.content;
var that = this;
var comm_id = content.comm_id;
this.comms[comm_id] = utils.load_class(content.target_name, content.target_module,
this.targets).then(function(target) {
var comm = new Comm(content.target_name, comm_id);
comm.kernel = that.kernel;
try {
var response = target(comm, msg);
} catch (e) {
comm.close();
that.unregister_comm(comm);
var wrapped_error = new utils.WrappedError("Exception opening new comm", e);
console.error(wrapped_error);
return Promise.reject(wrapped_error);
}
// Regardless of the target return value, we need to
// then return the comm
return Promise.resolve(response).then(function() {return comm;});
}, utils.reject('Could not open comm', true));
return this.comms[comm_id];
};
CommManager.prototype.comm_close = function(msg) {
var content = msg.content;
if (this.comms[content.comm_id] === undefined) {
console.error('Comm promise not found for comm id ' + content.comm_id);
return;
}
var that = this;
this.comms[content.comm_id] = this.comms[content.comm_id].then(function(comm) {
that.unregister_comm(comm);
try {
comm.handle_close(msg);
} catch (e) {
console.log("Exception closing comm: ", e, e.stack, msg);
}
// don't return a comm, so that further .then() functions
// get an undefined comm input
});
return this.comms[content.comm_id];
};
CommManager.prototype.comm_msg = function(msg) {
var content = msg.content;
if (this.comms[content.comm_id] === undefined) {
console.error('Comm promise not found for comm id ' + content.comm_id);
return;
}
this.comms[content.comm_id] = this.comms[content.comm_id].then(function(comm) {
return (Promise.resolve(comm.handle_msg(msg))
.catch(utils.reject('Exception handling comm message'))
.then(function() {return comm;}));
});
return this.comms[content.comm_id];
};
//-----------------------------------------------------------------------
// Comm base class
//-----------------------------------------------------------------------
var Comm = function (target_name, comm_id) {
this.target_name = target_name;
this.comm_id = comm_id || utils.uuid();
this._msg_callback = this._close_callback = null;
};
// methods for sending messages
Comm.prototype.open = function (data, callbacks, metadata, buffers) {
var content = {
comm_id : this.comm_id,
target_name : this.target_name,
data : data || {},
};
return this.kernel.send_shell_message("comm_open", content, callbacks, metadata, buffers);
};
Comm.prototype.send = function (data, callbacks, metadata, buffers) {
var content = {
comm_id : this.comm_id,
data : data || {},
};
return this.kernel.send_shell_message("comm_msg", content, callbacks, metadata, buffers);
};
Comm.prototype.close = function (data, callbacks, metadata, buffers) {
var content = {
comm_id : this.comm_id,
data : data || {},
};
return this.kernel.send_shell_message("comm_close", content, callbacks, metadata, buffers);
};
// methods for registering callbacks for incoming messages
Comm.prototype._register_callback = function (key, callback) {
this['_' + key + '_callback'] = callback;
};
Comm.prototype.on_msg = function (callback) {
this._register_callback('msg', callback);
};
Comm.prototype.on_close = function (callback) {
this._register_callback('close', callback);
};
// methods for handling incoming messages
Comm.prototype._callback = function (key, msg) {
var callback = this['_' + key + '_callback'];
if (callback) {
try {
return callback(msg);
} catch (e) {
console.log("Exception in Comm callback", e, e.stack, msg);
}
}
};
Comm.prototype.handle_msg = function (msg) {
return this._callback('msg', msg);
};
Comm.prototype.handle_close = function (msg) {
this._callback('close', msg);
};
return {
'CommManager': CommManager,
'Comm': Comm
};
});
|