Commit graph

207 commits

Author SHA1 Message Date
Nathaniel Nicandro
e133c34707 Add do prefix to launch,shutdown,interrupt functions of a kernel
launch,shutdown,interrupt are now monadic functions.
2023-02-13 20:22:50 -06:00
Nathaniel Nicandro
dcf2e2ef6e Remove jupyter-finalized-object 2023-02-13 20:22:50 -06:00
Nathaniel Nicandro
8f9f563afc Fix test 2023-02-13 20:22:49 -06:00
Nathaniel Nicandro
2d807d8670 Replace uses of jupyter-clients
This function is used elsewhere.
2023-02-13 20:22:49 -06:00
Nathaniel Nicandro
b5fac750e3 Remove setting of conn slot of a client in tests 2023-02-13 20:22:49 -06:00
Nathaniel Nicandro
4393e8adb0 Update tests for jupyter-kernel-process 2023-02-13 20:22:49 -06:00
Nathaniel Nicandro
7a52f6240d Update jupyter-kernel-process test 2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
e111aba096 Move jupyter-(dis)?connect into the jupyter-connection interface
The former pair of methods were only used to connect clients and
kernels.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
348df7feb5 Remove jupyter-send-* methods
These methods were not used much internally.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
82e5a0fbc2 Remove jupyter-server-kernel-connected-p
This was mainly used when having to refresh the websocket connections
to a server after a REST request.  Refreshing the websocket
connections is now handled internally to the connection of a
`jupyter-server`.

* jupyter-server-kernel.el
(initialize-instance): Add `kernel-ids` variable, modify it when
connecting/disconnecting kernel channels to the server's websocket
connections.  When starting the subprocess that handles websocket
connections, re-connect any previously connected kernels.
(jupyter-server--refresh-comm): Remove.
(jupyter-server-kernel-connected-p): Remove.
(jupyter-api-request): Use `jupyter-start`/`jupyter-stop` when
refreshing websocket connections.
(jupyter-server--connect-channels): Use `jupyter-ioloop-wait-until`.
(jupyter-connection): Remove `jupyter-server-kernel-connected-p`
call.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
8cd90fb9e0 Fix ordering of test cleanup
Stop the clients before stopping an connections to kernel servers.

* test/test-helper.el: Do it.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
55ee3e7e46 Use jupyter-process-environment in one place
* test/test-helper.el (jupyter-test-ipython-kernel-version): Do it.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
c18a5a9763 Remove a use of the manager slot of a client 2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
f2c9d5fb25 Convert jupyter-kernel into a method
* jupyter-kernel-process.el
* jupyter-server-kernel.el (jupyter-kernel): New method.

* jupyter-kernel.el (jupyter-kernel): Convert.  Load files that handle
  keywords and re-dispatch when ARGS does not have any connection
  info.  Document behavior.

* test/test-helper.el (jupyter-test-with-kernel-client)
* jupyter-server.el
(jupyter-connect-server-repl)
(jupyter-server-start-new-kernel): Use `jupyter-kernel`.
2023-02-13 20:21:14 -06:00
Nathaniel Nicandro
a33231b4d3 Rename many methods 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
61d01dfcf1 Skip some tests 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
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
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
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
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
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
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
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
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
Tomasz Mieszkowski
7d20c0aee2 Sort available kernelspecs; guess with ^; use guessing in jupyter-run-repl 2022-04-19 13:52:23 -05:00
Nathaniel Nicandro
c702f1e90f Add some helper functions for Org tests 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
4729e21184 Fix output order when using pandoc conversion
Fixes #351.
2021-11-30 10:46:52 -06:00
Nathaniel Nicandro
1fa6ed4e5f Remove the jupyter-ioloop-handler method
Replace it with a handler function passed to `jupyter-ioloop-start`.

* jupyter-ioloop-comm.el: Do it.

* jupyter-channel-ioloop-comm.el: Do it.

* jupyter-channel-ioloop-comm.el: Do it.

* jupyter-ioloop.el: Do it. Update comments and doc.
(jupyter-ioloop--delete-process): Remove. It was used to perform
cleanup when the object used for dispatching to a handler method was
garbage collected. Since we no longer rely on having an object for
dispatching, preferring just a function to call, its not needed.

(jupyter-ioloop--make-filter): Fall back to calling handler function
instead of `jupyter-ioloop-handler`. Only handle ioloop start/stop
events internally. This means we can remove the check for those in
`jupyter-ioloop-comm`.

* test/jupyter-test.el: Update tests to take into account above
  changes.
2021-04-22 09:51:36 -05:00
Nathaniel Nicandro
026c3fac61 Use :extra methods for org-babel-jupyter-parse-session
* ob-jupyter.el
(org-babel-jupyter-server-session)
(org-babel-jupyter-parse-session): Convert to method and re-implement
with :extra methods.

* test/jupyter-server-test.el
(org-babel-jupyter-server-session): New test.

* test/jupyter-test.el
(org-babel-jupyter-parse-session): Move test of server based sessions
to the new test.
2021-04-22 09:51:36 -05:00
Nathaniel Nicandro
c3fae7d7df jupyter-request-idle-received-p -> jupyter-request-idle-p
but first...

* jupyter-base.el: ...idle-received-p -> idle-p for jupyter-request objects

* jupyter-client.el: ...then, do it.

