2018-02-27 14:38:13 -06:00
|
|
|
;;; ein-testing-notebook.el --- Testing utilities for notebook module
|
|
|
|
|
|
|
|
;; Copyright (C) 2012 Takafumi Arakaki
|
|
|
|
|
|
|
|
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
;; ein-testing-notebook.el is free software: you can redistribute it
|
|
|
|
;; and/or modify it under the terms of the GNU General Public License
|
|
|
|
;; as published by the Free Software Foundation, either version 3 of
|
|
|
|
;; the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
;; ein-testing-notebook.el is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with ein-testing-notebook.el.
|
|
|
|
;; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
|
|
|
|
(require 'ein-notebook)
|
|
|
|
(require 'ein-testing-cell)
|
|
|
|
|
2018-09-26 10:07:50 -04:00
|
|
|
(defvar ein:testing-notebook-dummy-name "Dummy Name.ipynb")
|
|
|
|
(defvar ein:testing-notebook-dummy-url "DUMMY-URL")
|
|
|
|
|
|
|
|
(defun ein:testing-notebook-from-json (json-string)
|
2018-02-27 14:38:13 -06:00
|
|
|
(let* ((data (ein:json-read-from-string json-string))
|
|
|
|
(path (plist-get data :path))
|
2018-12-05 17:15:02 -05:00
|
|
|
(kernelspec (make-ein:$kernelspec :name "python" :language "python"))
|
2018-09-26 10:07:50 -04:00
|
|
|
(content (make-ein:$content :url-or-port ein:testing-notebook-dummy-url
|
2018-12-25 10:59:18 -05:00
|
|
|
:notebook-version "5.7.0"
|
2018-02-27 14:38:13 -06:00
|
|
|
:path path)))
|
2018-12-25 10:59:18 -05:00
|
|
|
(cl-letf (((symbol-function 'ein:need-notebook-version) (lambda (&rest ignore) "5.7.0"))
|
2018-12-05 17:15:02 -05:00
|
|
|
((symbol-function 'ein:kernel-retrieve-session) #'ignore)
|
|
|
|
((symbol-function 'ein:notebook-enable-autosaves) #'ignore))
|
2018-09-26 10:07:50 -04:00
|
|
|
(let ((notebook (ein:notebook-new ein:testing-notebook-dummy-url path kernelspec)))
|
2018-02-27 14:38:13 -06:00
|
|
|
(setf (ein:$notebook-kernel notebook)
|
2018-11-05 10:16:40 -05:00
|
|
|
(ein:kernel-new 8888 "" nil "/kernels" (ein:$notebook-events notebook) (ein:need-notebook-version (ein:$notebook-url-or-port notebook))))
|
2018-02-27 14:38:13 -06:00
|
|
|
(setf (ein:$kernel-events (ein:$notebook-kernel notebook))
|
|
|
|
(ein:events-new))
|
2019-11-08 12:47:06 -05:00
|
|
|
; matryoshka: new-content makes a ein:$content using CONTENT as template
|
2018-09-26 10:07:50 -04:00
|
|
|
; populating its raw_content field with DATA's content field
|
2019-03-29 12:11:12 -04:00
|
|
|
(ein:notebook-open--callback
|
2019-11-08 12:47:06 -05:00
|
|
|
notebook nil
|
2019-03-29 12:11:12 -04:00
|
|
|
(ein:new-content (ein:$notebook-url-or-port notebook)
|
|
|
|
(ein:$notebook-notebook-path notebook) data))
|
2018-02-27 14:38:13 -06:00
|
|
|
(ein:notebook-buffer notebook)))))
|
|
|
|
|
2018-09-26 10:07:50 -04:00
|
|
|
(defun ein:testing-notebook-make-data (name path cells)
|
2018-02-27 14:38:13 -06:00
|
|
|
(setq cells
|
|
|
|
(ein:testing-notebook--preprocess-cells-data-for-json-encode cells))
|
|
|
|
`((path . ,path)
|
|
|
|
(name . ,name)
|
|
|
|
(type . "notebook")
|
|
|
|
(format . "json")
|
|
|
|
(mimetype . nil)
|
|
|
|
(writeable . t)
|
|
|
|
(content (metadata . ())
|
|
|
|
(nbformat . 4)
|
|
|
|
(nbformat_minor . 0)
|
|
|
|
(cells . ,(apply #'vector cells)))))
|
|
|
|
|
|
|
|
(defun ein:testing-notebook--preprocess-cells-data-for-json-encode (cells)
|
|
|
|
"Preprocess CELLS data to make it work nice with `json-encode'."
|
|
|
|
(mapcar (lambda (c)
|
|
|
|
(cond
|
|
|
|
((equal (plist-get c :cell_type) "code")
|
|
|
|
;; turn `:outputs' into an array.
|
|
|
|
(plist-put c :outputs (apply #'vector (plist-get c :outputs))))
|
|
|
|
(t c)))
|
|
|
|
cells))
|
|
|
|
|
2018-09-26 10:07:50 -04:00
|
|
|
(defun ein:testing-notebook-make-new (&optional name path cells)
|
2018-02-27 14:38:13 -06:00
|
|
|
"Make new notebook. One empty cell will be inserted
|
2018-09-26 10:07:50 -04:00
|
|
|
automatically if CELLS is nil."
|
2018-02-27 14:38:13 -06:00
|
|
|
(ein:testing-notebook-from-json
|
2019-11-08 12:47:06 -05:00
|
|
|
(json-encode (ein:testing-notebook-make-data
|
|
|
|
(or name ein:testing-notebook-dummy-name)
|
2018-09-26 10:07:50 -04:00
|
|
|
(or path name ein:testing-notebook-dummy-name)
|
|
|
|
cells))))
|
2018-02-27 14:38:13 -06:00
|
|
|
|
2018-09-26 10:07:50 -04:00
|
|
|
(defun ein:testing-notebook-make-empty (&optional name path)
|
2018-02-27 14:38:13 -06:00
|
|
|
"Make empty notebook and return its buffer.
|
|
|
|
Automatically inserted cell for new notebook is deleted."
|
2018-09-26 10:07:50 -04:00
|
|
|
(let ((buffer (ein:testing-notebook-make-new name path)))
|
2018-02-27 14:38:13 -06:00
|
|
|
(with-current-buffer buffer
|
|
|
|
(call-interactively #'ein:worksheet-delete-cell))
|
|
|
|
buffer))
|
|
|
|
|
2018-09-26 10:07:50 -04:00
|
|
|
(defmacro ein:testing-with-one-cell (type-or-cell &rest body)
|
|
|
|
"Insert new cell of TYPE-OR-CELL in a clean notebook and execute BODY.
|
2018-02-27 14:38:13 -06:00
|
|
|
The new cell is bound to a variable `cell'."
|
|
|
|
(declare (indent 1))
|
|
|
|
`(with-current-buffer (ein:testing-notebook-make-empty)
|
|
|
|
(let ((cell (ein:worksheet-insert-cell-below ein:%worksheet%
|
2018-09-26 10:07:50 -04:00
|
|
|
,type-or-cell nil t)))
|
2018-02-27 14:38:13 -06:00
|
|
|
,@body)))
|
|
|
|
|
|
|
|
(defun ein:testing-make-notebook-with-outputs (list-outputs)
|
|
|
|
"Make a new notebook with cells with output.
|
|
|
|
LIST-OUTPUTS is a list of list of strings (pyout text). Number
|
|
|
|
of LIST-OUTPUTS equals to the number cells to be contained in the
|
|
|
|
notebook."
|
2019-11-08 12:47:06 -05:00
|
|
|
(ein:testing-notebook-make-new
|
2018-09-26 10:07:50 -04:00
|
|
|
ein:testing-notebook-dummy-name nil
|
2018-02-27 14:38:13 -06:00
|
|
|
(mapcar (lambda (outputs)
|
|
|
|
(ein:testing-codecell-data
|
|
|
|
nil nil (mapcar #'ein:testing-codecell-pyout-data outputs)))
|
|
|
|
list-outputs)))
|
|
|
|
|
|
|
|
(provide 'ein-testing-notebook)
|
|
|
|
|
|
|
|
;;; ein-testing-notebook.el ends here
|