Commit 0bd4ecd7 authored by Ludovic Courtès's avatar Ludovic Courtès
Browse files

Switch to Guile-JSON 3.x.

Guile-JSON 3.x is incompatible with Guile-JSON 1.x, which we relied on
until now: it maps JSON dictionaries to alists (instead of hash tables),
and JSON arrays to vectors (instead of lists).  This commit is about
adjusting all the existing code to this new mapping.

* jupyter/kernels.scm (kernel-arguments): Use 'assoc-ref' instead of
'hash-ref'; pass JSON array through 'vector->list'.
(relay-message, reply-html): Likewise.
* jupyter/messages.scm (string->header): Likewise.
* guix-jupyter-container.scm (local-eval, reply-execute-request): Likewise.
* guix-jupyter-kernel.scm (reply-execute-request): Likewise.
* guix-kernel/jupyter-client.scm (json->kernel): Likewise.
* tests/kernels.scm: Likewise.
* environment.scm (propagated-inputs): Replace GUILE-JSON with
GUILE-JSON-3.
parent 200be24b
......@@ -89,7 +89,7 @@
`(("guix" ,guix)
("guile" ,guile-2.2)))
(propagated-inputs
`(("guile-json" ,guile-json)
`(("guile-json" ,guile-json-3)
("guile-simple-zmq" ,guile-simple-zmq)
("guile-gcrypt" ,guile-gcrypt)))
......
......@@ -95,14 +95,14 @@ KEY for signing."
(let* ((scontent (json-string->scm (message-content message)))
(sender (header-sender (message-header message)))
(code (string-append "(begin "
(delete-magic (hash-ref scontent "code"))
(delete-magic (assoc-ref scontent "code"))
")"))
(silent (hash-ref scontent "silent"))
(store-history (hash-ref scontent "store_history"))
(user-expressions (hash-ref scontent "user_expressions"))
(allow-stdin (hash-ref scontent "allow_stdin"))
(stop-on-error (hash-ref scontent "stop_on_error"))
(empty-object (make-hash-table 1))
(silent (assoc-ref scontent "silent"))
(store-history (assoc-ref scontent "store_history"))
(user-expressions (assoc-ref scontent "user_expressions"))
(allow-stdin (assoc-ref scontent "allow_stdin"))
(stop-on-error (assoc-ref scontent "stop_on_error"))
(empty-object '())
(counter (+ count 1))) ;Execution counter
(let-values (((err err-key evalue stacktrace result)
(catch #t
......@@ -126,7 +126,7 @@ KEY for signing."
(lambda () (display key))))
(evalue (with-output-to-string
(lambda () (display parameters))))
(stacktrace (list err-key evalue))
(stacktrace (vector err-key evalue))
(result ""))
(values err err-key evalue
stacktrace result))))))
......@@ -218,8 +218,8 @@ KEY for signing."
(values kernels count))
(define (reply-execute-request socket message kernels count)
(let ((code (hash-ref (json-string->scm (message-content message))
"code")))
(let ((code (assoc-ref (json-string->scm (message-content message))
"code")))
(cond
((magic-env? code)
(values kernels
......
;;; Guix-kernel -- Guix kernel for Jupyter
;;; Copyright (C) 2018 Evgeny Panfilov <epanfilov@gmail.com>
;;; Copyright (C) 2018 Pierre-Antoine Rouby <pierre-antoine.rouby@inria.fr>
;;; Copyright (C) 2018 Inria
;;; Copyright (C) 2018, 2019 Inria
;;;
;;; 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
......@@ -112,7 +112,7 @@ acts as a proxy to an actual kernel."
(define (reply-execute-request kernel message containers count)
(let* ((content (message-content message))
(code (hash-ref (json-string->scm content) "code"))
(code (assoc-ref (json-string->scm content) "code"))
(magic (get-magic-line code)))
(catch 'guix-kernel
(λ ()
......
;;; Guix-kernel -- Guix kernel for Jupyter
;;; Copyright (C) 2018 Pierre-Antoine Rouby <pierre-antoine.rouby@inria.fr>
;;; Copyright (C) 2018 Inria
;;; Copyright (C) 2018, 2019 Inria
;;;
;;; 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
......@@ -36,9 +36,9 @@
<kernel> record."
;; See <https://jupyter-client.readthedocs.io/en/stable/kernels.html#connection-files>.
(let* ((table (json->scm port))
(ip (hash-ref table "ip"))
(scheme (hash-ref table "transport"))
(key (hash-ref table "key"))
(ip (assoc-ref table "ip"))
(scheme (assoc-ref table "transport"))
(key (assoc-ref table "key"))
(context (zmq-create-context)))
(define (port->uri port)
(string-append scheme "://" ip ":"
......@@ -53,15 +53,15 @@
(kernel name (getpid)
#:key (string->utf8 key)
#:control
(make-socket ZMQ_ROUTER (hash-ref table "control_port"))
(make-socket ZMQ_ROUTER (assoc-ref table "control_port"))
#:shell
(make-socket ZMQ_ROUTER (hash-ref table "shell_port"))
(make-socket ZMQ_ROUTER (assoc-ref table "shell_port"))
#:standard-input
(make-socket ZMQ_ROUTER (hash-ref table "stdin_port"))
(make-socket ZMQ_ROUTER (assoc-ref table "stdin_port"))
#:heartbeat
(make-socket ZMQ_REP (hash-ref table "hb_port"))
(make-socket ZMQ_REP (assoc-ref table "hb_port"))
#:iosub
(make-socket ZMQ_PUB (hash-ref table "iopub_port")))))
(make-socket ZMQ_PUB (assoc-ref table "iopub_port")))))
(define guile-version (effective-version))
......
......@@ -166,11 +166,11 @@ sockets."
(define (kernel-arguments kernel-file connection-file)
"Return the argument list read from KERNEL-FILE, substituting references to
'connection-file' with CONNECTION-FILE."
(let ((table (call-with-input-file kernel-file json->scm)))
(let ((alist (call-with-input-file kernel-file json->scm)))
(map (match-lambda
("{connection_file}" connection-file)
(x x))
(hash-ref table "argv"))))
(vector->list (assoc-ref alist "argv")))))
(define search-path->list
(let ((not-colon (char-set-complement (char-set #\:))))
......@@ -326,7 +326,7 @@ KERNEL."
(loop))
("status"
(let ((content (json-string->scm (message-content reply))))
(match (hash-ref content "execution_state")
(match (assoc-ref content "execution_state")
("idle" reply)
(_ (loop)))))
(_
......@@ -350,9 +350,9 @@ KERNEL."
(define (reply-html kernel message html count)
"Reply to MESSAGE with HTML."
(let ((code (hash-ref (json-string->scm (message-content message))
"code"))
(empty-object (make-hash-table 1))
(let ((code (assoc-ref (json-string->scm (message-content message))
"code"))
(empty-object '())
(counter (+ count 1)) ;execution counter
(send (lambda (socket type content)
......
......@@ -163,16 +163,17 @@ This is a low-level procedure for internal use."
(define* (string->header str #:optional sender)
"Read the JSON dictionary in STR and return the corresponding <header>
record. Return #f if STR is the empty dictionary."
(let ((table (json-string->scm str)))
(if (zero? (hash-count (const #t) table))
#f
(header (hash-ref table "msg_type")
(hash-ref table "username")
(hash-ref table "session")
#:id (hash-ref table "msg_id")
#:date (hash-ref table "date")
#:version (hash-ref table "version")
#:sender sender))))
(match (json-string->scm str)
(()
#f)
((alist ...)
(header (assoc-ref alist "msg_type")
(assoc-ref alist "username")
(assoc-ref alist "session")
#:id (assoc-ref alist "msg_id")
#:date (assoc-ref alist "date")
#:version (assoc-ref alist "version")
#:sender sender))))
(define (header->string header)
"Return HEADER as a JSON string."
......
......@@ -47,9 +47,9 @@
(let ((greetings (read-message %kernel 3000)))
(or (not greetings)
(list (message-type greetings)
(hash-ref (json-string->scm
(message-content greetings))
"execution_state"))))))))
(assoc-ref (json-string->scm
(message-content greetings))
"execution_state"))))))))
(unless %kernel (test-skip 1))
(test-equal "kernel_info_request"
......@@ -65,8 +65,8 @@
(string=? "status" (message-type reply1))
(string=? "busy"
(hash-ref (json-string->scm (message-content reply1))
"execution_state"))
(assoc-ref (json-string->scm (message-content reply1))
"execution_state"))
(equal? (message-parent-header reply1) (message-header request))
(string=? "kernel_info_reply" (message-type reply2))
......@@ -74,14 +74,14 @@
(string=? "status" (message-type reply3))
(string=? "idle"
(hash-ref (json-string->scm (message-content reply3))
"execution_state"))
(assoc-ref (json-string->scm (message-content reply3))
"execution_state"))
(equal? (message-parent-header reply3) (message-header request))
(hash-ref (hash-ref
(json-string->scm (message-content reply2))
"language_info")
"name")))))
(assoc-ref (assoc-ref
(json-string->scm (message-content reply2))
"language_info")
"name")))))
(unless %kernel (test-skip 1))
(test-equal "execute_request"
......@@ -106,8 +106,8 @@
(string=? "status" (message-type reply1))
(string=? "busy"
(hash-ref (json-string->scm (message-content reply1))
"execution_state"))
(assoc-ref (json-string->scm (message-content reply1))
"execution_state"))
(equal? (message-parent-header reply1) (message-header request))
(string=? "execute_input" (message-type reply2))
......@@ -116,8 +116,8 @@
(string=? (message-type reply3) "execute_result")
(equal? (message-parent-header reply3) (message-header request))
(let* ((content (json-string->scm (message-content reply3)))
(data (hash-ref content "data"))
(text (hash-ref data "text/plain")))
(data (assoc-ref content "data"))
(text (assoc-ref data "text/plain")))
(string->number text))))))
(when %kernel
......
Markdown is supported
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