* test/jupyter-test.el: Do it.
2021-04-03 12:22:58 -05:00
Nathaniel Nicandro
7ca3344c1b Add syncing functions
These will be used in future commits.

* jupyter-client.el (jupyter-idle-sync)
(jupyter-add-idle-sync-hook): New functions.

* test/jupyter-test.el (jupyter-idle-sync): New test.
2021-04-03 12:22:58 -05:00
Nathaniel Nicandro
d6aced9e71 Rename internal functions for clarity
The following changes are made:

jupyter--run-handler-p -> jupyter--request-allows-handler-p
jupyter--handler-dispatch -> jupyter--run-handler

* jupyter-client.el: Do it.

* test/jupyter-test.el: Do it.
2021-04-03 12:22:58 -05:00
Nathaniel Nicandro
a9ae0bcef5 Replace jupyter-repl-cell-cond with jupyter-repl-map-cells
* jupyter-repl.el
(jupyter-repl-map-cells): New function.
(jupyter-repl-cell-cond): Remove. Update callers with new function.

* jupyter-julia.el: Ditto.

* test/jupyter-test.el
(jupyter-repl-map-cells): New test.
(jupyter-repl-cell-cond): Remove.
2020-04-14 17:18:38 -05:00
Nathaniel Nicandro
a7dbff9aa3 Refactor source block overriding mechanism
This commit adds the function `org-babel-jupyter--babel-map` and some
auxiliary functions to simplify the code for source block
overriding. The old implementation was very hard to follow, these
changes should hopefully make the code more readable.

* ob-jupyter.el (org-babel-jupyter--override-restore-header-args)
(org-babel-jupyter--override-restore-src-block): Remove.
(org-babel-jupyter--babel-vars): New variable.
(org-babel-jupyter--babel-op-symbol)
(org-babel-jupyter--babel-var-symbol)
(org-babel-jupyter--babel-map): New functions.
(org-babel-jupyter-override-src-block)
(org-babel-jupyter-restore-src-block)
(org-babel-jupyter-make-language-alias): Use them.
2020-04-14 17:05:28 -05:00
Nathaniel Nicandro
e400266ce1 Replace the jupyter-loop-over-mime macro
It is replaced with `jupyter-map-mime-bundle` which is a function.

* jupyter-base.el (jupyter-loop-over-mime): Remove.
(jupyter-map-mime-bundle): New function. Update call sites of the
removed function, with calls to this function.
(jupyter-normalize-data): Return a mime bundle, a property list with
:data and :metadata keys.  Update all callers.

* jupyter-mime.el (jupyter-insert): Updated to take into account the
  previously mentioned changes.

* jupyter-org-client.el (jupyter-org-result): Ditto.

* test/jupyter-test.el (jupyter-loop-over-mime): Rename to
  `jupyter-map-mime-bundle` and update the test to consider it.
2020-04-14 15:46:38 -05:00
Nathaniel Nicandro
91ca722b63 Remove unused code
The `jupyter-zmq-channel-comm.el` file was more of an experiment and
is unused.  It also causes failures on Emacs 27 due to missing
`thread-alive-p` function in 27.

* jupyter-zmq-channel-comm.el: Remove.

* test/jupyter-test.el: Remove the related `require`.  Re-work tests
  that used `jupyter-zmq-channel-comm` to not use it.
2020-04-14 12:23:51 -05:00
Nathaniel Nicandro
97c421a698 Fix regression
In `org-babel-jupyter-initiate-session-by-key`, the function to
disassociate a client from a :session was added to the
`current-buffer`s value of `kill-buffer-hook` but it should be added
to the REPL buffer's value of that hook.

fixes #240

* ob-jupyter.el
(org-babel-jupyter-initiate-session-by-key): Add to the local value of
`kill-buffer-hook` in the REPL's buffer.

* test/jupyter-test.el
(org-babel-jupyter-initiate-session-by-key): New test.
2020-04-14 11:36:40 -05:00
Nathaniel Nicandro
587ed4e029 Keep uppercase characters in kernel language names
Converting to lowercase was done arbitrarily.  So that one could write
`(jupyter-lang wolfram-language)` instead of
`(jupyter-lang Wolfram-Language)`.

fixes #241

* CHANGELOG.org: Add note.

* README.org: Update relevant sections.

* jupyter-base.el (jupyter-canonicalize-language-string): Do it.

* ob-jupyter.el (org-babel-jupyter-aliases-from-kernelspecs):
2020-04-09 11:33:57 -05:00
Nathaniel Nicandro
403c70c83c Bump version 2020-04-07 15:13:51 -05:00
Nathaniel Nicandro
187cb479c2 Run test only on Emacs versions where a bug can be detected
The test for #219 isn't really testing the root cause of the bug, only a
symptom.  And it doesn't catch anything on Emacs < 27.

* test/jupyter-test.el (jupyter-repl-issue-219): Skip on Emacs < 27
2020-04-04 12:27:44 -05:00
Nathaniel Nicandro
3aa523ddf2 Add test for org-babel-jupyter-parse-session
* test/jupyter-test.el (org-babel-jupyter-parse-session): New test.
2020-04-04 08:52:15 -05:00
Nathaniel Nicandro
e9899c65cd Add test to catch a symptom of #219
* test/jupyter-test.el: Do it.
2020-04-04 08:39:48 -05:00