def register_codec(self, config): """Register codec.""" assert "name" in config assert "encoder" in config or "decoder" in config if "type" in config: for tp in list(self._codecs.keys()): codec = self._codecs[tp] if codec.type == config["type"] or tp == config["name"]: logger.info("Removing duplicated codec: " + tp) del self._codecs[tp] self._codecs[config["name"]] = dotdict(config)
def __init__(self, socketio, plugin_id, session_id): """Set up instance.""" super().__init__(logger) self.plugin_config = dotdict() self._socketio = socketio self._plugin_id = plugin_id self._session_id = session_id self._access_token = None self._expires_in = None self._plugin_origin = "*" self._refresh_token = None self.peer_id = None self.on("initialized", self._initialized)
def set_interface(self, interface, config=None): config = config or {} config = dotdict(config) config.name = config.name or "Jupyter Notebook" config.allow_execution = config.allow_execution or False config.version = config.version or "0.1.0" config.api_version = config.api_version or "0.2.3" config.description = config.description or "[TODO: add description]" config.id = config.id or str(uuid.uuid4()) self.default_config = config self.interface = interface for k in self.clients: self.clients[k].rpc.set_interface(interface, self.default_config) display(Javascript("window.connectPlugin && window.connectPlugin()")) display(HTML('<div id="{}"></div>'.format(config.id)))
def set_interface(self, interface, config=None): """Set the interface.""" config = config or self.default_config config = dotdict(config) config.id = str(uuid.uuid4()) config.name = config.name or config.id config.allow_execution = config.allow_execution or False config.version = config.version or "0.1.0" config.api_version = config.api_version or "0.2.3" config.description = config.description or "[TODO: add description]" self.default_config = config self.interface = interface futures = [] for k in self.clients: fut = self.clients[k].rpc.set_interface(interface, self.default_config) futures.append(fut) return asyncio.gather(*futures)
def initialize(data): self.clients[comm.comm_id] = dotdict() config = self.default_config.copy() cfg = data["config"] if cfg.get("credential_required") is not None: result = config.verify_credential(cfg["credential"]) cfg["auth"] = result["auth"] cfg["id"] = config["id"] rpc = RPC(connection, self.rpc_context, config=cfg, codecs=self._codecs,) rpc.set_interface(self.interface) rpc.init() def patch_api(_): api = rpc.get_remote() or dotdict() api.export = self.set_interface api.registerCodec = self.register_codec api.disposeObject = rpc.dispose_object rpc.on("remoteReady", patch_api) self.clients[comm.comm_id].rpc = rpc
def __init__(self, config): self.config = dotdict(config or {}) super().__init__(self.config.get("debug")) self.channel = self.config.get("channel") or "test_plugin" self._event_handlers = {} self.peer_id = str(uuid.uuid4()) sio = socketio.Client() @sio.on("imjoy_rpc") def on_message(data): if data.get("peer_id") == self.peer_id: if "type" in data: self._fire(data["type"], data) elif self.config.get("debug"): print( f"connection peer id mismatch {data.peer_id} != {self.peer_id}" ) @sio.event def connect(): sio.emit("join_rpc_channel", {"channel": self.channel}) self.emit({ "type": "initialized", "config": self.config, "peer_id": self.peer_id }) self._fire("connected") @sio.event def connect_error(): self._fire("connectFailure") @sio.event def disconnect(): self._fire("disconnected") self.sio = sio
def initialize(data): """Initialize connection.""" self.clients[comm.comm_id] = dotdict() config = self.default_config.copy() config.update(data["config"]) config["id"] = config.get("id", str(uuid.uuid4())) if config.get("credential_required") is not None: result = config.verify_credential(config["credential"]) config["auth"] = result["auth"] rpc = RPC(connection, self.rpc_context, config=config, codecs=self._codecs) rpc.set_interface(self.interface) rpc.init() def patch_api(_): """Patch api.""" api = rpc.get_remote() or dotdict api.init = self.init api.export = self.set_interface api.dispose = rpc.disconnect api.registerCodec = self.register_codec api.disposeObject = rpc.dispose_object rpc.on("remoteReady", patch_api) if on_ready_callback: def ready(_): on_ready_callback(rpc.get_remote()) rpc.once("interfaceSetAsRemote", ready) if on_error_callback: rpc.once("disconnected", on_error_callback) rpc.on("error", on_error_callback) self.clients[comm.comm_id].rpc = rpc
def set_interface(self, interface, config=None): """Set the interface.""" config = config or self.default_config config = dotdict(config) config.id = str(uuid.uuid4()) config.name = config.name or config.id config.allow_execution = config.allow_execution or False config.version = config.version or "0.1.0" config.api_version = config.api_version or "0.2.3" config.description = config.description or "[TODO: add description]" self.default_config = config self.interface = interface futures = [] for k in self.clients: fut = self.clients[k].rpc.set_interface(interface, self.default_config) futures.append(fut) display( Javascript( 'window.connectPlugin && window.connectPlugin("{}")'.format( self.kernel_id))) display(HTML('<div id="{}"></div>'.format(config.id))) return asyncio.gather(*futures)
def patch_api(_): api = rpc.get_remote() or dotdict() api.export = self.set_interface api.registerCodec = self.register_codec api.disposeObject = rpc.dispose_object