Commit graph

1861 commits

Author SHA1 Message Date
Nathaniel Nicandro
0521bca637 Add shutdown and interrupt functions to a client
Somehow this was not done until now.  I had in my mind that shutting
down and interrupting a kernel was part of the responsibility of the
manager class.  This is how it is done in the reference
jupyter/jupyter_client implementation, I think.

* jupyter-client.el (jupyter-shutdown-kernel)
(jupyter-interrupt-kernel): New functions.

* jupyter-repl.el
(jupyter-repl-restart-kernel): Use the `jupyter-shutdown-kernel`
method to restart a client's kernel.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
790a29fe5d Reconnect clients after kernel restart 2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
20c1c80bc0 Remove jupyter-client-has-manager-p
* jupyter-client.el (jupyter-client-has-manager-p): Do it.
(initialize-instance)
* jupyter-repl.el
(jupyter-repl-kill-buffer-query-function)
* test/jupyter-test.el (jupyter-repl-client-predicates): Update all callers.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
c06b71a3f0 Remove unused function
* jupyter-repl.el (jupyter-repl-display-kernel-buffer): Do it.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
61d01dfcf1 Skip some tests 2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
805048f0f7 Pre-fix jupyter-connect-repl to allow tests to pass
Just to allow tests to pass at this commit
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
aa6826962d Fix manager slot check during tests
`(slot-boundp client 'manager)` always returns t since the `manager`
slot defaults to `nil` for every client.  To check for a manager we
need to check for a non-nil `manager` slot.

* test/test-helper.el
(jupyter-test-with-client-cache): Check for a non-nil `manager` slot.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
390a6c814e Replace jupyter-start-new-kernel calls with jupyter-client 2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
bdb8488bf3 Remove comm-layer and related
Remove `jupyter-channel-ioloop-comm.el`, `jupyter-comm-layer.el`, `jupyter-ioloop-comm.el`.
Remove the kcomm slot of a client, replace it with a kernel slot.
jupyter-server.el: Remove kernel communication related code.
Remove `jupyter-kernel-lifetime`.

* jupyter-kernel-manager.el (jupyter-kernel-lifetime): Do it. Update
  all subclasses. All methods it defined no longer take a type.  This
  is in preparation of removing most of them altogether.
(jupyter-kernel-manager): Update doc.

* jupyter-kernel-process-manager.el
(jupyter-kernel-process): Update documentation.
(jupyter--kernel-died-process-sentinel): Remove type check.

* test/jupyter-test.el (jupyter-local-tcp-conn-info): Remove.
(jupyter-kernel-lifetime): Remove.

[conn] Have `jupyter-kernel-manager.el` re-direct to the new interface

* jupyter-kernel-manager.el
(jupyter-kernel-alive-p): Check that the kernel is
live, not an Emacs connection to it.
(jupyter-start-kernel)
[jupyter-kernel, jupyter-kernel-manager]: Use `jupyter-launch`.
(jupyter-shutdown-kernel) [jupyter-kernel-manager]: Use `jupyer-shutdown`.
(jupyter-kill-kernel) [jupyter-kernel]: Ditto. Remove a method
definition.
(jupyter-make-client): Use `jupyter-client`.
(jupyter-interrupt-kernel) [jupyter-kernel-manager]: Use `jupyer-interrupt`.

* jupyter-kernel-process-manager.el: Remove.

Don't add finalizer to cleanup process.

Cleanup is done in other ways, e.g. in
`jupyter--gc-kernel-processes`.  Also it doesn't make sense to delete
a general kernel because the object that represents it in Emacs is no
longer accessible.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
707ebd189f Add jupyter-client method 2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
dbbaf8ed77 Remove the jupyter-server test
Since `jupyter-comm-layer` is being removed and `jupyter-server`s
behavior that involves kernel connections is being moved into an
implementation detail, see `jupyter-connection` and `jupyter-io` in
future commits.

* test/jupyter-server-test.el: Do it.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
7398b2c4f8 2/2 Implement new connection interface for kernel servers
This commit removes the `jupyter-comm-layer` dependency of
`jupyter-server`.

