2012-05-07 14:41:15 +02:00
;;; ein-notebooklist.el --- Notebook list buffer
2018-04-22 10:22:11 -05:00
;; Copyright (C) 2018- John M. Miller
2012-05-07 14:41:15 +02:00
2015-01-31 10:13:49 -06:00
;; Authors: Takafumi Arakaki <aka.tkf at gmail.com>
;; John M. Miller <millejoh at mac.com>
2012-05-07 14:41:15 +02:00
;; This file is NOT part of GNU Emacs.
;; ein-notebooklist.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-notebooklist.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-notebooklist.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
( eval-when-compile ( require 'cl ) )
( require 'widget )
2018-05-31 13:15:04 -05:00
( require 'cus-edit )
2012-08-28 15:26:32 +02:00
( require 'ein-core )
2012-05-07 14:41:15 +02:00
( require 'ein-notebook )
2018-03-10 07:07:37 -06:00
( require 'ein-connect )
2017-04-20 07:47:05 -05:00
( require 'ein-file )
2015-02-10 14:53:08 -06:00
( require 'ein-contents-api )
2012-05-19 15:47:09 +02:00
( require 'ein-subpackages )
2018-12-25 19:12:26 -05:00
( require 'ein-ac )
( require 'ein-company )
2017-05-30 14:33:31 -05:00
( require 'deferred )
2017-09-30 14:35:36 +09:00
( require 'dash )
2018-10-24 13:12:16 -04:00
( require 'ido )
2018-12-01 18:54:58 -05:00
( autoload 'ein:jupyterhub-connect " ein-jupyterhub " )
2018-10-24 13:12:16 -04:00
( defcustom ein:notebooklist-login-timeout ( truncate ( * 6.3 1000 ) )
" Timeout in milliseconds for logging into server "
:group 'ein
:type 'integer
)
2012-05-07 14:41:15 +02:00
2018-10-01 18:40:31 -04:00
( defcustom ein:notebooklist-render-order
2018-03-25 13:30:50 -07:00
' ( render-header
render-opened-notebooks
2018-10-01 18:40:31 -04:00
render-directory )
2018-03-25 13:30:50 -07:00
" Order of notebook list sections.
2018-10-01 18:40:31 -04:00
Must contain render-header, render-opened-notebooks, and render-directory. "
2018-03-25 13:30:50 -07:00
:group 'ein
:type 'list
)
2012-07-05 00:13:12 +02:00
( defcustom ein:notebooklist-first-open-hook nil
" Hooks to run when the notebook list is opened at first time.
Example to open a notebook named _scratch_ when the notebook list
is opened at first time.::
( add-hook
'ein:notebooklist-first-open-hook
2018-10-24 13:12:16 -04:00
( lambda ( ) ( ein:notebook-open ( ein:$notebooklist-url-or-port ein:%notebooklist% ) \"main.ipynb\" ) ) )
2012-07-05 00:13:12 +02:00
"
:type 'hook
:group 'ein )
2012-05-13 02:51:47 +02:00
( defstruct ein:$notebooklist
" Hold notebooklist variables.
` ein:$notebooklist-url-or-port '
URL or port of IPython server.
2018-10-01 18:40:31 -04:00
` ein:$notebooklist-path '
2014-09-11 20:04:00 -05:00
The path for the notebooklist.
2012-05-13 02:51:47 +02:00
` ein:$notebooklist-data '
2014-10-17 16:44:04 -05:00
JSON data sent from the server.
` ein:$notebooklist-api-version '
Major version of the IPython notebook server we are talking to. "
2012-05-13 02:51:47 +02:00
url-or-port
2014-09-11 20:04:00 -05:00
path
2014-10-17 16:44:04 -05:00
data
api-version )
2012-05-13 02:51:47 +02:00
2012-08-18 22:31:15 +02:00
( ein:deflocal ein:%notebooklist% nil
2012-05-13 02:51:47 +02:00
" Buffer local variable to store an instance of `ein:$notebooklist' . " )
2018-04-22 20:57:01 -05:00
2018-09-26 10:25:48 -04:00
( ein:deflocal ein:%notebooklist-new-kernel% nil
" Buffer local variable to store kernel type for newly created notebooks. " )
2018-05-31 13:15:04 -05:00
( defcustom ein:notebooklist-sort-field :name
" The notebook list sort field. "
:type ' ( choice ( const :tag " Name " :name )
( const :tag " Last modified " :last_modified ) )
:group 'ein )
( make-variable-buffer-local 'ein:notebooklist-sort-field )
( put 'ein:notebooklist-sort-field 'permanent-local t )
( defcustom ein:notebooklist-sort-order :ascending
" The notebook list sort order. "
:type ' ( choice ( const :tag " Ascending " :ascending )
( const :tag " Descending " :descending ) )
:group 'ein )
( make-variable-buffer-local 'ein:notebooklist-sort-order )
( put 'ein:notebooklist-sort-order 'permanent-local t )
2018-05-04 15:07:32 -04:00
( defmacro ein:make-sorting-widget ( tag custom-var )
" Create the sorting widget. "
;; assume that custom-var has type `choice' of `const's.
` ( widget-create
'menu-choice :tag , tag
:value , custom-var
:notify ( lambda ( widget &rest ignore )
2018-10-24 13:12:16 -04:00
( run-at-time 1 nil #' ein:notebooklist-reload )
2018-05-04 15:07:32 -04:00
( setq , custom-var ( widget-value widget ) ) )
,@ ( mapcar ( lambda ( const )
` ' ( item :tag , ( third const ) :value , ( fourth const ) ) )
( rest ( custom-variable-type custom-var ) ) ) ) )
2018-04-22 20:57:01 -05:00
2012-08-18 22:31:15 +02:00
( define-obsolete-variable-alias 'ein:notebooklist 'ein:%notebooklist% " 0.1.2 " )
2012-05-07 14:41:15 +02:00
( defvar ein:notebooklist-buffer-name-template " *ein:notebooklist %s* " )
2012-06-26 17:45:51 +02:00
( defvar ein:notebooklist-map ( make-hash-table :test 'equal )
" Data store for `ein:notebooklist-list' .
Mapping from URL-OR-PORT to an instance of ` ein:$notebooklist '. " )
2012-06-05 14:54:18 +02:00
2012-06-26 17:45:51 +02:00
( defun ein:notebooklist-list ( )
" Get a list of opened `ein:$notebooklist' . "
( ein:hash-vals ein:notebooklist-map ) )
2018-10-15 16:57:22 -04:00
( defun ein:notebooklist-list-remove ( url-or-port )
( remhash url-or-port ein:notebooklist-map ) )
2012-06-26 17:45:51 +02:00
( defun ein:notebooklist-list-add ( nblist )
" Register notebook list instance NBLIST for global lookup.
This function adds NBLIST to ` ein:notebooklist-map '. "
( puthash ( ein:$notebooklist-url-or-port nblist )
nblist
ein:notebooklist-map ) )
2012-05-07 14:41:15 +02:00
2012-07-05 00:13:12 +02:00
( defun ein:notebooklist-list-get ( url-or-port )
" Get an instance of `ein:$notebooklist' by URL-OR-PORT as a key. "
( gethash url-or-port ein:notebooklist-map ) )
2014-10-17 16:44:04 -05:00
( defun ein:notebooklist-url ( url-or-port version &optional path )
( let ( ( base-path ( cond ( ( = version 2 ) " api/notebooks " )
2016-11-11 09:48:47 -08:00
( ( >= version 3 ) " api/contents " ) ) ) )
2018-10-15 16:57:22 -04:00
( ein:url url-or-port base-path path ) ) )
2014-10-17 16:44:04 -05:00
2018-12-01 18:54:58 -05:00
( defun ein:notebooklist-sentinel ( url-or-port process event )
2018-10-24 13:12:16 -04:00
" Remove URL-OR-PORT from ein:notebooklist-map when PROCESS dies "
2018-10-15 16:57:22 -04:00
( when ( not ( string= " open " ( substring event 0 4 ) ) )
2018-10-24 13:12:16 -04:00
( ein:log 'info " Process %s %s %s "
( car ( process-command process ) )
( replace-regexp-in-string " \n $ " " " event )
url-or-port )
2018-10-15 16:57:22 -04:00
( ein:notebooklist-list-remove url-or-port ) ) )
2012-05-12 00:34:00 +02:00
2012-05-13 02:51:47 +02:00
( defun ein:notebooklist-get-buffer ( url-or-port )
2012-05-12 00:34:00 +02:00
( get-buffer-create
2012-05-13 02:51:47 +02:00
( format ein:notebooklist-buffer-name-template url-or-port ) ) )
2012-05-12 00:34:00 +02:00
2018-10-22 14:49:45 -05:00
( defun ein:crib-token--all-local-tokens ( )
2018-10-22 14:52:46 -05:00
" Generate a hash table of authorization tokens (when they
exist ) for allow local jupyter instances, keyed by they url and
port the instance is running on. "
2018-11-05 11:46:20 -05:00
( let ( ( lines
( condition-case err
;; there may be NO local jupyter installation
( process-lines ein:jupyter-default-server-command
" notebook " " list " " --json " )
( error ( message " Error getting local tokens: %s " err )
( ) ) ) ) ; empty list
2018-10-24 13:12:16 -04:00
( url-tokens ( make-hash-table :test #' equal ) ) )
2018-10-23 02:12:45 -04:00
( loop for line in lines
do ( destructuring-bind
( &key password url token &allow-other-keys )
( ein:json-read-from-string line )
2018-10-24 13:12:16 -04:00
( push ( list password token ) ( gethash ( ein:url url ) url-tokens ) ) ) )
url-tokens ) )
2018-10-22 14:49:45 -05:00
( defun ein:crib-token ( url-or-port )
2018-10-24 13:12:16 -04:00
( let ( ( pw-pairs ( gethash url-or-port ( ein:crib-token--all-local-tokens ) ) ) )
;; pw-pairs is of the form ((PASSWORD-P TOKEN) (PASSWORD-P TOKEN))
( cond ( ( = ( length pw-pairs ) 1 ) ( car pw-pairs ) )
2018-11-05 11:46:20 -05:00
( ( > ( length pw-pairs ) 1 )
2018-10-24 13:12:16 -04:00
;; orig code: (list :json-false token) meant "no password, yes token"
;; It's not clear how two entries for the same url could happen but if it did,
;; 1. what if both entries don't have any auth enabled?
;; 2. what if an entry required a password and not a token?
;; It's best to return "nil nil" in this unlikely (impossible?) event, and let
;; the downstream logic handle it.
( warn " I see multiple jupyter servers registered on the same url! Please enter the token for one that is actually running. " )
( list nil nil ) )
( t ( list nil nil ) ) ) ) )
2018-10-15 16:57:22 -04:00
( defun ein:notebooklist-token-or-password ( url-or-port )
2018-10-24 13:12:16 -04:00
" Return token or password (jupyter requires one or the other but not both) for URL-OR-PORT. Empty string token means all authentication disabled. Nil means don't know. "
2018-10-15 16:57:22 -04:00
( multiple-value-bind ( password-p token ) ( ein:crib-token url-or-port )
2018-10-23 02:12:45 -04:00
( autoload 'ein:jupyter-server-conn-info " ein-jupyter " )
2018-10-18 19:01:43 -04:00
( multiple-value-bind ( my-url-or-port my-token ) ( ein:jupyter-server-conn-info )
2018-10-24 13:12:16 -04:00
( cond ( ( eq password-p t ) ( read-passwd ( format " Password for %s: " url-or-port ) ) )
( ( and ( stringp token ) ( eql password-p :json-false ) ) token )
( ( equal url-or-port my-url-or-port ) my-token )
( t nil ) ) ) ) )
2018-10-15 16:57:22 -04:00
2012-06-09 01:02:53 +02:00
( defun ein:notebooklist-ask-url-or-port ( )
2018-10-24 13:12:16 -04:00
( let* ( ( default ( ein:url ( ein:aif ( ein:get-notebook )
( ein:$notebook-url-or-port it )
( ein:aif ein:%notebooklist%
( ein:$notebooklist-url-or-port it )
( ein:default-url-or-port ) ) ) ) )
( url-or-port-list
( -distinct ( mapcar #' ein:url
2018-11-05 11:46:20 -05:00
( append ( list default )
2018-10-24 13:12:16 -04:00
ein:url-or-port
2018-11-05 11:46:20 -05:00
( hash-table-keys
2018-10-24 13:12:16 -04:00
( ein:crib-token--all-local-tokens ) ) ) ) ) )
( url-or-port ( let ( ( ido-report-no-match nil )
( ido-use-faces nil ) )
2018-10-28 12:22:38 -04:00
( ido-completing-read " URL or port: "
2018-10-24 13:12:16 -04:00
url-or-port-list
nil nil nil nil
default ) ) ) )
2018-10-11 16:53:02 -04:00
( ein:url url-or-port ) ) )
2012-06-09 01:02:53 +02:00
2018-10-28 14:27:09 -04:00
( defun ein:notebooklist-open* ( url-or-port &optional path resync callback errback )
2018-10-15 16:57:22 -04:00
" The main entry to server at URL-OR-PORT. Users should not directly call this, but instead `ein:notebooklist-login' .
2018-12-01 18:54:58 -05:00
PATH is specifying directory from file navigation. PATH is empty on login. RESYNC is requery server attributes such as ipython version and kernelspecs. CALLBACK takes two arguments, the resulting buffer and URL-OR-PORT. ERRBACK takes one argument, the resulting buffer.
TODO: going to maintain jupyterhub hooks here
2018-10-15 16:57:22 -04:00
"
2014-09-12 12:37:24 -05:00
( unless path ( setq path " " ) )
2018-10-11 16:53:02 -04:00
( setq url-or-port ( ein:url url-or-port ) ) ;; should work towards not needing this
2018-10-15 16:57:22 -04:00
( lexical-let* ( ( url-or-port url-or-port )
( path path )
2018-12-01 18:54:58 -05:00
( success ( apply-partially #' ein:notebooklist-open--finish
url-or-port callback ) )
2018-10-28 14:27:09 -04:00
( failure errback ) )
2018-10-01 18:40:31 -04:00
( if ( or resync ( not ( ein:notebooklist-list-get url-or-port ) ) )
( deferred:$
( deferred:parallel
( lexical-let ( ( d ( deferred:new #' identity ) ) )
2018-10-18 19:01:43 -04:00
( ein:query-notebook-version url-or-port ( lambda ( )
2018-10-01 18:40:31 -04:00
( deferred:callback-post d ) ) )
d )
( lexical-let ( ( d ( deferred:new #' identity ) ) )
( ein:query-kernelspecs url-or-port ( lambda ( )
( deferred:callback-post d ) ) )
2018-10-11 16:53:02 -04:00
d ) )
( deferred:nextc it
( lambda ( &rest ignore )
2018-10-12 15:32:51 -05:00
( lexical-let ( ( d ( deferred:new #' identity ) ) )
2018-10-24 13:12:16 -04:00
( ein:content-query-hierarchy url-or-port ( lambda ( tree )
( deferred:callback-post d ) ) )
2018-10-12 15:32:51 -05:00
d ) ) )
2018-10-01 18:40:31 -04:00
( deferred:nextc it
( lambda ( &rest ignore )
2018-10-28 14:27:09 -04:00
( ein:content-query-contents url-or-port path success failure ) ) ) )
( ein:content-query-contents url-or-port path success failure ) ) ) )
2012-05-07 14:41:15 +02:00
2017-01-31 10:44:04 -06:00
( defcustom ein:notebooklist-keepalive-refresh-time 1
" When the notebook keepalive is enabled, the frequency, IN
HOURS, with which to make calls to the jupyter content API to
refresh the notebook connection. "
:type 'float
:group 'ein )
2017-02-15 19:33:35 -06:00
( defcustom ein:enable-keepalive nil
" When non-nil, will cause EIN to automatically call
` ein:notebooklist-enable-keepalive ' after any call to
` ein:notebooklist-open '. "
:type 'boolean
:group 'ein )
2018-04-23 17:33:13 -04:00
( defcustom ein:notebooklist-date-format " %x "
" The format spec for date in notebooklist mode.
2018-05-25 11:43:39 -04:00
See ` ein:format-time-string '. "
2018-04-23 17:33:13 -04:00
:type ' ( or string function )
:group 'ein )
2017-01-31 10:44:04 -06:00
( defvar ein:notebooklist--keepalive-timer nil )
;;;###autoload
( defun ein:notebooklist-enable-keepalive ( &optional url-or-port )
2017-02-01 09:25:47 -06:00
" Enable periodic calls to the notebook server to keep long running sessions from expiring.
By long running we mean sessions to last days, or weeks. The
frequency of the refresh ( which is very similar to a call to
` ein:notebooklist-open ` ) is controlled by
` ein:notebooklist-keepalive-refresh-time ` , and is measured in
2017-02-16 09:01:14 -06:00
terms of hours. If ` ein:enable-keepalive ' is non-nil this will
automatically be called during calls to ` ein:notebooklist-open ` . "
2017-01-31 10:44:04 -06:00
( interactive ( list ( ein:notebooklist-ask-url-or-port ) ) )
2017-02-15 19:33:35 -06:00
( unless ein:notebooklist--keepalive-timer
( message " Enabling notebooklist keepalive... " )
( let ( ( success
( lambda ( content )
( ein:log 'info " Refreshing notebooklist connection. " ) ) )
( refresh-time ( * ein:notebooklist-keepalive-refresh-time 60 60 ) ) )
( setq ein:notebooklist--keepalive-timer
2018-10-28 14:27:09 -04:00
( run-at-time 0.1 refresh-time #' ein:content-query-contents url-or-port " " success nil ) ) ) ) )
2017-01-31 10:44:04 -06:00
;;;###autoload
( defun ein:notebooklist-disable-keepalive ( )
2017-02-01 09:25:47 -06:00
" Disable the notebooklist keepalive calls to the jupyter notebook server. "
2017-01-31 10:44:04 -06:00
( interactive )
2017-02-15 19:33:35 -06:00
( message " Disabling notebooklist keepalive... " )
2017-02-16 09:01:14 -06:00
( cancel-timer ein:notebooklist--keepalive-timer )
( setq ein:notebooklist--keepalive-timer nil ) )
2017-01-31 10:44:04 -06:00
2018-10-30 14:17:29 -04:00
( defun ein:notebooklist-open--finish ( url-or-port callback content )
2018-12-01 18:54:58 -05:00
" Called via `ein:notebooklist-open*' . "
2018-10-30 14:17:29 -04:00
( let ( ( path ( ein:$content-path content ) )
2018-10-24 13:12:16 -04:00
( nb-version ( ein:$content-notebook-version content ) )
2015-02-10 14:53:08 -06:00
( data ( ein:$content-raw-content content ) ) )
( with-current-buffer ( ein:notebooklist-get-buffer url-or-port )
( let ( ( already-opened-p ( ein:notebooklist-list-get url-or-port ) )
( orig-point ( point ) ) )
( setq ein:%notebooklist%
( make-ein:$notebooklist :url-or-port url-or-port
:path path
:data data
2018-10-24 13:12:16 -04:00
:api-version nb-version ) )
2015-02-10 14:53:08 -06:00
( ein:notebooklist-list-add ein:%notebooklist% )
2018-10-24 13:12:16 -04:00
( ein:notebooklist-render nb-version )
2015-02-10 14:53:08 -06:00
( goto-char orig-point )
2018-10-24 13:12:16 -04:00
( ein:log 'verbose " Opened notebooklist at %s " ( ein:url url-or-port path ) )
2015-02-10 14:53:08 -06:00
( unless already-opened-p
( run-hooks 'ein:notebooklist-first-open-hook ) )
2017-02-15 19:33:35 -06:00
( when ein:enable-keepalive
2018-10-30 14:17:29 -04:00
( ein:notebooklist-enable-keepalive url-or-port ) )
2018-10-22 14:49:45 -05:00
( when callback
2018-12-01 18:54:58 -05:00
( funcall callback ( current-buffer ) url-or-port ) ) )
( current-buffer ) ) ) )
2012-05-12 00:34:00 +02:00
2014-09-11 20:04:00 -05:00
( defun* ein:notebooklist-open-error ( url-or-port path
2018-10-01 18:40:31 -04:00
&key error-thrown
2012-06-26 19:30:35 +02:00
&allow-other-keys )
( ein:log 'error
2018-10-01 18:40:31 -04:00
" ein:notebooklist-open-error %s: ERROR %s DATA %s " ( concat ( file-name-as-directory url-or-port ) path ) ( car error-thrown ) ( cdr error-thrown ) ) )
2012-06-26 19:30:35 +02:00
2012-09-01 20:51:55 +02:00
;;;###autoload
2018-10-24 13:12:16 -04:00
( defun ein:notebooklist-reload ( &optional nblist resync )
2012-05-13 05:18:38 +02:00
" Reload current Notebook list. "
2018-10-24 13:12:16 -04:00
( interactive )
( unless nblist
( setq nblist ein:%notebooklist% ) )
( when nblist
( ein:notebooklist-open* ( ein:$notebooklist-url-or-port nblist )
( ein:$notebooklist-path nblist ) resync ) ) )
2012-05-13 05:18:38 +02:00
2012-06-26 17:03:36 +02:00
( defun ein:notebooklist-refresh-related ( )
" Reload notebook list in which current notebook locates.
This function is called via ` ein:notebook-after-rename-hook '. "
2018-10-15 16:57:22 -04:00
( ein:notebooklist-open* ( ein:$notebook-url-or-port ein:%notebook% )
( ein:$notebook-notebook-path ein:%notebook% ) ) )
2012-06-26 17:03:36 +02:00
( add-hook 'ein:notebook-after-rename-hook 'ein:notebooklist-refresh-related )
2017-04-02 14:33:56 -05:00
;;;###autoload
( defun ein:notebooklist-upload-file ( upload-path )
( interactive " fSelect file to upload: " )
( unless ein:%notebooklist%
( error " Only works when called from an ein:notebooklist buffer. " ) )
( let ( ( nb-path ( ein:$notebooklist-path ein:%notebooklist% ) ) )
( ein:content-upload nb-path upload-path ) ) )
2012-09-01 20:51:55 +02:00
;;;###autoload
2018-10-24 13:12:16 -04:00
( defun ein:notebooklist-new-notebook ( &optional url-or-port kernelspec path callback )
2018-12-05 17:15:02 -05:00
" Ask server to create a new notebook and open it in a new buffer. "
2016-01-13 07:52:02 -06:00
( interactive ( list ( ein:notebooklist-ask-url-or-port )
2018-11-03 16:02:04 -04:00
( ido-completing-read
" Select kernel: "
2017-04-02 14:33:56 -05:00
( ein:list-available-kernels ( ein:$notebooklist-url-or-port ein:%notebooklist% ) ) nil t nil nil " default " nil ) ) )
2014-12-04 13:52:36 -06:00
( let ( ( path ( or path ( ein:$notebooklist-path ( or ein:%notebooklist%
( ein:notebooklist-list-get url-or-port ) ) ) ) )
( version ( ein:$notebooklist-api-version ( or ein:%notebooklist%
( ein:notebooklist-list-get url-or-port ) ) ) ) )
2014-09-19 09:17:12 -05:00
( unless url-or-port
( setq url-or-port ( ein:$notebooklist-url-or-port ein:%notebooklist% ) ) )
2018-10-15 16:57:22 -04:00
( let ( ( url ( ein:notebooklist-url url-or-port
version
path ) ) )
2014-09-19 09:17:12 -05:00
( ein:query-singleton-ajax
( list 'notebooklist-new-notebook url-or-port path )
url
:type " POST "
2015-01-16 22:01:54 -06:00
:data ( json-encode ' ( ( :type . " notebook " ) ) )
2014-12-04 13:52:36 -06:00
:parser #' ein:json-read
;; (lambda ()
;; (ein:html-get-data-in-body-tag "data-notebook-id"))
2014-09-19 09:17:12 -05:00
:error ( apply-partially #' ein:notebooklist-new-notebook-error
2018-10-24 13:12:16 -04:00
url-or-port path callback )
2014-09-19 09:17:12 -05:00
:success ( apply-partially #' ein:notebooklist-new-notebook-callback
2018-10-24 13:12:16 -04:00
url-or-port kernelspec path callback ) ) ) ) )
2012-12-29 17:17:39 +01:00
( defun* ein:notebooklist-new-notebook-callback ( url-or-port
2016-03-01 16:02:00 -06:00
kernelspec
2014-09-19 09:17:12 -05:00
path
2012-12-29 17:17:39 +01:00
callback
&key
2012-12-29 17:52:05 +01:00
data
2018-10-11 16:53:02 -04:00
&allow-other-keys )
( let ( ( nbname ( plist-get data :name ) )
( nbpath ( plist-get data :path ) ) )
2018-12-25 10:59:18 -05:00
( when ( < ( ein:notebook-version-numeric url-or-port ) 3 )
2018-10-11 16:53:02 -04:00
( if ( string= nbpath " " )
( setq nbpath nbname )
( setq nbpath ( format " %s/%s " nbpath nbname ) ) ) )
2018-10-24 13:12:16 -04:00
( ein:notebook-open url-or-port nbpath kernelspec callback )
2018-10-15 16:57:22 -04:00
( ein:notebooklist-open* url-or-port path ) ) )
2012-05-26 19:51:57 +02:00
2012-09-06 23:46:29 +02:00
( defun* ein:notebooklist-new-notebook-error
2018-10-24 13:12:16 -04:00
( url-or-port callback
2012-12-29 17:44:49 +01:00
&key response &allow-other-keys
&aux
( error ( request-response-error-thrown response ) )
2013-01-10 19:10:59 +01:00
( dest ( request-response-url response ) ) )
2012-09-06 23:46:29 +02:00
( ein:log 'verbose
2013-01-10 19:10:59 +01:00
" NOTEBOOKLIST-NEW-NOTEBOOK-ERROR url-or-port: %S; error: %S; dest: %S "
url-or-port error dest )
( ein:log 'error
" Failed to open new notebook (error: %S). \
You may find the new one in the notebook list. " error)
2018-12-01 18:54:58 -05:00
( ein:notebooklist-open* url-or-port nil nil ( lambda ( buffer url-or-port )
( pop-to-buffer buffer ) ) ) )
2012-09-06 23:46:29 +02:00
2012-09-01 20:51:55 +02:00
;;;###autoload
2016-03-22 07:47:11 -05:00
( defun ein:notebooklist-new-notebook-with-name ( name kernelspec url-or-port &optional path )
2012-06-08 12:59:14 +02:00
" Open new notebook and rename the notebook. "
2012-09-05 14:51:35 +02:00
( interactive ( let* ( ( url-or-port ( or ( ein:get-url-or-port )
( ein:default-url-or-port ) ) )
2018-11-03 16:02:04 -04:00
( kernelspec ( ido-completing-read
" Select kernel: "
2016-03-22 07:47:11 -05:00
( ein:list-available-kernels url-or-port ) nil t nil nil " default " nil ) )
2016-01-13 07:52:02 -06:00
( name ( read-from-minibuffer
( format " Notebook name (at %s): " url-or-port ) ) ) )
( list name kernelspec url-or-port ) ) )
2015-03-25 09:52:16 -05:00
( let ( ( path ( or path ( ein:$notebooklist-path
( or ein:%notebooklist%
( ein:get-notebook )
( gethash url-or-port ein:notebooklist-map ) ) ) ) ) )
2014-10-17 16:44:04 -05:00
( ein:notebooklist-new-notebook
url-or-port
2016-01-13 07:52:02 -06:00
kernelspec
2014-10-17 16:44:04 -05:00
path
2018-10-24 13:12:16 -04:00
( apply-partially
( lambda ( name* notebook created )
2018-12-25 19:12:26 -05:00
( unless created
( ein:log 'warn " Notebook %s already existed " name ) )
2018-10-24 13:12:16 -04:00
( with-current-buffer ( ein:notebook-buffer notebook )
( ein:notebook-rename-command name* )
( pop-to-buffer ( current-buffer ) ) ) )
name ) ) ) )
2012-06-08 12:59:14 +02:00
2015-02-10 14:53:08 -06:00
( defun ein:notebooklist-delete-notebook-ask ( path )
( when ( y-or-n-p ( format " Delete notebook %s? " path ) )
( ein:notebooklist-delete-notebook path ) ) )
2012-05-13 06:51:26 +02:00
2018-10-11 16:53:02 -04:00
( defun ein:notebooklist-delete-notebook ( path &optional callback )
2018-11-08 03:44:59 -05:00
" CALLBACK with no arguments, e.g., semaphore "
2018-10-11 16:53:02 -04:00
( lexical-let* ( ( path path )
( notebooklist ein:%notebooklist% )
( callback callback )
( url-or-port ( ein:$notebooklist-url-or-port notebooklist ) ) )
( unless callback ( setq callback ( lambda ( ) ( ein:notebooklist-reload notebooklist ) ) ) )
( ein:query-singleton-ajax
( list 'notebooklist-delete-notebook ( ein:url url-or-port path ) )
( ein:notebook-url-from-url-and-id
url-or-port ( ein:$notebooklist-api-version notebooklist ) path )
:type " DELETE "
:complete ( apply-partially #' ein:notebooklist-delete-notebook--complete ( ein:url url-or-port path ) callback ) ) ) )
( defun* ein:notebooklist-delete-notebook--complete ( url callback
&key data response symbol-status
&allow-other-keys
&aux ( resp-string ( format " STATUS: %s DATA: %s " ( request-response-status-code response ) data ) ) )
( ein:log 'debug " ein:notebooklist-delete-notebook--complete %s " resp-string )
( when ( and callback ( eq symbol-status 'success ) ) ( funcall callback ) ) )
2012-05-12 00:59:15 +02:00
2015-12-04 14:15:41 -06:00
;; Because MinRK wants me to suffer (not really, I love MinRK)...
2014-11-12 06:38:02 -06:00
( defun ein:get-actual-path ( path )
( ein:aif ( cl-position ?/ path :from-end t )
( substring path 0 it )
" " ) )
2014-12-09 10:41:53 -06:00
( defun generate-breadcrumbs ( path )
" Given notebooklist path, generate alist of breadcrumps of form (name . path). "
( let* ( ( paths ( split-string path " / " t ) )
( current-path " / " )
( pairs ( list ( cons " Home " " " ) ) ) )
( dolist ( p paths pairs )
2015-09-14 13:16:49 -05:00
( setf current-path ( concat current-path " / " p )
2014-12-09 10:41:53 -06:00
pairs ( append pairs ( list ( cons p current-path ) ) ) ) ) ) )
2018-04-22 20:57:01 -05:00
( defun* ein:nblist--sort-group ( group by-param order )
2018-04-22 10:22:11 -05:00
( sort group #' ( lambda ( x y )
2018-05-04 15:07:32 -04:00
( cond ( ( eql order :ascending )
2018-04-22 20:57:01 -05:00
( string-lessp ( plist-get x by-param )
( plist-get y by-param ) ) )
2018-05-04 15:07:32 -04:00
( ( eql order :descending )
2018-04-22 20:57:01 -05:00
( string-greaterp ( plist-get x by-param )
( plist-get y by-param ) ) ) ) ) ) )
( defun ein:notebooklist--order-data ( nblist-data sort-param sort-order )
2018-03-27 22:34:19 -07:00
" Try to sanely sort the notebooklist data for the current path. "
( let* ( ( groups ( -group-by #' ( lambda ( x ) ( plist-get x :type ) )
nblist-data ) )
2018-04-22 20:57:01 -05:00
( dirs ( ein:nblist--sort-group ( cdr ( assoc " directory " groups ) )
sort-param
sort-order ) )
( nbs ( ein:nblist--sort-group ( cdr ( assoc " notebook " groups ) )
sort-param
sort-order ) )
2018-04-22 10:22:11 -05:00
( files ( ein:nblist--sort-group ( -flatten-n 1 ( -map #' cdr ( -group-by
#' ( lambda ( x ) ( car ( last ( s-split " \\ . " ( plist-get x :name ) ) ) ) )
2018-04-22 20:57:01 -05:00
( cdr ( assoc " file " groups ) ) ) ) )
sort-param
sort-order ) ) )
2018-03-27 22:34:19 -07:00
( -concat dirs nbs files ) ) )
2018-10-01 18:40:31 -04:00
( defun render-header-ipy2 ( &rest args )
2018-04-22 10:22:11 -05:00
" Render the header (for ipython2). "
2016-04-15 16:08:53 -05:00
;; Create notebook list
( widget-insert ( format " IPython %s Notebook list \n \n " ( ein:$notebooklist-api-version ein:%notebooklist% ) ) )
2018-03-27 22:34:19 -07:00
2016-04-15 16:08:53 -05:00
( let ( ( breadcrumbs ( generate-breadcrumbs ( ein:$notebooklist-path ein:%notebooklist% ) ) ) )
( dolist ( p breadcrumbs )
( lexical-let ( ( name ( car p ) )
( path ( cdr p ) ) )
( widget-insert " | " )
( widget-create
'link
2018-11-08 09:36:35 -05:00
:notify ( lambda ( &rest ignore )
( ein:notebooklist-login
( ein:$notebooklist-url-or-port ein:%notebooklist% ) path ) )
2016-04-15 16:08:53 -05:00
name ) ) )
( widget-insert " | \n \n " ) )
2018-03-27 22:34:19 -07:00
2016-04-15 16:08:53 -05:00
( widget-create
'link
:notify ( lambda ( &rest ignore ) ( ein:notebooklist-new-notebook
( ein:$notebooklist-url-or-port ein:%notebooklist% ) ) )
" New Notebook " )
( widget-insert " " )
( widget-create
'link
2018-10-24 13:12:16 -04:00
:notify ( lambda ( &rest ignore ) ( ein:notebooklist-reload nil t ) )
2016-04-15 16:08:53 -05:00
" Reload List " )
( widget-insert " " )
( widget-create
'link
:notify ( lambda ( &rest ignore )
( browse-url
( ein:url ( ein:$notebooklist-url-or-port ein:%notebooklist% ) ) ) )
" Open In Browser " )
2018-04-22 10:22:11 -05:00
( widget-insert " \n " ) )
2017-09-12 16:22:19 -05:00
2018-10-01 18:40:31 -04:00
( defun render-header* ( url-or-port &rest args )
2018-04-22 10:22:11 -05:00
" Render the header (for ipython>=3). "
2018-10-01 18:40:31 -04:00
( with-current-buffer ( ein:notebooklist-get-buffer url-or-port )
( widget-insert
2018-12-25 10:59:18 -05:00
( format " Contents API %s (%s) \n \n " ( ein:need-notebook-version url-or-port ) url-or-port ) )
2018-10-01 18:40:31 -04:00
( let ( ( breadcrumbs ( generate-breadcrumbs ( ein:$notebooklist-path ein:%notebooklist% ) ) ) )
( dolist ( p breadcrumbs )
( lexical-let ( ( url-or-port url-or-port )
( name ( car p ) )
( path ( cdr p ) ) )
( widget-insert " | " )
( widget-create
'link
:notify ( lambda ( &rest ignore )
2018-12-01 18:54:58 -05:00
( ein:notebooklist-open* url-or-port path nil
( lambda ( buffer url-or-port )
( pop-to-buffer buffer ) ) ) )
2018-10-01 18:40:31 -04:00
name ) ) )
( widget-insert " | \n \n " ) )
( lexical-let* ( ( url-or-port url-or-port )
( kernels ( ein:list-available-kernels url-or-port ) ) )
( if ( null ein:%notebooklist-new-kernel% )
( setq ein:%notebooklist-new-kernel% ( ein:get-kernelspec url-or-port ( caar kernels ) ) ) )
( widget-create
'link
:notify ( lambda ( &rest ignore ) ( ein:notebooklist-new-notebook
url-or-port
ein:%notebooklist-new-kernel% ) )
" New Notebook " )
( widget-insert " " )
( widget-create
'link
2018-10-24 13:12:16 -04:00
:notify ( lambda ( &rest ignore ) ( ein:notebooklist-reload nil t ) )
2018-10-01 18:40:31 -04:00
" Resync " )
( widget-insert " " )
( widget-create
'link
:notify ( lambda ( &rest ignore )
( browse-url ( ein:url url-or-port ) ) )
" Open In Browser " )
( widget-insert " \n \n Create New Notebooks Using Kernel: \n " )
( let* ( ( radio-widget ( widget-create 'radio-button-choice
:value ( and ein:%notebooklist-new-kernel% ( ein:$kernelspec-name ein:%notebooklist-new-kernel% ) )
:notify ( lambda ( widget &rest ignore )
( setq ein:%notebooklist-new-kernel%
( ein:get-kernelspec url-or-port ( widget-value widget ) ) )
( message " New notebooks will be started using the %s kernel. "
( ein:$kernelspec-display-name ein:%notebooklist-new-kernel% ) ) ) ) ) )
( if ( null kernels )
( widget-insert " \n No kernels found. " )
( dolist ( k kernels )
( widget-radio-add-item radio-widget ( list 'item :value ( car k )
:format ( format " %s \n " ( cdr k ) ) ) ) )
( widget-insert " \n " ) ) ) ) ) )
( defun render-opened-notebooks ( url-or-port &rest args )
2018-03-25 12:32:31 -07:00
" Render the opened notebooks section (for ipython>=3). "
2018-04-22 10:22:11 -05:00
;; Opened Notebooks Section
2018-10-01 18:40:31 -04:00
( with-current-buffer ( ein:notebooklist-get-buffer url-or-port )
( widget-insert " \n ---------- All Opened Notebooks ---------- \n \n " )
( loop for buffer in ( ein:notebook-opened-buffers )
do ( progn ( widget-create
'link
:notify ( lexical-let ( ( buffer buffer ) )
( lambda ( &rest ignore )
2018-10-22 14:49:45 -05:00
( condition-case err
2018-10-15 16:57:22 -04:00
( switch-to-buffer buffer )
( error
( message " %S " err )
2018-10-24 13:12:16 -04:00
( ein:notebooklist-reload ) ) ) ) )
2018-10-01 18:40:31 -04:00
" Open " )
( widget-create
'link
:notify ( lexical-let ( ( buffer buffer ) )
( lambda ( &rest ignore )
2018-10-15 16:57:22 -04:00
( if ( buffer-live-p buffer )
( kill-buffer buffer ) )
2018-10-24 13:12:16 -04:00
( run-at-time 1 nil #' ein:notebooklist-reload ) ) )
2018-10-01 18:40:31 -04:00
" Close " )
( widget-insert " : " ( buffer-name buffer ) )
( widget-insert " \n " ) ) ) ) )
2018-03-31 14:38:25 -07:00
2018-04-22 20:57:01 -05:00
( defun ein:format-nbitem-data ( name last-modified )
( let ( ( dt ( date-to-time last-modified ) ) )
2018-04-23 17:33:13 -04:00
( format " %-40s%+20s " name
2018-05-25 11:43:39 -04:00
( ein:format-time-string ein:notebooklist-date-format dt ) ) ) )
2018-04-22 20:57:01 -05:00
2018-10-01 18:40:31 -04:00
( defun render-directory ( url-or-port sessions )
2018-11-03 16:02:04 -04:00
;; SESSIONS is a hashtable of path to (session-id . kernel-id) pairs
2018-10-01 18:40:31 -04:00
( with-current-buffer ( ein:notebooklist-get-buffer url-or-port )
( widget-insert " \n ------------------------------------------ \n \n " )
2018-05-04 15:07:32 -04:00
( ein:make-sorting-widget " Sort by " ein:notebooklist-sort-field )
( ein:make-sorting-widget " In Order " ein:notebooklist-sort-order )
2018-04-22 20:57:01 -05:00
( widget-insert " \n " )
( loop for note in ( ein:notebooklist--order-data ( ein:$notebooklist-data ein:%notebooklist% )
2018-05-04 15:07:32 -04:00
ein:notebooklist-sort-field
ein:notebooklist-sort-order )
2016-08-31 10:04:06 -05:00
for name = ( plist-get note :name )
for path = ( plist-get note :path )
2018-04-22 20:57:01 -05:00
for last-modified = ( plist-get note :last_modified )
2018-03-31 13:58:01 -07:00
;; (cond ((= 2 api-version)
;; (plist-get note :path))
;; ((= 3 api-version)
;; (ein:get-actual-path (plist-get note :path))))
2016-08-31 10:04:06 -05:00
for type = ( plist-get note :type )
2018-10-01 18:40:31 -04:00
for opened-notebook-maybe = ( ein:notebook-get-opened-notebook url-or-port path )
2016-08-31 10:04:06 -05:00
do ( widget-insert " " )
if ( string= type " directory " )
do ( progn ( widget-create
'link
2018-10-01 18:40:31 -04:00
:notify ( lexical-let ( ( url-or-port url-or-port )
( name name ) )
2016-08-31 10:04:06 -05:00
( lambda ( &rest ignore )
2018-10-01 18:40:31 -04:00
;; each directory creates a whole new notebooklist
2018-10-15 16:57:22 -04:00
( ein:notebooklist-open* url-or-port
2018-12-01 18:54:58 -05:00
( concat ( file-name-as-directory
( ein:$notebooklist-path ein:%notebooklist% ) )
name )
nil
( lambda ( buffer url-or-port ) ( pop-to-buffer buffer ) ) ) ) )
2016-08-31 10:04:06 -05:00
" Dir " )
( widget-insert " : " name )
( widget-insert " \n " ) )
2018-12-25 10:59:18 -05:00
if ( and ( string= type " file " ) ( > ( ein:notebook-version-numeric url-or-port ) 2 ) )
2018-03-31 13:58:01 -07:00
do ( progn ( widget-create
'link
2018-10-01 18:40:31 -04:00
:notify ( lexical-let ( ( url-or-port url-or-port )
2018-03-31 13:58:01 -07:00
( path path ) )
( lambda ( &rest ignore )
2018-10-24 13:12:16 -04:00
( ein:file-open url-or-port path ) ) )
2018-03-31 13:58:01 -07:00
" Open " )
2018-04-22 20:57:01 -05:00
( widget-insert " ------ " )
2018-03-31 13:58:01 -07:00
( widget-create
'link
:notify ( lexical-let ( ( path path ) )
( lambda ( &rest ignore )
( message " [EIN]: NBlist delete file command. Implement me! " ) ) )
" Delete " )
2018-04-22 20:57:01 -05:00
( widget-insert " : " ( ein:format-nbitem-data name last-modified ) )
2018-03-31 13:58:01 -07:00
( widget-insert " \n " ) )
2016-08-31 10:04:06 -05:00
if ( string= type " notebook " )
do ( progn ( widget-create
'link
2018-10-24 13:12:16 -04:00
:notify ( lexical-let ( ( url-or-port url-or-port )
2016-08-31 10:04:06 -05:00
( path path ) )
( lambda ( &rest ignore )
2018-10-24 13:12:16 -04:00
( run-at-time 3 nil #' ein:notebooklist-reload ) ;; TODO using deferred better?
( ein:notebook-open url-or-port path ) ) )
2018-11-05 11:46:20 -05:00
2016-08-31 10:04:06 -05:00
" Open " )
( widget-insert " " )
2018-04-22 20:57:01 -05:00
( if ( gethash path sessions )
( widget-create
'link
2018-11-09 18:27:23 -06:00
:notify ( lexical-let ( ( url url-or-port )
2018-11-03 16:02:04 -04:00
( session ( car ( gethash path sessions ) ) ) )
2018-04-22 20:57:01 -05:00
( lambda ( &rest ignore )
2018-11-09 18:53:40 -06:00
( ein:kernel-delete--from-session-id url session #' ein:notebooklist-reload ) ) )
2018-04-22 20:57:01 -05:00
" Stop " )
( widget-insert " ------ " ) )
( widget-insert " " )
2016-08-31 10:04:06 -05:00
( widget-create
'link
:notify ( lexical-let ( ( path path ) )
( lambda ( &rest ignore )
( ein:notebooklist-delete-notebook-ask
path ) ) )
" Delete " )
2018-04-22 20:57:01 -05:00
( widget-insert " : " ( ein:format-nbitem-data name last-modified ) )
2018-04-22 10:22:11 -05:00
( widget-insert " \n " ) ) ) ) )
2018-03-25 12:32:31 -07:00
2018-10-24 13:12:16 -04:00
( defun ein:notebooklist-render ( nb-version )
2018-03-25 12:32:31 -07:00
" Render notebook list widget.
Notebook list data is passed via the buffer local variable
` ein:notebooklist-data '. "
( kill-all-local-variables )
( let ( ( inhibit-read-only t ) )
( erase-buffer ) )
( remove-overlays )
2018-10-01 18:40:31 -04:00
( let ( ( url-or-port ( ein:$notebooklist-url-or-port ein:%notebooklist% ) ) )
2018-10-28 14:27:09 -04:00
( ein:content-query-sessions url-or-port
( apply-partially #' ein:notebooklist-render--finish nb-version url-or-port )
nil ) ) )
2018-10-01 18:40:31 -04:00
2018-10-24 13:12:16 -04:00
( defun ein:notebooklist-render--finish ( nb-version url-or-port sessions )
( cl-letf ( ( ( symbol-function 'render-header ) ( if ( < nb-version 3 )
2018-10-12 15:32:51 -05:00
#' render-header-ipy2
2018-10-01 18:40:31 -04:00
#' render-header* ) ) )
( mapc ( lambda ( x ) ( funcall ( symbol-function x ) url-or-port sessions ) )
ein:notebooklist-render-order ) )
( with-current-buffer ( ein:notebooklist-get-buffer url-or-port )
( ein:notebooklist-mode )
( widget-setup )
( goto-char ( point-min ) ) ) )
2012-05-07 14:41:15 +02:00
2018-03-25 13:42:35 -07:00
;;;###autoload
2018-10-01 18:40:31 -04:00
2018-10-24 13:12:16 -04:00
( defun ein:notebooklist-list-paths ( &optional content-type )
" Return all files of CONTENT-TYPE for all sessions "
2012-07-12 01:05:37 +02:00
( apply #' append
( loop for nblist in ( ein:notebooklist-list )
for url-or-port = ( ein:$notebooklist-url-or-port nblist )
collect
2018-10-01 18:40:31 -04:00
( loop for content in ( ein:content-need-hierarchy url-or-port )
2018-12-01 18:54:58 -05:00
when ( or ( null content-type )
( string= ( ein:$content-type content ) content-type ) )
2018-10-24 13:12:16 -04:00
collect ( ein:url url-or-port ( ein:$content-path content ) ) ) ) ) )
2018-10-01 18:40:31 -04:00
2018-12-01 18:54:58 -05:00
( defun ein:notebooklist-parse-nbpath ( nbpath )
2018-10-24 13:12:16 -04:00
" Return ` (,url-or-port ,path) from URL-OR-PORT/PATH "
2018-12-01 18:54:58 -05:00
( loop for url-or-port in ( ein:hash-keys ein:notebooklist-map )
if ( search url-or-port nbpath :end2 ( length url-or-port ) )
return ( list ( substring nbpath 0 ( length url-or-port ) )
( substring nbpath ( 1+ ( length url-or-port ) ) ) )
end
finally ( ein:display-warning
( format " %s not among: %s " nbpath ( ein:hash-keys ein:notebooklist-map ) )
:error ) ) )
2018-10-24 13:12:16 -04:00
( defsubst ein:notebooklist-ask-path ( &optional content-type )
( ido-completing-read ( format " Open %s: " content-type )
( ein:notebooklist-list-paths content-type )
nil t ) )
2012-06-05 14:54:18 +02:00
2012-09-01 20:51:55 +02:00
;;;###autoload
2018-10-01 18:40:31 -04:00
2012-08-26 22:12:49 +02:00
( defun ein:notebooklist-load ( &optional url-or-port )
" Load notebook list but do not pop-up the notebook list buffer.
For example, if you want to load notebook list when Emacs starts,
add this in the Emacs initialization file::
( add-to-hook 'after-init-hook 'ein:notebooklist-load )
or even this ( if you want fast Emacs start-up ) : :
;; load notebook list if Emacs is idle for 3 sec after start-up
( run-with-idle-timer 3 nil #' ein:notebooklist-load )
You should setup ` ein:url-or-port ' or ` ein:default-url-or-port '
in order to make this code work.
See also:
` ein:connect-to-default-notebook ', ` ein:connect-default-notebook '. "
2018-12-01 18:54:58 -05:00
( ein:notebooklist-open* url-or-port ) )
2012-12-17 16:51:31 +01:00
;;; Login
2018-10-17 19:34:19 -04:00
( defun ein:notebooklist-login--iteration ( url-or-port callback errback token iteration response-status )
2018-10-22 14:52:46 -05:00
( ein:log 'debug " Login attempt #%d in response to %s from %s. "
2018-10-17 19:34:19 -04:00
iteration response-status url-or-port )
2018-10-24 13:12:16 -04:00
( unless callback
( setq callback #' ignore ) )
( unless errback
( setq errback #' ignore ) )
2018-12-01 18:54:58 -05:00
( ein:query-singleton-ajax
( list 'notebooklist-login--iteration url-or-port )
( ein:url url-or-port " login " )
;; do not use :type "POST" here (see git history)
:timeout ein:notebooklist-login-timeout
:data ( if token ( concat " password= " ( url-hexify-string token ) ) )
:parser #' ein:notebooklist-login--parser
:complete ( apply-partially #' ein:notebooklist-login--complete url-or-port )
:error ( apply-partially #' ein:notebooklist-login--error url-or-port token
callback errback iteration )
:success ( apply-partially #' ein:notebooklist-login--success url-or-port callback
errback token iteration ) ) )
2018-10-15 16:57:22 -04:00
2018-10-17 17:13:19 -04:00
;;;###autoload
( defun ein:notebooklist-open ( url-or-port callback )
" This is now an alias for ein:notebooklist-login "
2018-12-01 18:54:58 -05:00
( interactive ` ( , ( ein:notebooklist-ask-url-or-port )
, ( lambda ( buffer url-or-port ) ( pop-to-buffer buffer ) ) ) )
2018-10-17 17:13:19 -04:00
( ein:notebooklist-login url-or-port callback ) )
( make-obsolete 'ein:notebooklist-open 'ein:notebooklist-login " 0.14.2 " )
2018-10-15 16:57:22 -04:00
2016-12-27 12:03:05 -05:00
;;;###autoload
2018-10-15 16:57:22 -04:00
( defalias 'ein:login 'ein:notebooklist-login )
2018-10-01 18:40:31 -04:00
2018-12-01 18:54:58 -05:00
( defun ein:notebooklist-ask-user-pw-pair ( user-prompt pw-prompt )
" Currently used for cookie and jupyterhub additional inputs. If we need more than one cookie, we first need to ask for how many. Returns list of name and content. "
( plist-put nil ( intern ( read-no-blanks-input ( format " %s: " user-prompt ) ) )
( read-no-blanks-input ( format " %s: " pw-prompt ) ) ) )
2018-10-29 00:51:33 -04:00
2018-10-15 16:57:22 -04:00
;;;###autoload
2018-10-29 00:51:33 -04:00
( defun ein:notebooklist-login ( url-or-port callback &optional cookie-plist )
2018-10-17 17:44:11 -04:00
" Deal with security before main entry of ein:notebooklist-open*.
2018-10-15 16:57:22 -04:00
2018-12-01 18:54:58 -05:00
CALLBACK takes two arguments, the buffer created by ein:notebooklist-open--success
and the url-or-port argument of ein:notebooklist-open*. "
2018-11-05 11:46:20 -05:00
( interactive ` ( , ( ein:notebooklist-ask-url-or-port )
2018-12-01 18:54:58 -05:00
, ( lambda ( buffer url-or-port ) ( pop-to-buffer buffer ) )
, ( if current-prefix-arg ( ein:notebooklist-ask-user-pw-pair " Cookie name " " Cookie content " ) ) ) )
( unless callback ( setq callback ( lambda ( buffer url-or-port ) ) ) )
2018-10-29 00:51:33 -04:00
( when cookie-plist
( let* ( ( parsed-url ( url-generic-parse-url ( file-name-as-directory url-or-port ) ) )
( domain ( url-host parsed-url ) )
( securep ( string-match " ^wss:// " url-or-port ) ) )
( loop for ( name content ) on cookie-plist by ( function cddr )
for line = ( mapconcat #' identity ( list domain " FALSE " ( car ( url-path-and-query parsed-url ) ) ( if securep " TRUE " " FALSE " ) " 0 " ( symbol-name name ) ( concat content " \n " ) ) " \t " )
2018-11-08 11:49:24 -05:00
do ( write-region line nil ( request--curl-cookie-jar ) 'append ) ) ) )
2018-10-29 00:51:33 -04:00
2018-12-02 15:23:20 -05:00
( let ( ( token ( ein:notebooklist-token-or-password url-or-port ) ) )
( cond ( ( null token ) ;; don't know
( ein:notebooklist-login--iteration url-or-port callback nil nil -1 nil ) )
( ( string= token " " ) ;; all authentication disabled
( ein:log 'verbose " Skipping login %s " url-or-port )
( ein:notebooklist-open* url-or-port nil nil callback nil ) )
( t ( ein:notebooklist-login--iteration url-or-port callback nil token 0 nil ) ) ) ) )
2012-12-17 19:26:13 +01:00
( defun ein:notebooklist-login--parser ( )
( goto-char ( point-min ) )
2013-01-10 19:25:08 +01:00
( list :bad-page ( re-search-forward " <input type=.?password " nil t ) ) )
2012-12-17 19:26:13 +01:00
2018-10-28 14:27:09 -04:00
( defun ein:notebooklist-login--success-1 ( url-or-port callback errback )
2018-10-15 16:57:22 -04:00
( ein:log 'info " Login to %s complete. " url-or-port )
2018-10-28 14:27:09 -04:00
( ein:notebooklist-open* url-or-port nil nil callback errback ) )
2012-12-17 19:26:13 +01:00
2018-10-15 16:57:22 -04:00
( defun ein:notebooklist-login--error-1 ( url-or-port errback )
( ein:log 'error " Login to %s failed " url-or-port )
( funcall errback ) )
2012-12-17 19:26:13 +01:00
2018-10-11 16:53:02 -04:00
( defun* ein:notebooklist-login--complete ( url-or-port &key data response
2018-10-22 14:49:45 -05:00
&allow-other-keys
2018-10-11 16:53:02 -04:00
&aux ( resp-string ( format " STATUS: %s DATA: %s " ( request-response-status-code response ) data ) ) )
( ein:log 'debug " ein:notebooklist-login--complete %s " resp-string ) )
2018-10-17 19:34:19 -04:00
( defun* ein:notebooklist-login--success ( url-or-port callback errback token iteration
&key data response
&allow-other-keys
&aux
( response-status ( request-response-status-code response ) ) )
2018-12-01 18:54:58 -05:00
( cond ( ( plist-get data :bad-page )
( if ( >= iteration 0 )
( ein:notebooklist-login--error-1 url-or-port errback )
( setq token ( read-passwd ( format " Password for %s: " url-or-port ) ) )
( ein:notebooklist-login--iteration url-or-port callback errback token ( 1+ iteration ) response-status ) ) )
( ( request-response-header response " x-jupyterhub-version " )
( let ( ( pam-plist ( ein:notebooklist-ask-user-pw-pair " User " " Password " ) ) )
( destructuring-bind ( user pw )
( loop for ( user pw ) on pam-plist by ( function cddr )
return ( list ( symbol-name user ) pw ) )
( ein:jupyterhub-connect url-or-port user pw callback ) ) ) )
( t ( ein:notebooklist-login--success-1 url-or-port callback errback ) ) ) )
2012-12-17 19:26:13 +01:00
2013-01-09 05:51:25 +01:00
( defun* ein:notebooklist-login--error
2018-10-17 19:34:19 -04:00
( url-or-port token callback errback iteration &key
2013-01-09 05:51:25 +01:00
data
symbol-status
response
&allow-other-keys
&aux
( response-status ( request-response-status-code response ) ) )
2018-10-24 13:12:16 -04:00
( cond ( ( and response-status ( < iteration 0 ) )
( setq token ( read-passwd ( format " Password for %s: " url-or-port ) ) )
2018-10-17 19:34:19 -04:00
( ein:notebooklist-login--iteration url-or-port callback errback token ( 1+ iteration ) response-status ) )
( ( and ( eq response-status 403 ) ( < iteration 1 ) )
( ein:notebooklist-login--iteration url-or-port callback errback token ( 1+ iteration ) response-status ) )
( ( and ( eq symbol-status 'timeout ) ;; workaround for url-retrieve backend
( eq response-status 302 )
( request-response-header response " set-cookie " ) )
2018-10-28 14:27:09 -04:00
( ein:notebooklist-login--success-1 url-or-port callback errback ) )
2018-10-15 16:57:22 -04:00
( t ( ein:notebooklist-login--error-1 url-or-port errback ) ) ) )
2018-12-11 08:48:40 -05:00
;;;###autoload
( defun ein:notebooklist-change-url-port ( new-url-or-port )
" Update the ipython/jupyter notebook server URL for all the
notebooks currently opened from the current notebooklist buffer.
This function works by calling ` ein:notebook-update-url-or-port '
on all the notebooks opened from the current notebooklist. "
( interactive ( list ( ein:notebooklist-ask-url-or-port ) ) )
( unless ( eql major-mode 'ein:notebooklist-mode )
( error " This command needs to be called from within a notebooklist buffer. " ) )
( let* ( ( current-nblist ein:%notebooklist% )
( old-url ( ein:$notebooklist-url-or-port current-nblist ) )
( new-url-or-port new-url-or-port )
( open-nb ( ein:notebook-opened-notebooks #' ( lambda ( nb )
( equal ( ein:$notebook-url-or-port nb )
( ein:$notebooklist-url-or-port current-nblist ) ) ) ) ) )
( ein:notebooklist-open* new-url-or-port )
( loop for x upfrom 0 by 1
until ( or ( get-buffer ( format ein:notebooklist-buffer-name-template new-url-or-port ) )
( = x 100 ) )
do ( sit-for 0.1 ) )
( dolist ( nb open-nb )
( ein:notebook-update-url-or-port new-url-or-port nb ) )
( kill-buffer ( ein:notebooklist-get-buffer old-url ) )
( ein:notebooklist-open* new-url-or-port nil nil ( lambda ( buffer url-or-port )
( pop-to-buffer buffer ) ) ) ) )
( defun ein:notebooklist-change-url-port--deferred ( new-url-or-port )
( lexical-let* ( ( current-nblist ein:%notebooklist% )
( old-url ( ein:$notebooklist-url-or-port current-nblist ) )
( new-url-or-port new-url-or-port )
( open-nb ( ein:notebook-opened-notebooks
( lambda ( nb )
( equal ( ein:$notebook-url-or-port nb )
( ein:$notebooklist-url-or-port current-nblist ) ) ) ) ) )
( deferred:$
( deferred:next
( lambda ( )
( ein:notebooklist-open* new-url-or-port )
( loop until ( get-buffer ( format ein:notebooklist-buffer-name-template new-url-or-port ) )
do ( sit-for 0.1 ) ) ) )
( deferred:nextc it
( lambda ( )
( dolist ( nb open-nb )
( ein:notebook-update-url-or-port new-url-or-port nb ) ) ) )
( deferred:nextc it
( lambda ( )
( kill-buffer ( ein:notebooklist-get-buffer old-url ) )
( ein:notebooklist-open* new-url-or-port nil nil ( lambda ( buffer url-or-port )
( pop-to-buffer buffer ) ) ) ) ) ) ) )
2012-08-13 18:55:32 +02:00
;;; Generic getter
2017-05-24 09:53:58 -05:00
2018-10-01 18:40:31 -04:00
2012-08-13 18:55:32 +02:00
( defun ein:get-url-or-port--notebooklist ( )
2012-08-18 22:31:15 +02:00
( when ( ein:$notebooklist-p ein:%notebooklist% )
( ein:$notebooklist-url-or-port ein:%notebooklist% ) ) )
2012-08-13 18:55:32 +02:00
2012-05-15 03:41:08 +02:00
;;; Notebook list mode
2018-10-01 18:40:31 -04:00
2012-05-15 04:16:06 +02:00
( defun ein:notebooklist-prev-item ( ) ( interactive ) ( move-beginning-of-line 0 ) )
( defun ein:notebooklist-next-item ( ) ( interactive ) ( move-beginning-of-line 2 ) )
2016-09-23 10:37:48 -05:00
( defvar ein:notebooklist-mode-map
( let ( ( map ( make-sparse-keymap ) ) )
( set-keymap-parent map ( make-composed-keymap widget-keymap
special-mode-map ) )
( define-key map " \C -c \C -r " 'ein:notebooklist-reload )
2018-10-24 13:12:16 -04:00
( define-key map " \C -c \C -f " 'ein:file-open )
( define-key map " \C -c \C -o " 'ein:notebook-open )
2016-09-23 10:37:48 -05:00
( define-key map " p " 'ein:notebooklist-prev-item )
( define-key map " n " 'ein:notebooklist-next-item )
map )
" Keymap for ein:notebooklist-mode. " )
( easy-menu-define ein:notebooklist-menu ein:notebooklist-mode-map
" EIN Notebook List Mode Menu "
` ( " EIN Notebook List "
,@ ( ein:generate-menu
' ( ( " Reload " ein:notebooklist-reload )
( " New Notebook " ein:notebooklist-new-notebook )
( " New Notebook (with name) "
2018-12-05 17:15:02 -05:00
ein:notebooklist-new-notebook-with-name ) ) ) ) )
2012-05-15 03:41:08 +02:00
2016-09-19 09:02:35 -05:00
( defun ein:notebooklist-revert-wrapper ( &optional ignore-auto noconfirm preserve-modes )
2018-10-24 13:12:16 -04:00
( ein:notebooklist-reload ) )
2016-09-19 09:02:35 -05:00
( define-derived-mode ein:notebooklist-mode special-mode " ein:notebooklist "
" IPython notebook list mode.
Commands:
2018-09-04 11:54:37 -04:00
\\{ein:notebooklist-mode-map} "
2016-09-19 09:02:35 -05:00
( set ( make-local-variable 'revert-buffer-function )
'ein:notebooklist-revert-wrapper ) )
2012-05-07 14:41:15 +02:00
( provide 'ein-notebooklist )
;;; ein-notebooklist.el ends here