Commit c98bb877 authored by Ludovic Courtès's avatar Ludovic Courtès
Browse files

Use 'match' to match on all the magic commands.

* guix-jupyter-kernel.scm (reply-execute-request): Use 'match' to
dispatch among all the magic, getting rid of (guix jupyter magic).
parent e58b8b27
......@@ -44,7 +44,6 @@
(guix jupyter logging)
(guix jupyter proxy)
(guix jupyter kernel)
(guix jupyter magic)
(guix jupyter environment))
(define session-id (random (* 255 255)
......@@ -216,76 +215,71 @@ to KERNEL as a reply to MESSAGE, and return STATE suitably adjusted."
(define (reply-execute-request kernel kind message state)
(let* ((content (message-content message))
(code (assoc-ref (json-string->scm content) "code"))
(magic (get-magic-line code))
(line (string-take code
(or (string-index code #\newline)
(string-length code))))
(count (proxy-state-execution-count state)))
(cond
((magic-env? code)
(pub-busy kernel message)
(match (string-tokenize magic)
((_ "environment" name "<-" specs ...)
(match (lookup-proxied name state)
(#f
(create-environment name specs state
#:kernel kernel
#:message message))
((? kernel? proxy)
(format/log "terminating existing '~a' environment~%"
name)
(unmonitor-client proxy)
(terminate-proxied-kernel proxy)
(create-environment name specs
(unregister-proxied name state)
#:kernel kernel
#:message message))))
((_ "environment" name)
(match (lookup-proxied name state)
(#f
(reply-html kernel message
(sxml->html-string
`(bold "Unknown environment "
(code ,name) "."))
count)
(increment-execution-count state))
((? kernel? proxy)
(format/log "evaluating code in container ~s (PID ~s)~%"
name (kernel-pid proxy))
(send-message proxy message)
(let ((state (increment-execution-count state)))
(set-proxy-state-default-environment state name)))))
((lst ...)
(reply-html kernel message
(sxml->html-string
`(bold "Invalid " (code "guix environment")
" magic."))
count)
(increment-execution-count state))))
((magic-html? code)
(reply-html kernel message (delete-magic code) count)
(increment-execution-count state))
((proxy-state-default-environment state)
=>
(lambda (environment)
(let ((proxied (lookup-proxied environment state)))
(format/log "evaluating code in environment ~s (PID ~s)~%"
environment (kernel-pid proxied))
(send-message proxied message
#:kernel-socket kind)
(increment-execution-count state))))
(else
(reply-html kernel message
(sxml->html-string
`(div "You have not selected an execution environment
(pub-busy kernel message)
(match (string-tokenize line)
((";;guix" "environment" name "<-" specs ...)
(match (lookup-proxied name state)
(#f
(create-environment name specs state
#:kernel kernel
#:message message))
((? kernel? proxy)
(format/log "terminating existing '~a' environment~%"
name)
(unmonitor-client proxy)
(terminate-proxied-kernel proxy)
(create-environment name specs
(unregister-proxied name state)
#:kernel kernel
#:message message))))
((";;guix" "environment" name)
(match (lookup-proxied name state)
(#f
(reply-html kernel message
(sxml->html-string
`(bold "Unknown environment "
(code ,name) "."))
count)
(increment-execution-count state))
((? kernel? proxy)
(format/log "evaluating code in container ~s (PID ~s)~%"
name (kernel-pid proxy))
(send-message proxy message)
(let ((state (increment-execution-count state)))
(set-proxy-state-default-environment state name)))))
((";;guix" _ ...)
(reply-html kernel message
(sxml->html-string
`(bold "Invalid " (code "guix") " magic."))
count)
(increment-execution-count state))
(_
(match (proxy-state-default-environment state)
((? string? environment)
(let ((proxied (lookup-proxied environment state)))
(format/log "evaluating code in environment ~s (PID ~s)~%"
environment (kernel-pid proxied))
(send-message proxied message #:kernel-socket kind)
(increment-execution-count state)))
(#f
(reply-html kernel message
(sxml->html-string
`(div "You have not selected an execution environment
yet. You can create one by entering a “magic command” in a cell as follows:"
(p (code ";;guix environment my-environment <- \
(p (code ";;guix environment my-environment <- \
python-ipython python-ipykernel"))
"You can then select a kernel and hand it code
"You can then select a kernel and hand it code
to execute:"
(p (pre
(code
";;guix kernel my-environment ipython\n"
"2 + 2")))))
count)
state))))
(p (pre
(code
";;guix kernel my-environment ipython\n"
"2 + 2")))))
count)
state))))))
(define (shutdown kernel kind message state)
(format/log "shutting down ~a containers~%"
......
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