Commit 89d52b8f authored by Ludovic Courtès's avatar Ludovic Courtès
Browse files

messages: Provide a simpler 'header' procedure and export it.

* jupyter/messages.scm (<header>): Rename contructor to '%header'.
(%protocol-version): New variable.
(current-date-string, header, make-header): New procedures.
(reply): Adjust to new 'header' API.
(string->header): Likewise.
parent 16553955
...@@ -38,6 +38,8 @@ ...@@ -38,6 +38,8 @@
message-parts message-parts
parts->message parts->message
header
make-header
header? header?
header-id header-id
header-user header-user
...@@ -57,7 +59,7 @@ ...@@ -57,7 +59,7 @@
;; Jupyter message header as defined at ;; Jupyter message header as defined at
;; <https://jupyter-client.readthedocs.io/en/stable/messaging.html#general-message-format>. ;; <https://jupyter-client.readthedocs.io/en/stable/messaging.html#general-message-format>.
(define-record-type <header> (define-record-type <header>
(header id user session date type version sender) (%header id user session date type version sender)
header? header?
(id header-id) ;bytevector (UUID) (id header-id) ;bytevector (UUID)
(user header-user) ;string (user header-user) ;string
...@@ -70,6 +72,24 @@ ...@@ -70,6 +72,24 @@
;; itself. ;; itself.
(sender header-sender)) ;bytevector (UUID) | #f (sender header-sender)) ;bytevector (UUID) | #f
(define %protocol-version "5.0")
(define (current-date-string)
(date->string (time-utc->date (current-time time-utc))
"~5.~N"))
(define* (header type user session
#:key
(id (make-id))
(date (current-date-string))
(version %protocol-version)
sender)
"Return a new Jupyter message header."
(%header id user session date type version sender))
(define make-header header)
;; Jupyter message. ;; Jupyter message.
(define-record-type <message> (define-record-type <message>
(%message header parent-header metadata content buffers) (%message header parent-header metadata content buffers)
...@@ -122,14 +142,8 @@ This is a low-level procedure for internal use." ...@@ -122,14 +142,8 @@ This is a low-level procedure for internal use."
(define (reply message type content) (define (reply message type content)
"Return a Jupyter message that is a reply to MESSAGE." "Return a Jupyter message that is a reply to MESSAGE."
(let ((parent (message-header message))) (let ((parent (message-header message)))
(make-message (header (make-id) (header-user parent) (make-message (header type (header-user parent)
(header-session parent) (header-session parent))
(date->string (time-utc->date
(current-time time-utc))
"~5.~N")
type
(header-version parent)
#f)
content content
#:parent-header parent))) #:parent-header parent)))
...@@ -147,13 +161,13 @@ record. Return #f if STR is the empty dictionary." ...@@ -147,13 +161,13 @@ record. Return #f if STR is the empty dictionary."
(let ((table (json-string->scm str))) (let ((table (json-string->scm str)))
(if (zero? (hash-count (const #t) table)) (if (zero? (hash-count (const #t) table))
#f #f
(header (hash-ref table "msg_id") (header (hash-ref table "msg_type")
(hash-ref table "username") (hash-ref table "username")
(hash-ref table "session") (hash-ref table "session")
(hash-ref table "date") #:id (hash-ref table "msg_id")
(hash-ref table "msg_type") #:date (hash-ref table "date")
(hash-ref table "version") #:version (hash-ref table "version")
sender)))) #:sender sender))))
(define (header->string header) (define (header->string header)
"Return HEADER as a JSON string." "Return HEADER as a JSON string."
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment