2018-01-04 23:03:18 -06:00
|
|
|
(require 'jupyter-base)
|
|
|
|
(require 'jupyter-connection)
|
2017-12-13 11:27:13 -06:00
|
|
|
(require 'jupyter-channels)
|
|
|
|
(require 'jupyter-messages)
|
2018-01-04 23:03:18 -06:00
|
|
|
(eval-when-compile (require 'cl))
|
2018-01-04 20:58:28 -06:00
|
|
|
|
|
|
|
(defvar jupyter--debug nil
|
|
|
|
"Set to non-nil to emit sent and received messages to *Messages*.")
|
|
|
|
|
|
|
|
(defvar jupyter-default-timeout 1
|
|
|
|
"The default timeout in seconds for `jupyter-wait-until'.")
|
2017-12-14 13:39:30 -06:00
|
|
|
|
2017-12-22 00:50:56 -06:00
|
|
|
(defclass jupyter-kernel-client (jupyter-connection)
|
2017-12-19 18:02:55 -06:00
|
|
|
((requests
|
2017-12-13 11:27:13 -06:00
|
|
|
:type hash-table
|
2017-12-15 22:26:21 -06:00
|
|
|
:initform (make-hash-table :test 'equal)
|
2017-12-13 11:27:13 -06:00
|
|
|
:documentation "A hash table with message ID's as keys. This
|
|
|
|
is used to register callback functions to run once a reply from
|
|
|
|
a previously sent request is received. See
|
2018-01-02 01:02:35 -06:00
|
|
|
`jupyter-add-callback'. Note that this is also used to filter
|
|
|
|
received messages that originated from a previous request by
|
|
|
|
this client. Whenever the client sends a message in which a
|
2017-12-15 18:14:28 -06:00
|
|
|
reply is expected, it sets an entry in this table to represent
|
|
|
|
the fact that the message has been sent. So if there is a
|
|
|
|
non-nil value for a message ID it means that a message has been
|
|
|
|
sent and the client is expecting a reply from the kernel.")
|
2017-12-17 02:39:16 -06:00
|
|
|
(ioloop
|
|
|
|
:type (or null process)
|
2017-12-16 18:54:40 -06:00
|
|
|
:initform nil
|
|
|
|
:documentation "The process which polls for events on all
|
|
|
|
live channels of the client.")
|
2017-12-13 11:27:13 -06:00
|
|
|
(shell-channel
|
2017-12-21 18:12:25 -06:00
|
|
|
:type (or null jupyter-shell-channel)
|
2017-12-13 11:27:13 -06:00
|
|
|
:initarg :shell-channel
|
|
|
|
:documentation "The shell channel.")
|
|
|
|
(iopub-channel
|
2017-12-21 18:12:25 -06:00
|
|
|
:type (or null jupyter-iopub-channel)
|
|
|
|
:initform nil
|
2017-12-13 11:27:13 -06:00
|
|
|
:initarg :iopub-channel
|
|
|
|
:documentation "The IOPub channel.")
|
|
|
|
(hb-channel
|
2017-12-21 18:12:25 -06:00
|
|
|
:type (or null jupyter-hb-channel)
|
|
|
|
:initform nil
|
2017-12-13 11:27:13 -06:00
|
|
|
:initarg :hb-channel
|
|
|
|
:documentation "The heartbeat channel.")
|
|
|
|
(stdin-channel
|
2017-12-21 18:12:25 -06:00
|
|
|
:type (or null jupyter-stdin-channel)
|
|
|
|
:initform nil
|
2017-12-13 11:27:13 -06:00
|
|
|
:initarg :stdin-channel
|
|
|
|
:documentation "The stdin channel.")))
|
|
|
|
|
2018-01-04 20:59:01 -06:00
|
|
|
(cl-defmethod jupyter-initialize-connection
|
2017-12-22 00:50:56 -06:00
|
|
|
((client jupyter-kernel-client)
|
|
|
|
file-or-plist)
|
2018-01-04 20:59:01 -06:00
|
|
|
"Initialize CLIENT with a connection FILE-OR-PLIST.
|
|
|
|
If FILE-OR-PLIST is a file name, then it is assumed to be a file
|
|
|
|
containing a JSON dictionary with the necessary keys for
|
|
|
|
connecting to a Jupyter kernel. This file will be read using
|
|
|
|
`json-read-file' and the CLIENT's channels initialized using the
|
|
|
|
connection info read from the plist created from the files
|
|
|
|
contents. If FILE-OR-PLIST is a plist, then the CLIENT's channels
|
|
|
|
is initialized from the plist in the same way as described above.
|
|
|
|
Again, under the assumption that the plist has the necessay keys
|
|
|
|
for connecting to a Jupyter kernel, see
|
2017-12-22 00:50:56 -06:00
|
|
|
http://jupyter-client.readthedocs.io/en/latest/kernels.html#connection-files."
|
|
|
|
(cl-check-type file-or-plist (or json-plist file-exists))
|
2018-01-04 15:58:02 -06:00
|
|
|
(let ((conn-info (if (json-plist-p file-or-plist) file-or-plist
|
|
|
|
(let ((json-array-type 'list)
|
|
|
|
(json-object-type 'plist)
|
|
|
|
(json-false nil))
|
2017-12-22 00:50:56 -06:00
|
|
|
(json-read-file file-or-plist)))))
|
|
|
|
(oset client conn-info conn-info)
|
|
|
|
(cl-destructuring-bind
|
|
|
|
(&key shell_port iopub_port stdin_port hb_port control_port ip
|
|
|
|
key transport signature_scheme kernel_name
|
|
|
|
&allow-other-keys)
|
|
|
|
conn-info
|
|
|
|
(when (and (> (length key) 0)
|
|
|
|
(not (functionp (intern signature_scheme))))
|
|
|
|
(error "Unsupported signature scheme: %s" signature_scheme))
|
|
|
|
;; Stop the channels if connected to some other kernel
|
|
|
|
(jupyter-stop-channels client)
|
|
|
|
(let ((addr (concat transport "://" ip)))
|
|
|
|
(oset client session (jupyter-session :key key))
|
|
|
|
(oset client stdin-channel
|
|
|
|
(make-instance
|
2017-12-27 00:30:47 -06:00
|
|
|
'jupyter-stdin-channel
|
2017-12-22 00:50:56 -06:00
|
|
|
:endpoint (format "%s:%d" addr stdin_port)))
|
|
|
|
(oset client shell-channel
|
|
|
|
(make-instance
|
2017-12-27 00:30:47 -06:00
|
|
|
'jupyter-shell-channel
|
2017-12-22 00:50:56 -06:00
|
|
|
:endpoint (format "%s:%d" addr shell_port)))
|
|
|
|
(oset client hb-channel
|
2018-01-04 23:30:40 -06:00
|
|
|
(make-instance
|
|
|
|
'jupyter-hb-channel
|
2017-12-22 00:50:56 -06:00
|
|
|
:endpoint (format "%s:%d" addr hb_port)))
|
|
|
|
(oset client iopub-channel
|
|
|
|
(make-instance
|
2017-12-27 00:30:47 -06:00
|
|
|
'jupyter-iopub-channel
|
2017-12-22 00:50:56 -06:00
|
|
|
:endpoint (format "%s:%d" addr iopub_port)))))))
|
|
|
|
|
2017-12-15 18:21:54 -06:00
|
|
|
;;; Lower level sending/receiving
|
|
|
|
|
2017-12-31 15:22:48 -06:00
|
|
|
(cl-defmethod jupyter-send ((client jupyter-kernel-client)
|
|
|
|
channel
|
|
|
|
type
|
|
|
|
message
|
|
|
|
&optional flags)
|
2017-12-15 18:21:54 -06:00
|
|
|
"Encode MESSAGE and send it on CLIENT's CHANNEL.
|
2017-12-31 15:22:48 -06:00
|
|
|
The message should have a TYPE corresponding to one of those
|
|
|
|
found in the jupyter messaging protocol. Optional variable FLAGS
|
|
|
|
are the flags sent to the underlying `zmq-send-multipart' call
|
|
|
|
using the CHANNEL's socket."
|
2017-12-15 18:21:54 -06:00
|
|
|
(declare (indent 1))
|
2018-01-02 00:56:02 -06:00
|
|
|
(zmq-subprocess-send (oref client ioloop)
|
|
|
|
(list 'send (oref channel type) type message flags))
|
|
|
|
;; Anything sent to stdin is a reply not a request so don't add it to
|
|
|
|
;; `:jupyter-pending-requests.'
|
|
|
|
(unless (eq (oref channel type) :stdin)
|
|
|
|
(let ((req (make-jupyter-request)))
|
|
|
|
(jupyter--ioloop-push-request client req)
|
|
|
|
req)))
|
2017-12-15 18:21:54 -06:00
|
|
|
|
2017-12-17 02:39:16 -06:00
|
|
|
(defun jupyter--ioloop (client)
|
|
|
|
(let ((iopub-channel (oref client iopub-channel))
|
|
|
|
(shell-channel (oref client shell-channel))
|
2017-12-27 00:30:47 -06:00
|
|
|
(stdin-channel (oref client stdin-channel)))
|
2017-12-17 02:39:16 -06:00
|
|
|
`(lambda (ctx)
|
|
|
|
(require 'jupyter-channels ,(locate-library "jupyter-channels"))
|
|
|
|
(require 'jupyter-messages ,(locate-library "jupyter-messages"))
|
|
|
|
;; We can splice the session object because it contains primitive types
|
|
|
|
(let* ((session ,(oref client session))
|
|
|
|
(iopub
|
|
|
|
(let ((sock (jupyter-connect-channel
|
|
|
|
:iopub ,(oref (oref client iopub-channel) endpoint)
|
|
|
|
(jupyter-session-id session))))
|
|
|
|
(zmq-socket-set sock zmq-SUBSCRIBE "")
|
|
|
|
sock))
|
|
|
|
(shell
|
|
|
|
(jupyter-connect-channel
|
|
|
|
:shell ,(oref (oref client shell-channel) endpoint)
|
|
|
|
(jupyter-session-id session)))
|
|
|
|
(stdin
|
|
|
|
(jupyter-connect-channel
|
|
|
|
:stdin ,(oref (oref client stdin-channel) endpoint)
|
|
|
|
(jupyter-session-id session)))
|
2017-12-27 00:30:47 -06:00
|
|
|
(channels (list (cons stdin :stdin)
|
2017-12-17 02:39:16 -06:00
|
|
|
(cons shell :shell)
|
2017-12-21 18:16:15 -06:00
|
|
|
(cons iopub :iopub)))
|
2018-01-04 17:11:21 -06:00
|
|
|
(priorities (list (cons :shell 4)
|
|
|
|
(cons :iopub 2)
|
|
|
|
(cons :stdin 2)))
|
2017-12-21 18:16:15 -06:00
|
|
|
(idle-count 0)
|
|
|
|
(queue (make-ring 10)))
|
|
|
|
(cl-flet ((send-recvd
|
|
|
|
()
|
|
|
|
;; Try and have a consistent order with which messages are
|
|
|
|
;; received in the parent process. We queue messages
|
|
|
|
;; received from the kernel and send them up to the parent
|
|
|
|
;; process only when (1) no messages have been received in
|
|
|
|
;; two polling periods or (2) when the queue is filled.
|
|
|
|
;; When sending, the messages are sorted by their send time
|
|
|
|
;; using the `:date' field of the message `:header'. In the
|
|
|
|
;; case that two messages have the same send time from the
|
|
|
|
;; kernel (i.e. when they don't have fractional resolution)
|
|
|
|
;; the messages are sorted by channel priority.
|
|
|
|
(cl-sort (cddr queue)
|
|
|
|
;; [<sorted non-nil elements> nil nil ...]
|
|
|
|
(lambda (a b)
|
|
|
|
(cond
|
|
|
|
((and (eq a nil) (eq a b)) t)
|
|
|
|
((eq a nil) nil)
|
|
|
|
((eq b nil) t)
|
2017-12-27 00:32:00 -06:00
|
|
|
(t
|
|
|
|
;; elements are (ctype idents . msg)
|
|
|
|
(let ((ta (jupyter-message-time (cddr a)))
|
|
|
|
(tb (jupyter-message-time (cddr b))))
|
|
|
|
(or (time-less-p ta tb)
|
|
|
|
(when (equal ta tb)
|
|
|
|
(> (alist-get (car a) priorities)
|
|
|
|
(alist-get (car b) priorities)))))))))
|
2017-12-21 18:16:15 -06:00
|
|
|
(cl-loop
|
|
|
|
while (not (ring-empty-p queue))
|
2017-12-27 00:32:00 -06:00
|
|
|
do (zmq-prin1 (cons 'recvd (ring-remove queue)))))
|
2017-12-21 18:16:15 -06:00
|
|
|
(recv-message
|
2017-12-19 11:49:52 -06:00
|
|
|
(sock ctype)
|
2017-12-21 18:16:15 -06:00
|
|
|
(when (= (ring-length queue) (ring-size queue))
|
|
|
|
(send-recvd))
|
2017-12-31 15:22:48 -06:00
|
|
|
(ring-insert queue (cons ctype (jupyter-recv session sock))))
|
2017-12-19 11:49:52 -06:00
|
|
|
(send-message
|
|
|
|
(sock ctype rest)
|
|
|
|
(zmq-prin1
|
|
|
|
(cons 'sent
|
2017-12-31 15:22:48 -06:00
|
|
|
(cons ctype (apply #'jupyter-send session sock rest)))))
|
2017-12-19 11:49:52 -06:00
|
|
|
(start-channel
|
|
|
|
(sock)
|
|
|
|
(zmq-connect
|
|
|
|
sock (zmq-socket-get sock zmq-LAST_ENDPOINT))
|
|
|
|
(zmq-poller-register
|
|
|
|
(current-zmq-poller) sock zmq-POLLIN))
|
|
|
|
(stop-channel
|
|
|
|
(sock)
|
|
|
|
(zmq-poller-unregister (current-zmq-poller) sock)
|
|
|
|
(condition-case err
|
|
|
|
(zmq-disconnect
|
|
|
|
sock (zmq-socket-get sock zmq-LAST_ENDPOINT))
|
|
|
|
(zmq-ENOENT nil)
|
|
|
|
(error (signal (car err) (cdr err))))))
|
2017-12-19 18:47:57 -06:00
|
|
|
(condition-case nil
|
|
|
|
(with-zmq-poller
|
2018-01-02 01:02:35 -06:00
|
|
|
;; Also poll for standard-in events to be able to read commands
|
|
|
|
;; from the parent emacs process without blocking
|
2017-12-19 18:47:57 -06:00
|
|
|
(zmq-poller-register (current-zmq-poller) 0 zmq-POLLIN)
|
|
|
|
(mapc (lambda (x) (zmq-poller-register (current-zmq-poller)
|
|
|
|
(car x)
|
|
|
|
zmq-POLLIN))
|
|
|
|
channels)
|
|
|
|
(unwind-protect
|
|
|
|
(while t
|
2017-12-21 18:16:15 -06:00
|
|
|
(when (and (= idle-count 2)
|
|
|
|
(> (ring-length queue) 0))
|
|
|
|
(send-recvd))
|
|
|
|
(let ((events (condition-case err
|
|
|
|
(zmq-poller-wait-all (current-zmq-poller) 5 20)
|
|
|
|
(zmq-EINTR nil)
|
2018-01-02 01:02:35 -06:00
|
|
|
;; TODO: For any other kind of error,
|
|
|
|
;; just reset the polling loop by exiting
|
|
|
|
;; `with-zmq-poller'.
|
2017-12-21 18:16:15 -06:00
|
|
|
(error (signal (car err) (cdr err))))))
|
|
|
|
(if (null events) (setq idle-count (1+ idle-count))
|
|
|
|
(setq idle-count 0)
|
2017-12-19 18:47:57 -06:00
|
|
|
(when (alist-get 0 events)
|
2017-12-21 18:16:15 -06:00
|
|
|
(setf (alist-get 0 events nil 'remove) nil)
|
2017-12-19 18:47:57 -06:00
|
|
|
(cl-destructuring-bind (cmd . data)
|
|
|
|
(zmq-subprocess-read)
|
|
|
|
(cl-case cmd
|
|
|
|
(quit
|
|
|
|
(signal 'quit nil))
|
|
|
|
(start-channel
|
|
|
|
(let* ((ctype data)
|
|
|
|
(sock (car (rassoc ctype channels))))
|
|
|
|
(start-channel sock)))
|
|
|
|
(stop-channel
|
|
|
|
(let* ((ctype data)
|
|
|
|
(sock (car (rassoc ctype channels))))
|
|
|
|
(stop-channel sock)))
|
|
|
|
(send
|
|
|
|
(let* ((ctype (car data))
|
|
|
|
(sock (car (rassoc ctype channels)))
|
|
|
|
(rest (cdr data)))
|
|
|
|
(send-message sock ctype rest))))))
|
2017-12-21 18:16:15 -06:00
|
|
|
(cl-loop for (sock . event) in events
|
|
|
|
do (recv-message
|
|
|
|
sock (alist-get sock channels))))))
|
2017-12-19 18:47:57 -06:00
|
|
|
(mapc (lambda (s)
|
|
|
|
(zmq-socket-set s zmq-LINGER 0)
|
|
|
|
(zmq-close s))
|
|
|
|
(mapcar #'car channels))))
|
|
|
|
(quit (zmq-prin1 (cons 'quit (cons nil nil))))))))))
|
|
|
|
|
2018-01-02 00:56:02 -06:00
|
|
|
(defun jupyter--ioloop-pop-request (client)
|
|
|
|
(let* ((ring (process-get (oref client ioloop) :jupyter-pending-requests))
|
|
|
|
(req (ring-remove ring)))
|
|
|
|
req))
|
|
|
|
|
|
|
|
(defun jupyter--ioloop-push-request (client req)
|
|
|
|
(let* ((ioloop (oref client ioloop))
|
|
|
|
(ring (or (process-get ioloop :jupyter-pending-requests)
|
|
|
|
(let ((ring (make-ring 10)))
|
|
|
|
(process-put ioloop :jupyter-pending-requests ring)
|
|
|
|
ring))))
|
|
|
|
(ring-insert+extend ring req 'grow)))
|
|
|
|
|
2017-12-27 00:00:59 -06:00
|
|
|
(defun jupyter--ioloop-sentinel (client ioloop event)
|
|
|
|
(cond
|
2017-12-30 23:35:04 -06:00
|
|
|
((cl-loop for type in '("exited" "failed" "finished" "killed" "deleted")
|
|
|
|
thereis (string-prefix-p type event))
|
2018-01-04 17:08:41 -06:00
|
|
|
(jupyter-stop-channel (oref client hb-channel))
|
2017-12-27 00:00:59 -06:00
|
|
|
(oset client ioloop nil))))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2017-12-17 02:39:16 -06:00
|
|
|
(defun jupyter--ioloop-filter (client event)
|
|
|
|
(cl-destructuring-bind (ctype . data) (cdr event)
|
|
|
|
(cl-case (car event)
|
2017-12-27 00:00:59 -06:00
|
|
|
;; Cleanup handled in sentinel
|
|
|
|
(quit)
|
2017-12-17 02:39:16 -06:00
|
|
|
(sent
|
2018-01-02 00:56:02 -06:00
|
|
|
(when jupyter--debug
|
|
|
|
(message "SEND: %s" data))
|
2017-12-19 18:02:55 -06:00
|
|
|
(unless (eq ctype :stdin)
|
2018-01-02 00:56:02 -06:00
|
|
|
;; Anything sent on stdin is a reply and therefore never added to
|
|
|
|
;; `:jupyter-pending-requests'
|
|
|
|
(let ((id data)
|
|
|
|
(req (jupyter--ioloop-pop-request client)))
|
|
|
|
(setf (jupyter-request--id req) id)
|
|
|
|
(puthash id req (oref client requests)))))
|
2017-12-17 02:39:16 -06:00
|
|
|
(recvd
|
2018-01-02 00:56:02 -06:00
|
|
|
(when jupyter--debug
|
|
|
|
(message "RECV: %s %s %s"
|
|
|
|
(jupyter-message-type (cdr data))
|
|
|
|
(jupyter-message-parent-id (cdr data))
|
|
|
|
(jupyter-message-content (cdr data))))
|
|
|
|
(let ((channel (cl-find-if (lambda (c) (eq (oref c type) ctype))
|
2018-01-04 20:44:15 -06:00
|
|
|
(mapcar (lambda (x) (slot-value client x))
|
2018-01-02 00:56:02 -06:00
|
|
|
'(stdin-channel
|
|
|
|
shell-channel
|
|
|
|
iopub-channel)))))
|
2018-01-04 23:27:41 -06:00
|
|
|
(jupyter-push-message channel data)
|
2018-01-02 00:56:02 -06:00
|
|
|
(run-with-timer 0.001 nil #'jupyter-handle-message client channel))))))
|
2017-12-31 15:22:48 -06:00
|
|
|
|
2017-12-13 11:27:13 -06:00
|
|
|
(cl-defmethod jupyter-start-channels ((client jupyter-kernel-client)
|
|
|
|
&key (shell t)
|
|
|
|
(iopub t)
|
|
|
|
(stdin t)
|
2017-12-15 18:14:43 -06:00
|
|
|
(control t)
|
2017-12-13 11:27:13 -06:00
|
|
|
(hb t))
|
2017-12-15 18:14:28 -06:00
|
|
|
"Start the pre-configured channels of CLIENT.
|
|
|
|
This function calls `jupyter-start-channel' for every channel
|
|
|
|
that has a non-nil value passed to this function. All channels
|
|
|
|
are started by default, so to prevent a channel from starting you
|
|
|
|
would have to pass a nil value for the channel's key. As an
|
|
|
|
example, to prevent the control channel from starting you would
|
|
|
|
call this function like so
|
|
|
|
|
|
|
|
(jupyter-start-channels client :control nil)
|
|
|
|
|
|
|
|
In addition to calling `jupyter-start-channel', a subprocess is
|
|
|
|
created for each channel which monitors the channel's socket for
|
|
|
|
input events. Note that this polling subprocess is not created
|
|
|
|
for the heartbeat channel."
|
2017-12-21 18:12:25 -06:00
|
|
|
(unless (oref client ioloop)
|
|
|
|
(when hb (jupyter-start-channel (oref client hb-channel)))
|
|
|
|
(oset client ioloop
|
|
|
|
(zmq-start-process
|
|
|
|
(jupyter--ioloop client)
|
2017-12-27 00:00:59 -06:00
|
|
|
(apply-partially #'jupyter--ioloop-filter client)
|
|
|
|
(apply-partially #'jupyter--ioloop-sentinel client)))))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2017-12-19 18:13:42 -06:00
|
|
|
(cl-defmethod jupyter-stop-channels ((client jupyter-kernel-client))
|
|
|
|
"Stop any running channels of CLIENT."
|
2017-12-21 18:12:25 -06:00
|
|
|
(when (oref client hb-channel)
|
|
|
|
(jupyter-stop-channel (oref client hb-channel)))
|
2017-12-19 18:13:42 -06:00
|
|
|
(let ((ioloop (oref client ioloop)))
|
|
|
|
(when ioloop
|
2017-12-19 18:47:57 -06:00
|
|
|
(zmq-subprocess-send ioloop (cons 'quit nil))
|
2017-12-27 00:00:59 -06:00
|
|
|
(with-timeout (1 (delete-process ioloop))
|
2017-12-19 18:47:57 -06:00
|
|
|
(while (oref client ioloop)
|
|
|
|
(sleep-for 0 100))))))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2017-12-15 18:14:28 -06:00
|
|
|
(cl-defmethod jupyter-channels-running-p ((client jupyter-kernel-client))
|
|
|
|
"Are any channels of CLIENT alive?"
|
2017-12-13 11:27:13 -06:00
|
|
|
(cl-loop
|
|
|
|
for channel in (list 'shell-channel
|
|
|
|
'iopub-channel
|
|
|
|
'hb-channel
|
|
|
|
'stdin-channel)
|
2018-01-04 20:44:15 -06:00
|
|
|
thereis (jupyter-channel-alive-p (slot-value client channel))))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2017-12-15 22:26:21 -06:00
|
|
|
;;; Message callbacks
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2018-01-06 15:31:39 -06:00
|
|
|
(defun jupyter--run-callbacks (req msg)
|
|
|
|
"Run REQ's MSG callbacks.
|
|
|
|
See `jupyter-add-callback'."
|
2017-12-19 18:02:55 -06:00
|
|
|
(when req
|
|
|
|
(let* ((callbacks (jupyter-request-callbacks req))
|
2018-01-06 19:47:47 -06:00
|
|
|
(cb-all-types (cdr (assoc t callbacks)))
|
|
|
|
(cb-for-type (cdr (assoc (jupyter-message-type msg) callbacks))))
|
|
|
|
(and cb-all-types (funcall cb-all-types msg))
|
|
|
|
(and cb-for-type (funcall cb-for-type msg)))))
|
2017-12-19 18:02:55 -06:00
|
|
|
|
2018-01-06 15:31:39 -06:00
|
|
|
(defun jupyter--add-callback (req msg-type cb)
|
|
|
|
"Add REQ MSG-TYPE callback, CB."
|
|
|
|
(setq msg-type (or (plist-get jupyter-message-types msg-type)
|
|
|
|
;; A msg-type of t means that FUNCTION is run for all
|
|
|
|
;; messages associated with a request.
|
|
|
|
(eq msg-type t)))
|
|
|
|
(unless msg-type
|
|
|
|
(error "Not a valid message type (`%s')" msg-type))
|
|
|
|
(let ((callbacks (jupyter-request-callbacks req)))
|
|
|
|
(if (null callbacks)
|
|
|
|
(setf (jupyter-request-callbacks req)
|
|
|
|
(list (cons msg-type cb)))
|
|
|
|
(let ((cb-for-type (assoc msg-type callbacks)))
|
|
|
|
(if (not cb-for-type)
|
|
|
|
(nconc callbacks (list (cons msg-type cb)))
|
|
|
|
(setq cb (apply-partially
|
|
|
|
(lambda (cb1 cb2 msg)
|
|
|
|
(funcall cb1 msg)
|
|
|
|
(funcall cb2 msg))
|
|
|
|
(cdr cb-for-type)
|
|
|
|
cb))
|
|
|
|
(setcdr cb-for-type cb))))))
|
|
|
|
|
|
|
|
(defun jupyter-add-callback (req msg-type cb &rest callbacks)
|
|
|
|
"Add a callback to run when a message is received for a request.
|
|
|
|
REQ is a `jupyter-request' returned by one of the request methods
|
|
|
|
of a `jupyter-kernel-client'. MSG-TYPE is a keyword corresponding
|
|
|
|
to one of the keys in `jupyter-message-types'. MSG-TYPE can also
|
|
|
|
be a list, in which case run CB for every MSG-TYPE in the list.
|
|
|
|
If MSG-TYPE is t, then run CB for every message received for REQ.
|
|
|
|
CB is the callback function which will run with a single
|
|
|
|
argument, a message whose `jupyter-message-parent-id' is the same
|
|
|
|
as the `jupyter-request-id' of REQ and whose
|
|
|
|
`jupyter-message-type' corresponds to the value of MSG-TYPE in
|
|
|
|
the `jupyter-message-types' plist. Any additional arguments to
|
|
|
|
`jupyter-add-callback' are interpreted as additional CALLBACKS to
|
|
|
|
add to REQ. So to add multiple callbacks to a request you would
|
|
|
|
do
|
|
|
|
|
|
|
|
(jupyter-add-callback
|
|
|
|
(jupyter-execute-request client :code \"1 + 2\")
|
|
|
|
:status (lambda (msg) ...)
|
|
|
|
:execute-reply (lambda (msg) ...)
|
|
|
|
:execute-result (lambda (msg) ...))"
|
|
|
|
(declare (indent 1))
|
|
|
|
(if (jupyter-request-idle-received-p req)
|
|
|
|
(error "Request already received idle message")
|
|
|
|
(setq callbacks (append (list msg-type cb) callbacks))
|
|
|
|
(cl-loop for (msg-type cb) on callbacks by 'cddr
|
|
|
|
if (listp msg-type)
|
|
|
|
do (mapc (lambda (mt) (jupyter--add-callback req mt cb)) msg-type)
|
|
|
|
else do (jupyter--add-callback req msg-type cb))))
|
|
|
|
|
|
|
|
(defun jupyter-wait-until (req msg-type fun &optional timeout)
|
2018-01-02 14:37:41 -06:00
|
|
|
"Wait until FUN returns non-nil for a received message.
|
|
|
|
FUN is run on every received message for request, REQ, that has
|
|
|
|
type, MSG-TYPE. If FUN does not return a non-nil value before
|
|
|
|
TIMEOUT, return nil. Otherwise return the message which caused
|
|
|
|
FUN to return a non-nil value. Note that if TIMEOUT is nil, it
|
2018-01-04 20:58:28 -06:00
|
|
|
defaults to `jupyter-default-timeout'."
|
2018-01-06 15:31:39 -06:00
|
|
|
(declare (indent 1))
|
2018-01-04 20:58:28 -06:00
|
|
|
(setq timeout (or timeout jupyter-default-timeout))
|
2017-12-19 18:16:58 -06:00
|
|
|
(cl-check-type timeout number)
|
2017-12-15 18:18:41 -06:00
|
|
|
(lexical-let ((msg nil)
|
2018-01-02 14:37:41 -06:00
|
|
|
(fun fun))
|
2018-01-06 15:31:39 -06:00
|
|
|
(jupyter-add-callback req
|
|
|
|
msg-type (lambda (m) (setq msg (when (funcall fun m) m))))
|
2018-01-02 13:23:28 -06:00
|
|
|
(with-timeout (timeout nil)
|
|
|
|
(while (null msg)
|
|
|
|
(sleep-for 0.01))
|
|
|
|
msg)))
|
2017-12-15 18:18:41 -06:00
|
|
|
|
2017-12-31 13:54:43 -06:00
|
|
|
(defun jupyter-wait-until-idle (req &optional timeout)
|
2018-01-02 13:24:12 -06:00
|
|
|
"Wait until TIMEOUT for REQ to receive an idle message.
|
2018-01-04 20:58:28 -06:00
|
|
|
If TIMEOUT is non-nil, it defaults to `jupyter-default-timeout'."
|
2018-01-06 15:31:39 -06:00
|
|
|
(jupyter-wait-until req :status #'jupyter-message-status-idle-p timeout))
|
2017-12-15 18:18:41 -06:00
|
|
|
|
2017-12-31 13:54:43 -06:00
|
|
|
(defun jupyter-wait-until-received (msg-type req &optional timeout)
|
2017-12-31 13:16:59 -06:00
|
|
|
"Wait for a message with MSG-TYPE to be received by CLIENT.
|
2017-12-14 13:48:39 -06:00
|
|
|
This function waits until CLIENT receives a message from the
|
|
|
|
kernel that satisfies the following conditions:
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2017-12-14 13:48:39 -06:00
|
|
|
1. The message has a type of MSG-TYPE
|
|
|
|
2. The parent header of the message has a message ID of PMSG-ID
|
|
|
|
|
|
|
|
Note that MSG-TYPE should be one of the keys found in
|
|
|
|
`jupyter--recieved-message-types'. If it is not, an error is
|
|
|
|
raised.
|
|
|
|
|
2017-12-14 14:07:21 -06:00
|
|
|
All of the `jupyter-request-*' functions return a message ID that
|
2017-12-14 13:48:39 -06:00
|
|
|
can be passed to this function as the PMSG-ID. If the message
|
|
|
|
associated with PMSG-ID is not expecting to receive a message
|
|
|
|
with MSG-TYPE, this function will wait forever so be sure that
|
|
|
|
you are expecting to receive a message of a certain type after
|
|
|
|
sending one. For example you would not be expecting an
|
|
|
|
`execute-reply' when you send a kernel info request with
|
2017-12-14 14:07:21 -06:00
|
|
|
`jupyter-request-kernel-info', but you would be expecting a
|
2017-12-14 13:48:39 -06:00
|
|
|
`kernel-info-reply'. See the jupyter messaging specification for
|
|
|
|
more info
|
|
|
|
http://jupyter-client.readthedocs.io/en/latest/messaging.html"
|
2017-12-31 13:54:43 -06:00
|
|
|
(declare (indent 1))
|
2018-01-06 15:31:39 -06:00
|
|
|
(jupyter-wait-until req msg-type #'identity timeout))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2018-01-06 19:43:21 -06:00
|
|
|
(cl-defmethod jupyter-handle-message ((client jupyter-kernel-client) channel)
|
2017-12-13 11:27:13 -06:00
|
|
|
"Process a message on CLIENT's CHANNEL.
|
2018-01-06 19:43:21 -06:00
|
|
|
When a message is received from the kernel, the
|
|
|
|
`jupyter-handle-message' method is called on the client. The
|
|
|
|
client method runs any callbacks for the message and possibly
|
|
|
|
runs the client handler for the channel the message was received
|
|
|
|
on. The channel's `jupyter-handle-message' method will then pass
|
|
|
|
the message to the appropriate message handler based on message
|
|
|
|
type which terminates the execution path.
|
|
|
|
|
|
|
|
So when a message is received from the kernel the following steps
|
|
|
|
are taken:
|
|
|
|
|
|
|
|
- `jupyter-handle-message' (client)
|
|
|
|
- Run callbacks for message
|
|
|
|
- Possibly run channel handlers
|
|
|
|
- `jupyter-handle-message' (channel)
|
|
|
|
- Based on message type, dispatch to
|
|
|
|
`jupyter-handle-execute-result',
|
|
|
|
`jupyter-handle-kernel-info-reply', ...
|
|
|
|
- Cleanup request when kernel is done processing it"
|
2018-01-04 23:27:41 -06:00
|
|
|
(when (jupyter-messages-available-p channel)
|
|
|
|
(let* ((msg (jupyter-get-message channel))
|
2018-01-06 19:43:21 -06:00
|
|
|
(pmsg-id (jupyter-message-parent-id msg))
|
|
|
|
(requests (oref client requests))
|
|
|
|
(req (gethash pmsg-id requests)))
|
|
|
|
(if (not req)
|
|
|
|
;; Always run handlers of IOPub messages, even when they are not
|
|
|
|
;; associated with any request that was sent by us.
|
|
|
|
;;
|
|
|
|
;; TODO: Would we always want this?
|
|
|
|
(when (eq (oref channel type) :iopub)
|
|
|
|
(jupyter-handle-message channel client nil msg))
|
|
|
|
(unwind-protect
|
2018-01-06 15:31:39 -06:00
|
|
|
(jupyter--run-callbacks req msg)
|
2017-12-15 22:26:21 -06:00
|
|
|
(unwind-protect
|
2018-01-06 19:47:47 -06:00
|
|
|
(when (jupyter-request-run-handlers-p req)
|
|
|
|
(jupyter-handle-message channel client req msg))
|
2018-01-06 19:43:21 -06:00
|
|
|
(when (jupyter-message-status-idle-p msg)
|
2018-01-04 15:52:04 -06:00
|
|
|
(setf (jupyter-request-idle-received-p req) t)
|
2018-01-06 19:43:21 -06:00
|
|
|
(remhash pmsg-id requests))))))
|
|
|
|
(run-with-timer 0.005 nil #'jupyter-handle-message client channel)))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2018-01-02 01:02:35 -06:00
|
|
|
;;; STDIN message requests/handlers
|
2018-01-06 19:43:21 -06:00
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-message ((channel jupyter-stdin-channel)
|
|
|
|
client
|
|
|
|
req
|
|
|
|
msg)
|
2017-12-13 11:27:13 -06:00
|
|
|
(cl-destructuring-bind (&key prompt password &allow-other-keys)
|
2018-01-06 19:43:21 -06:00
|
|
|
(jupyter-message-content msg)
|
|
|
|
(jupyter-handle-input-reply client req prompt password)))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-input-reply ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
prompt
|
|
|
|
password)
|
2017-12-15 18:14:28 -06:00
|
|
|
"Handle an input request from CLIENT's kernel.
|
|
|
|
PROMPT is the prompt the kernel would like to show the user. If
|
|
|
|
PASSWORD is non-nil, then `read-passwd' is used to get input from
|
|
|
|
the user. Otherwise `read-from-minibuffer' is used."
|
2018-01-02 01:02:35 -06:00
|
|
|
;; TODO: Allow for quiting the input request. In this case, I suppose send an
|
|
|
|
;; interrupt request to the kernel
|
2017-12-13 11:27:13 -06:00
|
|
|
(let ((channel (oref client stdin-channel))
|
2017-12-31 15:19:23 -06:00
|
|
|
(msg (jupyter-message-input-reply
|
2017-12-13 11:27:13 -06:00
|
|
|
:value (funcall (if password #'read-passwd
|
|
|
|
#'read-from-minibuffer)
|
|
|
|
prompt))))
|
|
|
|
;; TODO: Check for 'allow_stdin'
|
|
|
|
;; http://jupyter-client.readthedocs.io/en/latest/messaging.html#stdin-messages
|
2017-12-31 15:22:48 -06:00
|
|
|
(jupyter-send client channel "input_reply" msg)))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2018-01-02 01:02:35 -06:00
|
|
|
;;; CONTROL message requests/handlers
|
2017-12-14 13:32:29 -06:00
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-message ((channel jupyter-control-channel)
|
|
|
|
client
|
|
|
|
req
|
|
|
|
msg)
|
2018-01-06 19:43:21 -06:00
|
|
|
(cl-destructuring-bind (&key status ename evalue &allow-other-keys)
|
|
|
|
(jupyter-message-content msg)
|
|
|
|
(if (equal status "ok")
|
|
|
|
;; FIXME: An interrupt reply is only sent when interrupt_mode is set
|
|
|
|
;; to message in a kernel's kernelspec.
|
|
|
|
(pcase (jupyter-message-type msg)
|
|
|
|
("interrupt_reply"
|
|
|
|
(jupyter-handle-interrupt-reply client req)))
|
|
|
|
;; FIXME: How to handle errors more generally? Just let the IOPub message
|
|
|
|
;; handle it?
|
|
|
|
(if (equal status "error") (error "Error (%s): %s" ename evalue)
|
|
|
|
(error "Error: aborted")))))
|
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-shutdown-request ((client jupyter-kernel-client)
|
|
|
|
&optional restart)
|
2017-12-15 18:14:28 -06:00
|
|
|
"Request a shutdown of CLIENT's kernel.
|
2017-12-14 13:32:29 -06:00
|
|
|
If RESTART is non-nil, request a restart instead of a complete shutdown."
|
2017-12-27 00:30:47 -06:00
|
|
|
(let ((channel (oref client shell-channel))
|
2017-12-31 15:19:23 -06:00
|
|
|
(msg (jupyter-message-shutdown-request :restart restart)))
|
2017-12-31 15:22:48 -06:00
|
|
|
(jupyter-send client channel "shutdown_request" msg)))
|
2017-12-14 13:32:29 -06:00
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-shutdown-reply ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
restart)
|
2017-12-19 18:02:55 -06:00
|
|
|
"Default shutdown reply handler.")
|
2017-12-19 11:54:10 -06:00
|
|
|
|
|
|
|
;; FIXME: This breaks the convention that all jupyter-request-* functions
|
|
|
|
;; returns a message-id future object.
|
2017-12-27 00:30:47 -06:00
|
|
|
;; (cl-defmethod jupyter-request-interrupt ((client jupyter-kernel-client))
|
|
|
|
;; ;; TODO: Check for interrupt_mode of the kernel's kernelspec
|
|
|
|
;; ;; http://jupyter-client.readthedocs.io/en/latest/messaging.html#kernel-interrupt
|
|
|
|
;; (let ((channel (oref client control-channel)))
|
2017-12-31 15:22:48 -06:00
|
|
|
;; (jupyter-send client channel "interrupt_request" ())))
|
2017-12-27 00:30:47 -06:00
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-interrupt-reply ((client jupyter-kernel-client)
|
|
|
|
req)
|
2017-12-31 13:16:59 -06:00
|
|
|
"Default interrupt reply handler.")
|
2017-12-14 13:32:29 -06:00
|
|
|
|
2018-01-02 01:02:35 -06:00
|
|
|
;;; SHELL message requests/handlers
|
2017-12-13 11:27:13 -06:00
|
|
|
|
|
|
|
;; http://jupyter-client.readthedocs.io/en/latest/messaging.html#messages-on-the-shell-router-dealer-channel
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-message ((channel jupyter-shell-channel)
|
|
|
|
client
|
|
|
|
req
|
|
|
|
msg)
|
2018-01-06 19:43:21 -06:00
|
|
|
(let ((content (jupyter-message-content msg)))
|
|
|
|
(cl-destructuring-bind (&key status ename evalue &allow-other-keys) content
|
|
|
|
;; is_complete_reply messages have a status other than "ok" so just
|
|
|
|
;; ensure that the status does not correspond to an error.
|
2017-12-19 11:55:07 -06:00
|
|
|
(if (not (member status '("error" "abort")))
|
2018-01-06 19:43:21 -06:00
|
|
|
(pcase (jupyter-message-type msg)
|
2017-12-14 13:34:03 -06:00
|
|
|
("execute_reply"
|
|
|
|
(cl-destructuring-bind (&key execution_count
|
|
|
|
user_expressions
|
|
|
|
payload
|
|
|
|
&allow-other-keys)
|
|
|
|
content
|
2018-01-02 00:38:46 -06:00
|
|
|
(jupyter-handle-execute-reply
|
2017-12-19 18:02:55 -06:00
|
|
|
client req execution_count user_expressions payload)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("inspect_reply"
|
|
|
|
(cl-destructuring-bind (&key found
|
|
|
|
data
|
|
|
|
metadata
|
|
|
|
&allow-other-keys)
|
|
|
|
content
|
2018-01-02 00:38:46 -06:00
|
|
|
(jupyter-handle-inspect-reply
|
2017-12-19 18:02:55 -06:00
|
|
|
client req found data metadata)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("complete_reply"
|
|
|
|
(cl-destructuring-bind (&key matches
|
|
|
|
cursor_start
|
|
|
|
cursor_end
|
|
|
|
metadata
|
|
|
|
&allow-other-keys)
|
|
|
|
content
|
2018-01-02 00:38:46 -06:00
|
|
|
(jupyter-handle-complete-reply
|
2017-12-19 18:02:55 -06:00
|
|
|
client req matches cursor_start cursor_end metadata)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("history_reply"
|
|
|
|
(cl-destructuring-bind (&key history &allow-other-keys)
|
|
|
|
content
|
2018-01-02 00:38:46 -06:00
|
|
|
(jupyter-handle-history-reply client req history)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("is_complete_reply"
|
|
|
|
(cl-destructuring-bind (&key status indent &allow-other-keys)
|
|
|
|
content
|
2018-01-02 00:38:46 -06:00
|
|
|
(jupyter-handle-is-complete-reply client req status indent)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("comm_info_reply"
|
|
|
|
(cl-destructuring-bind (&key comms &allow-other-keys)
|
|
|
|
content
|
2018-01-02 00:38:46 -06:00
|
|
|
(jupyter-handle-comm-info-reply client req comms)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("kernel_info_reply"
|
|
|
|
(cl-destructuring-bind (&key protocol_version
|
|
|
|
implementation
|
|
|
|
implementation_version
|
|
|
|
language_info
|
|
|
|
banner
|
|
|
|
help_links
|
|
|
|
&allow-other-keys)
|
|
|
|
content
|
2018-01-02 00:38:46 -06:00
|
|
|
(jupyter-handle-kernel-info-reply
|
2018-01-04 15:56:04 -06:00
|
|
|
client req protocol_version implementation
|
|
|
|
implementation_version language_info banner help_links)))
|
2017-12-13 11:27:13 -06:00
|
|
|
(_ (error "Message type not handled yet.")))
|
2017-12-31 13:16:59 -06:00
|
|
|
;; FIXME: Do something about errrors here?
|
|
|
|
;; (if (equal status "error")
|
|
|
|
;; (error "Error (%s): %s"
|
|
|
|
;; (plist-get content :ename) (plist-get content :evalue))
|
|
|
|
;; (error "Error: aborted"))
|
|
|
|
))))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defmethod jupyter-execute-request ((client jupyter-kernel-client)
|
2017-12-14 14:07:21 -06:00
|
|
|
&key code
|
|
|
|
(silent nil)
|
|
|
|
(store-history t)
|
|
|
|
(user-expressions nil)
|
|
|
|
(allow-stdin t)
|
|
|
|
(stop-on-error nil))
|
2017-12-15 18:14:28 -06:00
|
|
|
"Send an execute request."
|
2017-12-13 11:27:13 -06:00
|
|
|
(declare (indent 1))
|
|
|
|
(let ((channel (oref client shell-channel))
|
2017-12-31 15:19:23 -06:00
|
|
|
(msg (jupyter-message-execute-request
|
2017-12-13 11:27:13 -06:00
|
|
|
:code code
|
|
|
|
:silent silent
|
|
|
|
:store-history store-history
|
|
|
|
:user-expressions user-expressions
|
|
|
|
:allow-stdin allow-stdin
|
|
|
|
:stop-on-error stop-on-error)))
|
2017-12-31 15:22:48 -06:00
|
|
|
(jupyter-send client channel "execute_request" msg)))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2018-01-02 00:38:46 -06:00
|
|
|
(cl-defmethod jupyter-handle-execute-reply ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
execution-count
|
|
|
|
user-expressions
|
|
|
|
payload)
|
2017-12-14 13:34:03 -06:00
|
|
|
"Default execute reply handler.")
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defmethod jupyter-inspect-request ((client jupyter-kernel-client)
|
2017-12-14 14:07:21 -06:00
|
|
|
&key code
|
|
|
|
(pos 0)
|
|
|
|
(detail 0))
|
2017-12-15 18:14:28 -06:00
|
|
|
"Send an inspect request."
|
2017-12-14 13:34:03 -06:00
|
|
|
(declare (indent 1))
|
|
|
|
(let ((channel (oref client shell-channel))
|
2017-12-31 15:19:23 -06:00
|
|
|
(msg (jupyter-message-inspect-request
|
2017-12-14 13:34:03 -06:00
|
|
|
:code code :pos pos :detail detail)))
|
2017-12-31 15:22:48 -06:00
|
|
|
(jupyter-send client channel "inspect_request" msg)))
|
2017-12-14 13:34:03 -06:00
|
|
|
|
2018-01-02 00:38:46 -06:00
|
|
|
(cl-defmethod jupyter-handle-inspect-reply ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
found
|
|
|
|
data
|
|
|
|
metadata)
|
2017-12-14 13:34:03 -06:00
|
|
|
"Default inspect reply handler.")
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defmethod jupyter-complete-request ((client jupyter-kernel-client)
|
2017-12-14 14:07:21 -06:00
|
|
|
&key code
|
|
|
|
(pos 0))
|
2017-12-15 18:14:28 -06:00
|
|
|
"Send a complete request."
|
2017-12-14 13:34:03 -06:00
|
|
|
(declare (indent 1))
|
|
|
|
(let ((channel (oref client shell-channel))
|
2017-12-31 15:19:23 -06:00
|
|
|
(msg (jupyter-message-complete-request
|
2017-12-14 13:34:03 -06:00
|
|
|
:code code :pos pos)))
|
2017-12-31 15:22:48 -06:00
|
|
|
(jupyter-send client channel "complete_request" msg)))
|
2017-12-14 13:34:03 -06:00
|
|
|
|
2018-01-02 00:38:46 -06:00
|
|
|
(cl-defmethod jupyter-handle-complete-reply ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
matches
|
|
|
|
cursor-start
|
|
|
|
cursor-end
|
|
|
|
metadata)
|
2017-12-14 13:34:03 -06:00
|
|
|
"Default complete reply handler.")
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defmethod jupyter-history-request ((client jupyter-kernel-client)
|
2017-12-14 14:07:21 -06:00
|
|
|
&key
|
|
|
|
output
|
|
|
|
raw
|
2017-12-27 00:18:00 -06:00
|
|
|
(hist-access-type "tail")
|
2017-12-14 14:07:21 -06:00
|
|
|
session
|
|
|
|
start
|
|
|
|
stop
|
2017-12-27 00:18:00 -06:00
|
|
|
(n 10)
|
2017-12-14 14:07:21 -06:00
|
|
|
pattern
|
|
|
|
unique)
|
2017-12-15 18:14:28 -06:00
|
|
|
"Send a history request."
|
2017-12-14 13:34:03 -06:00
|
|
|
(declare (indent 1))
|
|
|
|
(let ((channel (oref client shell-channel))
|
2017-12-31 15:19:23 -06:00
|
|
|
(msg (jupyter-message-history-request
|
2017-12-14 13:34:03 -06:00
|
|
|
:output output
|
|
|
|
:raw raw
|
|
|
|
:hist-access-type hist-access-type
|
|
|
|
:session session
|
|
|
|
:start start
|
|
|
|
:stop stop
|
|
|
|
:n n
|
2017-12-17 02:58:11 -06:00
|
|
|
:pattern pattern
|
2017-12-14 13:34:03 -06:00
|
|
|
:unique unique)))
|
2017-12-31 15:22:48 -06:00
|
|
|
(jupyter-send client channel "history_request" msg)))
|
2017-12-14 13:34:03 -06:00
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-history-reply ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
history)
|
2017-12-14 13:34:03 -06:00
|
|
|
"Default history reply handler.")
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defmethod jupyter-is-complete-request ((client jupyter-kernel-client)
|
2017-12-14 14:07:21 -06:00
|
|
|
&key code)
|
2017-12-15 18:14:28 -06:00
|
|
|
"Send an is-complete request."
|
2017-12-14 13:34:03 -06:00
|
|
|
(declare (indent 1))
|
|
|
|
(let ((channel (oref client shell-channel))
|
2017-12-31 15:19:23 -06:00
|
|
|
(msg (jupyter-message-is-complete-request
|
2017-12-14 13:34:03 -06:00
|
|
|
:code code)))
|
2017-12-31 15:22:48 -06:00
|
|
|
(jupyter-send client channel "is_complete_request" msg)))
|
2017-12-14 13:34:03 -06:00
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-is-complete-reply ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
status
|
|
|
|
indent)
|
2017-12-14 13:34:03 -06:00
|
|
|
"Default is complete reply handler.")
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defmethod jupyter-comm-info-request ((client jupyter-kernel-client)
|
2017-12-14 14:07:21 -06:00
|
|
|
&key target-name)
|
2017-12-15 18:14:28 -06:00
|
|
|
"Send a comm-info request."
|
2017-12-14 13:34:03 -06:00
|
|
|
(declare (indent 1))
|
|
|
|
(let ((channel (oref client shell-channel))
|
2017-12-31 15:19:23 -06:00
|
|
|
(msg (jupyter-message-comm-info-request
|
2017-12-14 13:34:03 -06:00
|
|
|
:target-name target-name)))
|
2017-12-31 15:22:48 -06:00
|
|
|
(jupyter-send client channel "comm_info_request" msg)))
|
2017-12-14 13:34:03 -06:00
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-comm-info-reply ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
comms)
|
2017-12-14 13:34:03 -06:00
|
|
|
"Default comm info. reply handler.")
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defmethod jupyter-kernel-info-request ((client jupyter-kernel-client))
|
2017-12-15 18:14:28 -06:00
|
|
|
"Send a kernel-info request."
|
2018-01-04 15:56:04 -06:00
|
|
|
(let ((channel (oref client shell-channel))
|
|
|
|
(msg (jupyter-message-kernel-info-request)))
|
2017-12-31 15:19:23 -06:00
|
|
|
(jupyter-send client channel "kernel_info_request" msg)))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2018-01-02 00:38:46 -06:00
|
|
|
(cl-defmethod jupyter-handle-kernel-info-reply ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
protocol-version
|
|
|
|
implementation
|
|
|
|
implementation-version
|
|
|
|
language-info
|
|
|
|
banner
|
|
|
|
help-links)
|
2017-12-14 13:34:03 -06:00
|
|
|
"Default kernel-info reply handler.")
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2018-01-02 00:38:46 -06:00
|
|
|
;;; IOPUB message handlers
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-message ((channel jupyter-iopub-channel)
|
|
|
|
client
|
|
|
|
req
|
|
|
|
msg)
|
2018-01-02 00:38:46 -06:00
|
|
|
(let ((content (jupyter-message-content msg)))
|
|
|
|
(pcase (jupyter-message-type msg)
|
2017-12-19 11:54:10 -06:00
|
|
|
("shutdown_reply"
|
|
|
|
(cl-destructuring-bind (&key restart &allow-other-keys)
|
|
|
|
content
|
2018-01-04 15:56:04 -06:00
|
|
|
(jupyter-handle-shutdown-reply
|
|
|
|
client req restart)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("stream"
|
|
|
|
(cl-destructuring-bind (&key name text &allow-other-keys)
|
|
|
|
content
|
2018-01-04 15:56:04 -06:00
|
|
|
(jupyter-handle-stream
|
|
|
|
client req name text)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("execute_input"
|
|
|
|
(cl-destructuring-bind (&key code execution_count &allow-other-keys)
|
|
|
|
content
|
2018-01-04 15:56:04 -06:00
|
|
|
(jupyter-handle-execute-input
|
|
|
|
client req code execution_count)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("execute_result"
|
|
|
|
(cl-destructuring-bind (&key execution_count
|
|
|
|
data
|
|
|
|
metadata
|
|
|
|
&allow-other-keys)
|
|
|
|
content
|
2018-01-04 15:56:04 -06:00
|
|
|
(jupyter-handle-execute-result
|
|
|
|
client req execution_count data metadata)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("error"
|
|
|
|
(cl-destructuring-bind (&key ename evalue traceback &allow-other-keys)
|
|
|
|
content
|
2018-01-04 15:56:04 -06:00
|
|
|
(jupyter-handle-error
|
|
|
|
client req ename evalue traceback)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("status"
|
|
|
|
(cl-destructuring-bind (&key execution_state &allow-other-keys)
|
|
|
|
content
|
2018-01-04 15:56:04 -06:00
|
|
|
(jupyter-handle-status
|
|
|
|
client req execution_state)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("clear_output"
|
|
|
|
(cl-destructuring-bind (&key wait &allow-other-keys)
|
|
|
|
content
|
2018-01-04 15:56:04 -06:00
|
|
|
(jupyter-handle-clear-output
|
|
|
|
client req wait)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("display_data"
|
|
|
|
(cl-destructuring-bind (&key data metadata transient &allow-other-keys)
|
|
|
|
content
|
2018-01-04 15:56:04 -06:00
|
|
|
(jupyter-handle-display-data
|
|
|
|
client req data metadata transient)))
|
2017-12-14 13:34:03 -06:00
|
|
|
("update_display_data"
|
|
|
|
(cl-destructuring-bind (&key data metadata transient &allow-other-keys)
|
|
|
|
content
|
2018-01-04 15:56:04 -06:00
|
|
|
(jupyter-handle-update-display-data
|
|
|
|
client req data metadata transient)))
|
2017-12-13 11:27:13 -06:00
|
|
|
(_ (error "Message type not handled yet.")))))
|
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-stream ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
name
|
|
|
|
text)
|
2017-12-14 13:57:33 -06:00
|
|
|
"Default stream handler.")
|
2017-12-13 11:27:13 -06:00
|
|
|
|
|
|
|
(cl-defmethod jupyter-handle-execute-input ((client jupyter-kernel-client)
|
2017-12-19 18:02:55 -06:00
|
|
|
req
|
2017-12-13 11:27:13 -06:00
|
|
|
code
|
|
|
|
execution-count)
|
2017-12-14 13:34:03 -06:00
|
|
|
"Default execute input handler.")
|
|
|
|
|
|
|
|
(cl-defmethod jupyter-handle-execute-result ((client jupyter-kernel-client)
|
2017-12-19 18:02:55 -06:00
|
|
|
req
|
2017-12-14 13:34:03 -06:00
|
|
|
execution-count
|
|
|
|
data
|
|
|
|
metadata)
|
|
|
|
"Default execute result handler.")
|
|
|
|
|
|
|
|
(cl-defmethod jupyter-handle-error ((client jupyter-kernel-client)
|
2017-12-19 18:02:55 -06:00
|
|
|
req
|
2017-12-14 13:34:03 -06:00
|
|
|
ename
|
|
|
|
evalue
|
|
|
|
traceback)
|
|
|
|
"Default error handler.")
|
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-status ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
execution-state)
|
2017-12-14 13:34:03 -06:00
|
|
|
"Default status handler.")
|
|
|
|
|
2018-01-04 15:56:04 -06:00
|
|
|
(cl-defmethod jupyter-handle-clear-output ((client jupyter-kernel-client)
|
|
|
|
req
|
|
|
|
wait)
|
2017-12-14 13:34:03 -06:00
|
|
|
"Default clear output handler.")
|
|
|
|
|
|
|
|
(cl-defmethod jupyter-handle-display-data ((client jupyter-kernel-client)
|
2017-12-19 18:02:55 -06:00
|
|
|
req
|
2017-12-14 13:34:03 -06:00
|
|
|
data
|
|
|
|
metadata
|
|
|
|
transient)
|
|
|
|
"Default display data handler.")
|
|
|
|
|
|
|
|
(cl-defmethod jupyter-handle-update-display-data ((client jupyter-kernel-client)
|
2017-12-19 18:02:55 -06:00
|
|
|
req
|
2017-12-14 13:34:03 -06:00
|
|
|
data
|
|
|
|
metadata
|
|
|
|
transient)
|
|
|
|
"Default update display data handler.")
|
2017-12-13 11:27:13 -06:00
|
|
|
|
|
|
|
(provide 'jupyter-client)
|