* jupyter-server.el
(jupyter-connection): Require.
(jupyter-ioloop-comm): Remove require.
(jupyter-server): Remove the superclass.  Add `ioloop` and `handlers`
slot to compensate.
(jupyter-gc-servers)
(jupyter-server-kernel-connected-p)
(jupyter-server--connect-channels)
(jupyter-server--disconnect-channels): Use the `ioloop` slot and
related methods instead of `jupyter-comm-layer` methods.
(jupyter-server--refresh-comm): Ditto, but replace the
`jupyter-comm-start` call with `jupyter-server--start-comm`.
(jupyter-event-handler): Transfer to `ioloop` slot initialization in
`jupyter-server--start-comm`.
(jupyter--server-event-handler): New type.  The `handlers` slot is
a list of these.
(jupyter-server--start-comm): Initialize `ioloop` slot.  All unhandled
events received on `ioloop` are passed to the handlers in `handlers`.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
cb43fe137a 1/2 Implement new connection interface for kernel servers
This commit has `jupyter-server-kernel-comm` use the new
`jupyter-connection` interface.

* jupyter-server.el
(jupyter-server-kernel-comm): Add `conn` slot.
(jupyter-server--connect-channels): Group with the new interface.
(jupyter-comm-start, jupyter-comm-add-handler)
(jupyter-comm-remove-handler) [jupyter-server]: Remove.
(jupyter-server--start-comm, jupyter--disconnect-channels)
(make-jupyter-server-connection): New functions.  What the
removed functions did, these do.
(jupyter-comm-start, jupyter-comm-id, jupyter-comm-stop)
(jupyer-send, jupyter-comm-alive-p) [jupyter-server-kernel-comm]:
Replace the body of these with their equivalent in the new interface.
In `jupyter-comm-start`, set the `conn` slot to the connect returned
by `make-jupyter-server-connection` before starting the connection.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
1ac3d658e9 Simplify connection file removal
This is mostly a refactor of the old behavior except now the removal
of connection files happens in `kill-emacs-hook` and also when
cleaning up dead kernel processes.

* jupyter-env.el
(jupyter-write-connection-file): Turn into a `defun`.  Remove the
connection file removal code.

* jupyter-kernel-process-manager.el
(jupyter--kernel-processes): Mention new form of elements in doc.
(jupyter-delete-connection-files): New function.  Add it to
`kill-emacs-hook` at top-level.
(jupyter--gc-kernel-processes):
(jupyter--start-kernel-processes): Use new form of
`jupyter--kernel-processes`.
(jupyter-local-tcp-conn-info): Relocate to...

* test/jupyter-test.el: ...here.
(jupyter-write-connection-file): Update test.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
47689ed0ce Return file name as directory
* jupyter-env.el (jupyter-runtime-directory): Do it.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
3fed81216e Cleanup dead kernel processes when starting new ones
* jupyter-kernel-process-manager.el
(jupyter--kernel-processes): New variable.
(jupyter--gc-kernel-processes): New function.
(jupyter--start-kernel-process): Use them.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
eafbc9f967 Merge jupyter-spec-kernel and jupyter-command-kernel
The merged class is `jupyter-kernel-process`.

The `jupyter-command-kernel` class existed to launch kernels using the
`jupyter kernel` shell command. This was done to support launching
kernels on remote systems via TRAMP. The work of generating a
connection info. file was offloaded to that command since it can find
out a set of open ports more reliably on a remote system than Emacs
can.

The only difference between the two classes was that the connection
info. was generated manually for a `jupyter-spec-kernel`, so now the
`jupyter kernel` command is relied on in that case too (via
`jupyter-session-with-random-ports` from
8ad90b887a4afa161d907056ae44db5b119dbc5d).

* jupyter-kernel-process-manager.el
(jupyter-env, jupyter-kernelspec): Require.
(jupyter--after-kernel-process-ready): Remove.
(jupyter--start-kernel-process): New function.
(jupyter-start-kernel)
[jupyter-kernel-process]: Use it, set the SESSION slot of a kernel
before doing so.
(jupyter-command-kernel, jupyter-spec-kernel): Remove. Also remove all
related methods.
(jupyter-start-new-kernel): Use `jupyer--kernel-process` to replace
calls to the removed classes.

* test/jupyter-test.el
(jupyter-kernel-lifetime)
(jupyter-command-kernel): Replace references to removed classes with
`jupyter--kernel-process`
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
4164f13bdc Remove jupyter-hb-comm
* jupyter-channel-ioloop-comm.el
(jupyter-channel-ioloop-comm): Remove it from the super-class list.
Add a `hb` slot to compensate.
(jupyter-hb-beating-p, jupyter-hb-pause)
(jupyter-hb-unpause): Relocated from...

