2018-01-08 21:38:32 -06:00
|
|
|
;;; jupyter-messages.el --- Jupyter messages -*- lexical-binding: t -*-
|
|
|
|
|
|
|
|
;; Copyright (C) 2018 Nathaniel Nicandro
|
|
|
|
|
|
|
|
;; Author: Nathaniel Nicandro <nathanielnicandro@gmail.com>
|
|
|
|
;; Created: 08 Jan 2018
|
|
|
|
;; Version: 0.0.1
|
|
|
|
;; X-URL: https://github.com/nathan/jupyter-messages
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or
|
|
|
|
;; modify it under the terms of the GNU General Public License as
|
|
|
|
;; published by the Free Software Foundation; either version 2, or (at
|
|
|
|
;; your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;; General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
;; Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2018-01-04 23:03:18 -06:00
|
|
|
(require 'jupyter-base)
|
2017-12-22 00:38:03 -06:00
|
|
|
(require 'jupyter-channels)
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2018-01-08 21:38:32 -06:00
|
|
|
(defgroup jupyter-messages nil
|
|
|
|
"Jupyter messages"
|
2018-01-18 23:02:24 -06:00
|
|
|
:group 'jupyter)
|
2018-01-08 21:38:32 -06:00
|
|
|
|
2017-12-15 22:29:05 -06:00
|
|
|
(defconst jupyter-message-delimiter "<IDS|MSG>"
|
2018-01-18 23:02:24 -06:00
|
|
|
"The message delimiter required in the jupyter messaging protocol.")
|
2017-12-15 22:29:05 -06:00
|
|
|
|
2018-01-08 21:38:32 -06:00
|
|
|
(defconst jupyter--false :json-false
|
2018-01-04 23:03:18 -06:00
|
|
|
"The symbol used to disambiguate nil from boolean false.")
|
2017-12-13 11:27:13 -06:00
|
|
|
|
|
|
|
;;; Signing messages
|
|
|
|
|
2017-12-14 13:28:58 -06:00
|
|
|
(defun jupyter--sign-message (session parts)
|
2017-12-13 11:27:13 -06:00
|
|
|
(if (> (length (jupyter-session-key session)) 0)
|
|
|
|
(cl-loop
|
|
|
|
for b across (hmac-sha256 (mapconcat #'identity parts "")
|
|
|
|
(jupyter-session-key session))
|
|
|
|
concat (format "%02x" b))
|
|
|
|
""))
|
|
|
|
|
2017-12-14 13:28:58 -06:00
|
|
|
(defun jupyter--split-identities (parts)
|
2017-12-13 11:27:13 -06:00
|
|
|
"Extract the identities from a list of message PARTS."
|
|
|
|
(let ((idents nil))
|
|
|
|
(if (catch 'found-delim
|
|
|
|
(while (car parts)
|
|
|
|
(when (string= (car parts) jupyter-message-delimiter)
|
|
|
|
(setq parts (cdr parts)
|
|
|
|
idents (nreverse idents))
|
|
|
|
(throw 'found-delim t))
|
|
|
|
(setq idents (cons (car parts) idents)
|
|
|
|
parts (cdr parts))))
|
|
|
|
(cons idents parts)
|
|
|
|
(error "Message delimiter not in message list"))))
|
|
|
|
|
2017-12-14 13:28:58 -06:00
|
|
|
(defun jupyter--message-header (session msg-type)
|
2017-12-13 11:27:13 -06:00
|
|
|
(list
|
2018-01-04 23:03:18 -06:00
|
|
|
:msg_id (jupyter-new-uuid)
|
2017-12-13 11:27:13 -06:00
|
|
|
:msg_type msg-type
|
|
|
|
:version jupyter-protocol-version
|
|
|
|
:username user-login-name
|
|
|
|
:session (jupyter-session-id session)
|
2017-12-21 18:09:45 -06:00
|
|
|
:date (format-time-string "%FT%T.%6N%z" (current-time))))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
|
|
|
;;; Encode/decoding messages
|
|
|
|
|
2018-01-08 22:54:36 -06:00
|
|
|
(defun jupyter--encode (object)
|
2017-12-21 18:10:19 -06:00
|
|
|
;; Fix encoding recursive objects so that nil will get turned into "{}"
|
|
|
|
(cl-letf (((symbol-function 'json-encode-keyword)
|
|
|
|
(lambda (keyword)
|
|
|
|
(cond ((eq keyword t) "true")
|
|
|
|
((eq keyword json-false) "false")
|
|
|
|
((eq keyword json-null) "{}")))))
|
2018-01-08 22:54:36 -06:00
|
|
|
(encode-coding-string
|
|
|
|
(if (stringp object) object (json-encode-plist object)) 'utf-8)))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2018-01-08 22:54:36 -06:00
|
|
|
(defun jupyter--decode (str)
|
2018-05-05 23:14:04 -05:00
|
|
|
(setq str (decode-coding-string str 'utf-8))
|
|
|
|
(let* ((json-object-type 'plist)
|
|
|
|
(json-array-type 'list)
|
|
|
|
(json-false nil)
|
|
|
|
(val (condition-case nil
|
|
|
|
(json-read-from-string str)
|
|
|
|
;; If it can't be read as JSON, assume its just a regular
|
|
|
|
;; string
|
|
|
|
(json-unknown-keyword str))))
|
|
|
|
(prog1 val
|
|
|
|
(let ((date (plist-get val :date)))
|
|
|
|
(when date
|
|
|
|
(plist-put val :date (jupyter--decode-time date)))))))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2017-12-22 00:38:03 -06:00
|
|
|
(defun jupyter--decode-time (str)
|
|
|
|
(let* ((time (date-to-time str)))
|
|
|
|
(when (string-match "T.+\\(\\(?:\\.\\|,\\)[0-9]+\\)" str)
|
2017-12-27 00:13:43 -06:00
|
|
|
(setq time (list (car time)
|
|
|
|
(cadr time)
|
|
|
|
(ceiling (* 1000000 (string-to-number
|
|
|
|
(match-string 1 str))))
|
|
|
|
0)))
|
2017-12-22 00:38:03 -06:00
|
|
|
time))
|
|
|
|
|
2017-12-14 13:28:58 -06:00
|
|
|
(cl-defun jupyter--encode-message (session
|
|
|
|
type
|
|
|
|
&key idents
|
|
|
|
content
|
|
|
|
parent-header
|
|
|
|
metadata
|
|
|
|
buffers)
|
2017-12-13 11:27:13 -06:00
|
|
|
(declare (indent 2))
|
|
|
|
(cl-check-type session jupyter-session)
|
|
|
|
(cl-check-type metadata json-plist)
|
|
|
|
(cl-check-type content json-plist)
|
|
|
|
(cl-check-type buffers list)
|
2017-12-14 13:28:58 -06:00
|
|
|
(let* ((header (jupyter--message-header session type))
|
2017-12-13 11:27:13 -06:00
|
|
|
(msg-id (plist-get header :msg_id))
|
2018-01-08 22:54:36 -06:00
|
|
|
(parts (mapcar #'jupyter--encode (list header
|
|
|
|
parent-header
|
|
|
|
metadata
|
|
|
|
content))))
|
2017-12-13 11:27:13 -06:00
|
|
|
(cons msg-id
|
|
|
|
(append
|
|
|
|
(when idents (if (stringp idents) (list idents) idents))
|
|
|
|
(list jupyter-message-delimiter
|
2017-12-14 13:28:58 -06:00
|
|
|
(jupyter--sign-message session parts))
|
2017-12-13 11:27:13 -06:00
|
|
|
parts
|
|
|
|
buffers))))
|
|
|
|
|
2017-12-14 13:28:58 -06:00
|
|
|
(defun jupyter--decode-message (session parts)
|
2017-12-13 11:27:13 -06:00
|
|
|
(when (< (length parts) 5)
|
2018-01-08 22:31:33 -06:00
|
|
|
(error "Malformed message. Minimum length of parts is 5"))
|
2017-12-13 11:27:13 -06:00
|
|
|
(when (jupyter-session-key session)
|
|
|
|
(let ((signature (car parts)))
|
2018-02-09 09:11:57 -06:00
|
|
|
(when (string= signature "")
|
2018-01-08 22:31:33 -06:00
|
|
|
(error "Unsigned message"))
|
2017-12-13 11:27:13 -06:00
|
|
|
;; TODO: digest_history
|
|
|
|
;; https://github.com/jupyter/jupyter_client/blob/7a0278af7c1652ac32356d6f00ae29d24d78e61c/jupyter_client/session.py#L915
|
2018-02-09 09:11:57 -06:00
|
|
|
(unless (string= (jupyter--sign-message session (cdr parts)) signature)
|
2017-12-13 11:27:13 -06:00
|
|
|
(error "Invalid signature: %s" signature))))
|
|
|
|
(cl-destructuring-bind
|
|
|
|
(header parent-header metadata content &optional buffers)
|
|
|
|
(cdr parts)
|
2018-05-05 23:14:04 -05:00
|
|
|
(setq header (jupyter--decode header))
|
|
|
|
(list
|
|
|
|
:header header
|
|
|
|
:msg_id (plist-get header :msg_id)
|
|
|
|
:msg_type (plist-get header :msg_type)
|
|
|
|
:parent_header (jupyter--decode parent-header)
|
|
|
|
:metadata (jupyter--decode metadata)
|
|
|
|
:content (jupyter--decode content)
|
|
|
|
:buffers buffers)))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2017-12-17 02:39:16 -06:00
|
|
|
;;; Sending/receiving
|
|
|
|
|
2017-12-31 15:22:48 -06:00
|
|
|
(cl-defmethod jupyter-send ((session jupyter-session)
|
|
|
|
socket
|
|
|
|
type
|
|
|
|
message
|
|
|
|
&optional flags)
|
2017-12-17 02:39:16 -06:00
|
|
|
(declare (indent 1))
|
|
|
|
(cl-destructuring-bind (msg-id . msg)
|
|
|
|
(jupyter--encode-message session type :content message)
|
|
|
|
(zmq-send-multipart socket msg flags)
|
|
|
|
msg-id))
|
|
|
|
|
2017-12-31 15:22:48 -06:00
|
|
|
(cl-defmethod jupyter-recv ((session jupyter-session) socket &optional flags)
|
2017-12-27 00:17:03 -06:00
|
|
|
(let ((msg (zmq-recv-multipart socket flags)))
|
|
|
|
(when msg
|
|
|
|
(cl-destructuring-bind (idents . parts)
|
|
|
|
(jupyter--split-identities msg)
|
|
|
|
(cons idents (jupyter--decode-message session parts))))))
|
2017-12-17 02:39:16 -06:00
|
|
|
|
2017-12-22 00:38:03 -06:00
|
|
|
;;; Control messages
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defun jupyter-message-interrupt-request ()
|
2017-12-22 00:38:03 -06:00
|
|
|
(list))
|
|
|
|
|
2017-12-13 11:27:13 -06:00
|
|
|
;;; stdin messages
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defun jupyter-message-input-reply (&key value)
|
2017-12-13 11:27:13 -06:00
|
|
|
(cl-check-type value string)
|
|
|
|
(list :value value))
|
|
|
|
|
|
|
|
;;; shell messages
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defun jupyter-message-kernel-info-request ()
|
2017-12-22 00:38:03 -06:00
|
|
|
(list))
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defun jupyter-message-execute-request (&key
|
|
|
|
code
|
|
|
|
(silent nil)
|
|
|
|
(store-history t)
|
|
|
|
(user-expressions nil)
|
|
|
|
(allow-stdin t)
|
|
|
|
(stop-on-error nil))
|
2017-12-13 11:27:13 -06:00
|
|
|
(cl-check-type code string)
|
|
|
|
(cl-check-type user-expressions json-plist)
|
2017-12-14 13:29:31 -06:00
|
|
|
(list :code code :silent (if silent t jupyter--false)
|
|
|
|
:store_history (if store-history t jupyter--false)
|
2017-12-13 11:27:13 -06:00
|
|
|
:user_expressions user-expressions
|
2017-12-14 13:29:31 -06:00
|
|
|
:allow_stdin (if allow-stdin t jupyter--false)
|
|
|
|
:stop_on_error (if stop-on-error t jupyter--false)))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defun jupyter-message-inspect-request (&key code pos detail)
|
2017-12-13 11:27:13 -06:00
|
|
|
(setq detail (or detail 0))
|
|
|
|
(unless (member detail '(0 1))
|
|
|
|
(error "Detail can only be 0 or 1 (%s)" detail))
|
|
|
|
(when (markerp pos)
|
|
|
|
(setq pos (marker-position pos)))
|
|
|
|
(cl-check-type code string)
|
|
|
|
(cl-check-type pos integer)
|
|
|
|
(list :code code :cursor_pos pos :detail_level detail))
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defun jupyter-message-complete-request (&key code pos)
|
2017-12-13 11:27:13 -06:00
|
|
|
(when (markerp pos)
|
|
|
|
(setq pos (marker-position pos)))
|
|
|
|
(cl-check-type code string)
|
|
|
|
(cl-check-type pos integer)
|
|
|
|
(list :code code :cursor_pos pos))
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defun jupyter-message-history-request (&key
|
|
|
|
output
|
|
|
|
raw
|
|
|
|
hist-access-type
|
|
|
|
session
|
|
|
|
start
|
|
|
|
stop
|
|
|
|
n
|
|
|
|
pattern
|
|
|
|
unique)
|
2017-12-13 11:27:13 -06:00
|
|
|
(unless (member hist-access-type '("range" "tail" "search"))
|
|
|
|
(error "History access type can only be one of (range, tail, search)"))
|
|
|
|
(append
|
2017-12-14 13:29:31 -06:00
|
|
|
(list :output (if output t jupyter--false) :raw (if raw t jupyter--false)
|
2017-12-13 11:27:13 -06:00
|
|
|
:hist_access_type hist-access-type)
|
|
|
|
(cond
|
|
|
|
((equal hist-access-type "range")
|
|
|
|
(cl-check-type session integer)
|
|
|
|
(cl-check-type start integer)
|
|
|
|
(cl-check-type stop integer)
|
|
|
|
(list :session session :start start :stop stop))
|
|
|
|
((equal hist-access-type "tail")
|
|
|
|
(cl-check-type n integer)
|
|
|
|
(list :n n))
|
|
|
|
((equal hist-access-type "search")
|
|
|
|
(cl-check-type pattern string)
|
|
|
|
(cl-check-type n integer)
|
2017-12-14 13:29:31 -06:00
|
|
|
(list :pattern pattern :unique (if unique t jupyter--false) :n n)))))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defun jupyter-message-is-complete-request (&key code)
|
2017-12-13 11:27:13 -06:00
|
|
|
(cl-check-type code string)
|
|
|
|
(list :code code))
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defun jupyter-message-comm-info-request (&key target-name)
|
2017-12-13 11:27:13 -06:00
|
|
|
(when target-name
|
|
|
|
(cl-check-type target-name string)
|
|
|
|
(list :target_name target-name)))
|
|
|
|
|
2017-12-31 15:19:23 -06:00
|
|
|
(cl-defun jupyter-message-shutdown-request (&key restart)
|
2017-12-14 13:29:31 -06:00
|
|
|
(list :restart (if restart t jupyter--false)))
|
2017-12-13 11:27:13 -06:00
|
|
|
|
2017-12-15 22:28:45 -06:00
|
|
|
;;; Convenience functions
|
|
|
|
|
2018-01-02 00:28:54 -06:00
|
|
|
(defsubst jupyter-message-id (msg)
|
|
|
|
"Get the ID of MSG."
|
2017-12-15 22:28:45 -06:00
|
|
|
(plist-get msg :msg_id))
|
|
|
|
|
2018-01-02 00:28:54 -06:00
|
|
|
(defsubst jupyter-message-parent-id (msg)
|
|
|
|
"Get the parent ID of MSG."
|
|
|
|
(jupyter-message-id (plist-get msg :parent_header)))
|
2017-12-15 22:28:45 -06:00
|
|
|
|
2018-01-02 00:28:54 -06:00
|
|
|
(defsubst jupyter-message-type (msg)
|
|
|
|
"Get the MSG type."
|
|
|
|
(plist-get msg :msg_type))
|
2017-12-15 22:28:45 -06:00
|
|
|
|
2018-01-02 00:28:54 -06:00
|
|
|
(defsubst jupyter-message-content (msg)
|
|
|
|
"Get the MSG contents."
|
2017-12-22 00:38:03 -06:00
|
|
|
(plist-get msg :content))
|
|
|
|
|
2018-01-02 00:28:54 -06:00
|
|
|
(defsubst jupyter-message-time (msg)
|
|
|
|
"Get the MSG time.
|
|
|
|
The returned time has the same form as returned by
|
|
|
|
`current-time'."
|
2017-12-22 00:38:03 -06:00
|
|
|
(plist-get (plist-get msg :header) :date))
|
|
|
|
|
2018-01-02 00:28:54 -06:00
|
|
|
(defsubst jupyter-message-get (msg key)
|
2018-01-18 23:02:24 -06:00
|
|
|
"Get the value in MSG's `jupyter-message-content' that corresponds to KEY."
|
2018-01-02 00:28:54 -06:00
|
|
|
(plist-get (jupyter-message-content msg) key))
|
|
|
|
|
|
|
|
(defsubst jupyter-message-data (msg mimetype)
|
2018-01-18 23:02:24 -06:00
|
|
|
"Get the message data for a specific mimetype.
|
|
|
|
MSG should be a message with a `:data' field in its contents.
|
|
|
|
MIMETYPE is should be a standard media mimetype
|
|
|
|
keyword (`:text/plain', `:image/png', ...). If the messages data
|
|
|
|
has a key corresponding to MIMETYPE, return the value. Otherwise
|
|
|
|
return nil."
|
2018-01-02 00:28:54 -06:00
|
|
|
(plist-get (jupyter-message-get msg :data) mimetype))
|
|
|
|
|
|
|
|
(defun jupyter-message-status-idle-p (msg)
|
2018-01-18 23:02:24 -06:00
|
|
|
"Determine if MSG is a status: idle message."
|
2018-01-02 00:28:54 -06:00
|
|
|
(and (equal (jupyter-message-type msg) "status")
|
|
|
|
(equal (jupyter-message-get msg :execution_state) "idle")))
|
2017-12-15 22:28:45 -06:00
|
|
|
|
2018-01-18 23:02:24 -06:00
|
|
|
(defun jupyter-message-status-starting-p (msg)
|
|
|
|
"Determine if MSG is a status: starting message."
|
|
|
|
(and (equal (jupyter-message-type msg) "status")
|
|
|
|
(equal (jupyter-message-get msg :execution_state) "starting")))
|
|
|
|
|
2017-12-13 11:27:13 -06:00
|
|
|
(provide 'jupyter-messages)
|
|
|
|
|
2018-01-08 21:38:32 -06:00
|
|
|
;;; jupyter-messages.el ends here
|
|
|
|
|
2017-12-13 11:27:13 -06:00
|
|
|
;; Local Variables:
|
|
|
|
;; byte-compile-warnings: (not free-vars)
|
|
|
|
;; End:
|