Skip to content

Commit ee8b518

Browse files
committed
Make ZMQ an optional user dependency
* jupyter-server.el (jupyter-ioloop, jupyter-server-ioloop): Remove requires. (jupyter-server): Don't subclass `jupyter-ioloop-comm`. (jupyter-server--connect-channels, jupyter-server--refresh-comm): Only do something when the SERVER argument is a `jupyter-comm-layer`, i.e. a `jupyter-server-ioloop-comm`. We don't check for that class directly since we would end up requiring ZMQ. (jupyter-comm-start) [jupyter-server-kernel-manager]: Use the right subclass of `jupyter-server-abstract-kcomm` depending on if the `jupyter-server` object is using ZMQ for communication. (jupyter-server-make-instance): New function. Returns a `jupyter-server` instance (a `jupyter-server-ioloop-comm`) that uses ZMQ if `jupyter-server-use-zmq` is non-nil, and a plain old `jupyter-server` instance otherwise. (jupyter-current-server): `jupyter-server` -> `jupyter-server-make-instance`. * ob-jupyter.el (org-babel-jupyter--server-repl): `jupyter-server` -> `jupyter-server-make-instance`. * test/test-helper.el (jupyter-test-with-notebook): `jupyter-server` -> `jupyter-server-make-instance`.
1 parent 8883a66 commit ee8b518

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

jupyter-server.el

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@
7777
(require 'jupyter-repl)
7878
(require 'jupyter-rest-api)
7979
(require 'jupyter-kernel-manager)
80-
(require 'jupyter-ioloop-comm)
81-
(require 'jupyter-server-ioloop)
8280

8381
(declare-function jupyter-tramp-file-name-p "jupyter-tramp" (filename))
8482
(declare-function jupyter-tramp-server-from-file-name "jupyter-tramp" (filename))
@@ -109,7 +107,6 @@ Used in, e.g. a `jupyter-server-kernel-list-mode' buffer.")
109107
;; `jupyter-server-client' since it isn't a representation of a server, but a
110108
;; communication channel with one.
111109
(defclass jupyter-server (jupyter-rest-client
112-
jupyter-ioloop-comm
113110
eieio-instance-tracker)
114111
((tracking-symbol :initform 'jupyter--servers)
115112
(kernelspecs
@@ -265,17 +262,21 @@ ID of the kernel will be associated with NAME, see
265262

266263
(cl-defgeneric jupyter-server-kernel-connected-p ((client jupyter-server) id)
267264
"Return non-nil if CLIENT can communicate with a kernel that has ID.")
265+
266+
;; TODO: Move the following two functions to jupyter-server-ioloop-comm.el
268267
(defun jupyter-server--connect-channels (server id)
269-
(jupyter-send server 'connect-channels id)
270-
(jupyter-with-timeout
271-
(nil jupyter-default-timeout
272-
(error "Timeout when connecting websocket to kernel id %s" id))
273-
(jupyter-server-kernel-connected-p server id)))
268+
(when (object-of-class-p server 'jupyter-comm-layer)
269+
(jupyter-send server 'connect-channels id)
270+
(jupyter-with-timeout
271+
(nil jupyter-default-timeout
272+
(error "Timeout when connecting websocket to kernel id %s" id))
273+
(jupyter-server-kernel-connected-p server id))))
274274

275275
(defun jupyter-server--refresh-comm (server)
276276
"Stop and then start SERVER communication.
277277
Reconnect the previously connected kernels when starting."
278-
(when (jupyter-comm-alive-p server)
278+
(when (and (object-of-class-p server 'jupyter-comm-layer)
279+
(jupyter-comm-alive-p server))
279280
(let ((connected (cl-remove-if-not
280281
(apply-partially #'jupyter-server-kernel-connected-p server)
281282
(mapcar (lambda (kernel) (plist-get kernel :id))
@@ -408,7 +409,10 @@ MANAGER's COMM slot will be set to the `jupyter-comm-layer'
408409
receiving events on the websocket when this method returns."
409410
(with-slots (kernel comm) manager
410411
(unless (slot-boundp manager 'comm)
411-
(oset manager comm (jupyter-server-kernel-comm :kernel kernel)))
412+
(oset manager comm
413+
(if (object-of-class-p (oref kernel server) 'jupyter-comm-layer)
414+
(jupyter-server-ioloop-kernel-comm :kernel kernel)
415+
(jupyter-server-kernel-comm :kernel kernel))))
412416
(unless (jupyter-comm-alive-p comm)
413417
(jupyter-comm-start comm))))
414418

@@ -508,6 +512,18 @@ least the following keys:
508512
(define-error 'jupyter-server-non-existent
509513
"The server doesn't exist")
510514

515+
(defun jupyter-server-make-instance (&rest args)
516+
"Return a different subclass of `jupyter-server' depending on `jupyter-server-use-zmq'.
517+
A `jupyter-server-ioloop-comm' object is returned if
518+
`jupyter-server-use-zmq' is non-nil, a `jupyter-server' is
519+
returned otherwise. ARGS is passed to the `make-instance'
520+
invocation for the subclass."
521+
(if jupyter-server-use-zmq
522+
(progn
523+
(require 'jupyter-server-ioloop-comm)
524+
(apply #'jupyter-server-ioloop-comm args))
525+
(apply #'jupyter-server args)))
526+
511527
(defun jupyter-current-server (&optional ask)
512528
"Return an existing `jupyter-server' or ASK for a new one.
513529
If ASK is non-nil, always ask for a URL and return the
@@ -546,7 +562,7 @@ a URL."
546562
(setf (url-type u) "ws")
547563
(url-recreate-url u)))))
548564
(or (jupyter-find-server url ws-url)
549-
(let ((server (jupyter-server :url url :ws-url ws-url)))
565+
(let ((server (jupyter-server-make-instance :url url :ws-url ws-url)))
550566
(if (jupyter-api-server-exists-p server) server
551567
(delete-instance server)
552568
(signal 'jupyter-server-non-existent (list url)))))))))

ob-jupyter.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ variables in PARAMS."
239239
(require 'jupyter-server)
240240
(let* ((url (jupyter-tramp-url-from-file-name session))
241241
(server (or (jupyter-tramp-server-from-file-name session)
242-
(jupyter-server :url url)))
242+
(jupyter-server-make-instance :url url)))
243243
(localname (file-local-name session))
244244
(name-or-id (if (null localname) (error "No remote server session name")
245245
;; If a kernel has an associated name, get its kernel ID

test/test-helper.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ For `url-retrieve', the callback will be called with a nil status."
323323
`(let* ((host (format "localhost:%s" (jupyter-test-ensure-notebook-server)))
324324
(url (format "http://%s" host))
325325
(,server (or (jupyter-find-server url)
326-
(jupyter-server :url url))))
326+
(jupyter-server-make-instance :url url))))
327327
,@body))
328328

329329
(defmacro jupyter-test-with-server-kernel (server name kernel &rest body)

0 commit comments

Comments
 (0)