From 86beb065db3850657155ae3267265d13d6a54953 Mon Sep 17 00:00:00 2001 From: Nathaniel Nicandro Date: Mon, 22 Feb 2021 20:11:22 -0600 Subject: [PATCH] jupyter-kernel: Move conn-info key handling to "process" :extra method --- jupyter-kernel-process.el | 51 +++++++++++++++++++++++++++------------ jupyter-kernel.el | 26 +------------------- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/jupyter-kernel-process.el b/jupyter-kernel-process.el index 88d0af5..01d74ab 100644 --- a/jupyter-kernel-process.el +++ b/jupyter-kernel-process.el @@ -72,22 +72,43 @@ Return nil if KERNEL does not have an associated process." (apply #'make-jupyter-kernel-process args)) (cl-defmethod jupyter-kernel :extra "process" (&rest args) - "Return a representation of a kernel based on an Emacs process. -If ARGS contains a :spec key, return a `jupyter-kernel-process' -initialized using ARGS. If the value is the name of a -kernelspec, the returned kernel's spec slot will be set to the -corresponding `jupyter-kernelspec'. The session of the returned -kernel will be initialized with the return value of -`jupyter-session-with-random-ports'. + "Return a kernel as an Emacs process. +If ARGS contains a :spec key with a value being a +`jupyter-kernelspec', a `jupyter-kernel-process' initialized from +it will be returned. The value can also be a string, in which +case it is considered the name of a kernelspec to use. -Call the next method if ARGS does not contain :spec." - (let ((spec (plist-get args :spec))) - (if (not spec) (cl-call-next-method) - (when (stringp spec) - (plist-put args :spec - (or (jupyter-guess-kernelspec spec) - (error "No kernelspec matching name (%s)" spec)))) - (apply #'jupyter-kernel-process args)))) +If ARGS contains a :conn-info key, a `jupyter-kernel-process' +with a session initialized from its value, either the name of a +connection file to read or a connection property list itself (see +`jupyter-read-connection'), will be returned. The remaining ARGS +will be used to initialize the returned kernel. + +Call the next method if ARGS does not contain a :spec or +: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 diff --git a/jupyter-kernel.el b/jupyter-kernel.el index 08e7783..fab7b48 100644 --- a/jupyter-kernel.el +++ b/jupyter-kernel.el @@ -62,33 +62,9 @@ (cl-defgeneric jupyter-kernel (&rest 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 -purposes of handling different forms of ARGS that do not just -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))))) +purposes of handling different forms of ARGS.") ;;; Kernel management