* jupyter-comm-layer.el: ...here.

* jupyter-client.el (jupyter-hb-beating-p, jupyter-hb-pause)
(jupyter-hb-unpause): Check for a `hb` slot instead of it.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
93eeda42a6 Replace jupyter-channel-ioloop-comm internals with new impl.
* jupyter-comm-layer.el
(jupyter-comm-initialize): Remove default method.  This is in
preparation for moving over to classless communication.

* jupyter-channel-ioloop-comm (jupyter-connection): Require.
(jupyter--proxy-channel): New type.
(jupyter--make-channel-group, jupyter--channel-alive-p)
(jupyter--start-channel, jupyter--stop-channel)
(make-jupyter-async-connection): New functions.
(jupyter-channel-ioloop-comm): Remove `ioloop-class` slot, update all
callers. Remove `channels` slot, update all setters and
references. Add `conn` slot which holds a `jupyter-connection`.
(jupyter-comm-initialize): Initialize the `conn` slot to the
connection returned by `make-jupyter-async-connection`.
(jupyter-comm-start, jupyter-comm-stop)
(jupyter-comm-alive-p, jupyter-comm-id, jupyter-channel-alive-p)
(jupyter-stop-channel, jupyter-start-channel): Replace the body of these
functions with their equivalents in `conn`.

* jupyter-kernel-process-manager.el
(jupyter-make-client): Update `jupyter-channel-ioloop-comm` call.

* jupyter-repl.el:
(jupyter-connect-repl): Ditto.

* test/test-helper.el
(initialize-instance) [jupyter-echo-client]: Ditto. Replace setting of
`channel` slot with setting the `conn` clot.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
7a11c8f0ac Add jupyter-connection.el
This file holds the `jupyter-connection` type which is an intermediate
successor of `jupyter-comm-layer` on the way towards more functional
kernel I/O.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
95f3e150b4 Add jupyter-kernel.el and related
The new files added in this commit will eventually replace the manager
and kernel classes and favor struct types to represent kernels instead
of classes.  A kernel manager was a concept ripped from the
jupyter/jupyter_client reference implementation.

In Emacs the concept makes the client implementation more complicated
and is replaced by functions that manage the lifetime of a kernel:
`jupyter-launch`, `jupyter-shutdown`, and `jupyter-interrupt`.

* jupyter-kernel-manager.el
* jupyter-kernel-process-manager.el
* jupyter-server.el
* test/jupyter-server-test.el
* test/test-helper.el: Make `jupyter-kernel`, `jupyter-server-kernel`,
  and `jupyter-kernel-process` private classes.  The new files below
  use the public names, but as structs now.

* jupyter-kernel.el
* jupyter-kernel-process.el
* jupyter-server-kernel.el: New files.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
cc60e6abe2 Ensure all tests that rely on the REST API have a server tag
* test/jupyter-server-test.el: Do it.

* test/jupyter-tramp-test.el: Do it.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
b6f89c96dd Cleanup documentation
* jupyter-rest-api (jupyter-api-kernel-websocket): Do it.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
591d51c9bc jupyter-api-get-kernel-ws -> jupyter-api-kernel-websocket
* jupyter-rest-api.el: Do it.

* test/jupyter-server-test.el: Do it.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
ffcfdb6179 Let the server validate the kernelspec
* jupyter-server.el (jupyter-server--verify-kernelspec):
  Remove. Update all callers.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
07d444021a Add functions to aid in launching kernel processes
This is in preparation for merging `jupyter-command-kernel` and
`jupyter-spec-kernel` into just the `jupyter-kernel-process` class.

* jupyter-env.el
(jupyer-session-with-random-ports): New function.

* jupyter-kernelspec.el
(jupyter-expand-environment-variables)
(jupyter-process-environment)
(jupyter-kernel-argv): New functions.

* test/jupyter-test.el
(jupyer-session-with-random-ports)
(jupyter-expand-environment-variables)
(jupyter-process-environment)
(jupyter-kernel-argv): New tests.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
f7292ed195 Add a jupyter-kernelspec struct
* jupyter-kernel-manager.el (jupyter-kernel): Set `spec` slot type to
  `jupyter-kernelspec`.
