jupyter-kernel: Move conn-info key handling to "process" :extra method

This commit is contained in:
Nathaniel Nicandro 2021-02-22 20:11:22 -06:00
parent 5a9a068734
commit 86beb065db
2 changed files with 37 additions and 40 deletions

View file

@ -72,22 +72,43 @@ Return nil if KERNEL does not have an associated process."
(apply #'make-jupyter-kernel-process args)) (apply #'make-jupyter-kernel-process args))
(cl-defmethod jupyter-kernel :extra "process" (&rest args) (cl-defmethod jupyter-kernel :extra "process" (&rest args)
"Return a representation of a kernel based on an Emacs process. "Return a kernel as an Emacs process.
If ARGS contains a :spec key, return a `jupyter-kernel-process' If ARGS contains a :spec key with a value being a
initialized using ARGS. If the value is the name of a `jupyter-kernelspec', a `jupyter-kernel-process' initialized from
kernelspec, the returned kernel's spec slot will be set to the it will be returned. The value can also be a string, in which
corresponding `jupyter-kernelspec'. The session of the returned case it is considered the name of a kernelspec to use.
kernel will be initialized with the return value of
`jupyter-session-with-random-ports'.
Call the next method if ARGS does not contain :spec." If ARGS contains a :conn-info key, a `jupyter-kernel-process'
(let ((spec (plist-get args :spec))) with a session initialized from its value, either the name of a
(if (not spec) (cl-call-next-method) connection file to read or a connection property list itself (see
(when (stringp spec) `jupyter-read-connection'), will be returned. The remaining ARGS
(plist-put args :spec will be used to initialize the returned kernel.
(or (jupyter-guess-kernelspec spec)
(error "No kernelspec matching name (%s)" spec)))) Call the next method if ARGS does not contain a :spec or
(apply #'jupyter-kernel-process args)))) :conn-info key."
(if (plist-get args :server) (cl-call-next-method)
(let ((spec (plist-get args :spec))
(conn-info (plist-get args :conn-info)))
(cond
(spec
(when (stringp spec)
(plist-put args :spec
(or (jupyter-guess-kernelspec spec)
(error "No kernelspec matching name (%s)" spec))))
(cl-check-type (plist-get args :spec) jupyter-kernelspec)
(apply #'jupyter-kernel-process args))
(conn-info
(when (stringp conn-info)
(setq conn-info (jupyter-read-connection conn-info)))
(apply #'jupyter-kernel-process
:session (jupyter-session
:conn-info conn-info
:key (plist-get conn-info :key))
(cl-loop
for (k v) on args by #'cddr
unless (eq k :conn-info) collect k and collect v)))
(t
(cl-call-next-method))))))
;;; Client connection ;;; Client connection

View file

@ -62,33 +62,9 @@
(cl-defgeneric jupyter-kernel (&rest args) (cl-defgeneric jupyter-kernel (&rest args)
"Return a kernel constructed from ARGS. "Return a kernel constructed from ARGS.
ARGS are keyword arguments used to initialize the returned
kernel.
The default implementation will return a `jupyter-kernel' with a
session initialized from the value of :conn-info in ARGS, either
the name of a connection file to read or itself a connection
property list (see `jupyter-read-connection'). A client can
connect to the returned kernel using `jupyter-client'.
This method can be extended with extra primary methods for the This method can be extended with extra primary methods for the
purposes of handling different forms of ARGS that do not just purposes of handling different forms of ARGS.")
need the default behavior."
(let ((conn-info (plist-get args :conn-info)))
(cond
(conn-info
(when (stringp conn-info)
(setq conn-info (jupyter-read-connection conn-info)))
;; TODO: A `jupyter-io' method that handles this case.
(apply #'make-jupyter-kernel
:session (jupyter-session
:conn-info conn-info
:key (plist-get conn-info :key))
(cl-loop
for (k v) on args by #'cddr
unless (eq k :conn-info) collect k and collect v)))
(t
(cl-call-next-method)))))
;;; Kernel management ;;; Kernel management