(jupyter-kernel-name):
* jupyter-kernel-process-manager.el: Update all accessors.

* jupyter-kernelspec.el: Add `jupyter-kernelspec` struct type.
* jupyter-repl.el:
* jupyter-server.el:
* ob-jupyter.el:
* test/test-helper.el: Update all kernelspec using functions to use
  the new type.
2023-02-13 20:21:11 -06:00
Nathaniel Nicandro
eebeef1293 Don't attempt to byte-compile a symbol whose function value is a subr
`(byte-compile #'cons)` will return `t` which is not a function.
Passing in the `subr` to `byte-compile` will just return the `subr` so
everything works out OK.  This is an attempt at fixing issues like
nnicandro/emacs-zmq#32.
2023-02-13 20:15:56 -06:00
Nathaniel Nicandro
85f6ff84ed jupyter-org--coalesce-stream-results: Refactor 2023-02-13 20:15:56 -06:00
Ahmed Shariff
bc3b78be43 Fix available-kernelspecs failing from warning output 2023-02-04 20:24:55 -06:00
Jürgen Hötzel
4d62a88951 Use TRAMP handler for access-file
Fixes file-not-found error when using `dired`.
2023-02-04 20:18:06 -06:00
Nathaniel Nicandro
16cbda7916 Fix incorrect number of arguments 2023-02-04 16:58:12 -06:00
Nathaniel Nicandro
14899664b6 Don't use zmq-recv-decoded and zmq-send-encoded
These functions no longer exist in newer versions of `emacs-zmq`.
Closes #433.
2023-02-03 20:12:32 -06:00
Nathaniel Nicandro
3736ae753a Prevent leaking ANSI sequences when inserting text/plain results
* jupyter-mime.el (jupyter-insert): Do it.
2023-02-03 20:12:32 -06:00
Nathaniel Nicandro
2e8689227f Add a way to debug the message stream from a kernel
Previously it was hard to step through the code whenever messages were
coming in live from a kernel due to the asynchronous nature of
handling process output in Emacs.
2023-02-03 20:12:32 -06:00
Tomasz Mieszkowski
7d20c0aee2 Sort available kernelspecs; guess with ^; use guessing in jupyter-run-repl 2022-04-19 13:52:23 -05:00
Mikhail Rudenko
0a7055d7b1 Add optional image width limiting for REPL 2022-02-11 20:10:00 -06:00
Nathaniel Nicandro
42a9765897 Fix typo
Change `target-name` to `target_name`
2022-01-05 13:43:26 -06:00
Nathaniel Nicandro
df343af5e9 Remove .github/FUNDING.yml 2021-12-09 21:20:52 -06:00
Nathaniel Nicandro
c702f1e90f Add some helper functions for Org tests 2021-11-30 10:47:53 -06:00
Nathaniel Nicandro
03a4f688cc Remove superfluous and 2021-11-30 10:47:53 -06:00
Nathaniel Nicandro
113acb239d Use default-directory as :dir when executing a source block
`default-directory` is already set according to :dir when executing a
source block. This prevents `org-babel-expand-body:jupyter` from
attempting to re-expand the value of the :dir header argument.
See #302.
2021-11-30 10:47:53 -06:00
Nathaniel Nicandro
db450a9f44 Ignore ANSI escapes when testing some Org source blocks 2021-11-30 10:47:53 -06:00
Nathaniel Nicandro
a2cbebdfda Use a raw string in :dir handling code for Python source blocks 2021-11-30 10:47:53 -06:00
Nathaniel Nicandro
724d03ea20 Install pandoc during tests on Travis and Appveyor 2021-11-30 10:46:52 -06:00
Nathaniel Nicandro
4729e21184 Fix output order when using pandoc conversion
Fixes #351.
2021-11-30 10:46:52 -06:00
Tomasz Mieszkowski
57306bf385 Make scratch buffers easier to identify 2021-11-27 21:47:25 -06:00
Tomasz Mieszkowski
7b64e18144 Update dev, remove init target in Makefile (no --dev option) 2021-11-27 21:32:28 -06:00
Nathaniel Nicandro
0dec83f7e4 Remove a usage of thread-first and thread-last
This usage broke debugging of org-babel-execute:jupyter.
2021-11-20 20:32:35 -06:00
Nathaniel Nicandro
162d047e62 Add section on how to run tests in README
Closes #239
2021-11-20 10:53:09 -06:00