From a384ba15de4312760ba4e5baaf9f8308984eeb16 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 6 Dec 2012 19:48:37 +0100 Subject: [PATCH 1/9] Make ein:console-open work without python.el --- lisp/ein-console.el | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/lisp/ein-console.el b/lisp/ein-console.el index 92ae538..413601a 100644 --- a/lisp/ein-console.el +++ b/lisp/ein-console.el @@ -134,14 +134,28 @@ Types same as `ein:console-security-dir' are valid." (ein:choose-setting 'ein:console-args url-or-port)) (defun ein:console-make-command () + ;; FIXME: use %connect_info to get connection file, then I can get + ;; rid of `ein:console-security-dir'. (let* ((url-or-port (or (ein:get-url-or-port) (error "Cannot find notebook to connect!"))) (dir (ein:console-security-dir-get url-or-port)) (kid (ein:kernel-id (ein:get-kernel))) (ipy (ein:console-executable-get url-or-port)) (args (ein:console-args-get url-or-port))) - (format "python %s console --existing %skernel-%s.json %s" - ipy dir kid args))) + ;; FIMXE: do I need "python" here? + (append (list "python" ipy "console" "--existing" + (format "%skernel-%s.json" dir kid)) + (if (listp args) + args + (ein:display-warning + "String value for `ein:console-args' is obsolete. +Use list of string instead of space separated string.") + (split-string-and-unquote args))))) + +(defun ein:console-get-buffer () + (let ((url-or-port (ein:get-url-or-port)) + (notebook (ein:get-notebook))) + (format "*ein:console %s/%s*" url-or-port (ein:notebook-name notebook)))) ;;;###autoload (defun ein:console-open () @@ -152,11 +166,10 @@ This function requires `Fabian Gallina's python.el`_ for now; It should be possible to support python-mode.el. Patches are welcome! .. _`Fabian Gallina's python.el`: https://github.com/fgallina/python.el" - ;; FIXME: use %connect_info to get connection file, then I can get - ;; rid of `ein:console-security-dir'. (interactive) (if (fboundp 'python-shell-switch-to-shell) - (let ((cmd (ein:console-make-command)) + (let ((cmd (mapconcat #'shell-quote-argument + (ein:console-make-command) " ")) ;; python.el settings: (python-shell-setup-codes nil) ;; python.el makes dedicated process when @@ -169,9 +182,14 @@ It should be possible to support python-mode.el. Patches are welcome! (python-shell-make-comint cmd (python-shell-get-process-name t)) ;; Pop to inferior Python process buffer (python-shell-switch-to-shell)) - (error "python.el is not loaded!"))) + (let* ((command (ein:console-make-command)) + (program (car command)) + (args (cdr command)) + (buffer (ein:console-get-buffer))) + (apply #'make-comint-in-buffer + "ein:console" buffer program nil args) + (pop-to-buffer buffer)))) (provide 'ein-console) ;;; ein-console.el ends here - From a3a01483753403d7085e61b920fd13af0d667fc2 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 6 Dec 2012 20:14:24 +0100 Subject: [PATCH 2/9] zeroein.el works with --eval options now --- lisp/zeroein.el | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lisp/zeroein.el b/lisp/zeroein.el index e62a72b..52438d3 100755 --- a/lisp/zeroein.el +++ b/lisp/zeroein.el @@ -109,10 +109,12 @@ ;;; Finally, open notebook list -(require 'ein-dev) -(ein:dev-print-sys-info) - -(unless noninteractive - (call-interactively #'ein:notebooklist-open)) +(if noninteractive + (progn + ;; When called in batch mode, print system info. + (require 'ein-dev) + (ein:dev-print-sys-info)) + ;; To make EIN configurable by --eval, use idle timer: + (run-with-idle-timer 0 nil 'call-interactively 'ein:notebooklist-open)) ;;; zeroein.el ends here From bd122efe7e46910ab5253eeb2e06cacc067fe487 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 6 Dec 2012 20:51:57 +0100 Subject: [PATCH 3/9] Add ein:display-warning-once and use it in ein:console-make-command --- lisp/ein-console.el | 2 +- lisp/ein-utils.el | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lisp/ein-console.el b/lisp/ein-console.el index 413601a..6046291 100644 --- a/lisp/ein-console.el +++ b/lisp/ein-console.el @@ -147,7 +147,7 @@ Types same as `ein:console-security-dir' are valid." (format "%skernel-%s.json" dir kid)) (if (listp args) args - (ein:display-warning + (ein:display-warning-once "String value for `ein:console-args' is obsolete. Use list of string instead of space separated string.") (split-string-and-unquote args))))) diff --git a/lisp/ein-utils.el b/lisp/ein-utils.el index 9604edc..b1b1706 100644 --- a/lisp/ein-utils.el +++ b/lisp/ein-utils.el @@ -506,6 +506,16 @@ Use `ein:log' for debugging and logging." ;; FIXME: Call `ein:log' here (but do not display in minibuffer). (display-warning 'ein message level)) +(defvar ein:display-warning-once--db + (make-hash-table :test 'equal)) + +(defun ein:display-warning-once (message &optional level) + "Call `ein:display-warning' once for same MESSAGE and LEVEL." + (let ((key (list message level))) + (unless (gethash key ein:display-warning-once--db) + (ein:display-warning message level) + (puthash key t ein:display-warning-once--db)))) + (defun ein:get-docstring (function) "Return docstring of FUNCTION." ;; Borrowed from `ac-symbol-documentation'. From 827717cd0dec3772264409d04b0f1879e1fdf244 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 6 Dec 2012 21:34:21 +0100 Subject: [PATCH 4/9] ein:console-args can take a list as value now --- lisp/ein-console.el | 13 ++++++++++--- lisp/ein-utils.el | 9 ++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lisp/ein-console.el b/lisp/ein-console.el index 6046291..d5dfd2b 100644 --- a/lisp/ein-console.el +++ b/lisp/ein-console.el @@ -101,10 +101,13 @@ Types same as `ein:console-security-dir' are valid." (lambda (url-or-port) (executable-find "ipython")))) :group 'ein) -(defcustom ein:console-args "--profile nbserver" +(defcustom ein:console-args '("--profile" "nbserver") "Additional argument when using console. -Example: ``\"--ssh HOSTNAME\"``. +.. warning:: Space-separated string is obsolete now. Use a list + of string as value now. + +Example: ``(\"--ssh\" \"HOSTNAME\")``. Types same as `ein:console-security-dir' are valid." :type '(choice (string :tag "Arguments to IPython" @@ -131,7 +134,11 @@ Types same as `ein:console-security-dir' are valid." (ein:choose-setting 'ein:console-executable url-or-port)) (defun ein:console-args-get (url-or-port) - (ein:choose-setting 'ein:console-args url-or-port)) + (ein:choose-setting 'ein:console-args url-or-port + (lambda (x) + (or (stringp x) + (and (listp x) + (stringp (car x))))))) (defun ein:console-make-command () ;; FIXME: use %connect_info to get connection file, then I can get diff --git a/lisp/ein-utils.el b/lisp/ein-utils.el index b1b1706..b3c6563 100644 --- a/lisp/ein-utils.el +++ b/lisp/ein-utils.el @@ -424,12 +424,15 @@ Elements are compared using the function TEST (default: `eq')." ((boundp obj) (eval obj)) ((fboundp obj) (funcall obj)))) -(defun ein:choose-setting (symbol value) +(defun ein:choose-setting (symbol value &optional single-p) "Choose setting in stored in SYMBOL based on VALUE. -The value of SYMBOL can be string, alist or function." +The value of SYMBOL can be string, alist or function. +SINGLE-P is a function which takes one argument. It must +return t when the value of SYMBOL can be used as a setting. +SINGLE-P is `stringp' by default." (let ((setting (eval symbol))) (cond - ((stringp setting) setting) + ((funcall (or single-p 'stringp) setting) setting) ((functionp setting) (funcall setting value)) ((listp setting) (ein:get-value (or (assoc-default value setting) From 1fdfda039c694ee9220c0c16832ee8f3ae737f46 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 6 Dec 2012 21:35:39 +0100 Subject: [PATCH 5/9] Add tests for ein:choose-setting --- tests/test-ein-utils.el | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/test-ein-utils.el b/tests/test-ein-utils.el index 3a4bfa5..c57357c 100644 --- a/tests/test-ein-utils.el +++ b/tests/test-ein-utils.el @@ -149,3 +149,34 @@ def f(): (should (equal (ein:list-move-right '(a b c d) 'c) '(a b d c))) (should (equal (ein:list-move-right '(a b c d) 'd) '(d a b c))) (should-error (ein:list-move-right '(a b c d) 'X))) + +(defun ein:testing-choose-setting-should-equal + (setting value desired &optional single-p) + (let ((setting setting)) + (should (equal (ein:choose-setting 'setting value single-p) desired)))) + +(ert-deftest ein:choose-setting-single-string () + (let ((test 'ein:testing-choose-setting-should-equal)) + (funcall test "a" nil "a") + (funcall test "a" 'whatever "a"))) + +(ert-deftest ein:choose-setting-single-int () + (let ((test #'ein:testing-choose-setting-should-equal)) + (funcall test 1 nil 1 #'integerp) + (funcall test 1 'whatever 1 #'integerp))) + +(ert-deftest ein:choose-setting-alist () + (let* ((test (lambda (&rest args) + (apply #'ein:testing-choose-setting-should-equal + '(("a" . 1) ("b" . 2) ("c" . 3)) + args)))) + (funcall test "a" 1) + (funcall test "b" 2))) + +(ert-deftest ein:choose-setting-func () + (let* ((test (lambda (&rest args) + (apply #'ein:testing-choose-setting-should-equal + (lambda (x) 1) + args)))) + (funcall test nil 1) + (funcall test 'whatever 1))) From a46990ebda2cb64cabe66790e8ee019587b752f8 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 6 Dec 2012 22:20:45 +0100 Subject: [PATCH 6/9] Improve ein:console-args docstring --- lisp/ein-console.el | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lisp/ein-console.el b/lisp/ein-console.el index d5dfd2b..e8b2cf9 100644 --- a/lisp/ein-console.el +++ b/lisp/ein-console.el @@ -107,8 +107,29 @@ Types same as `ein:console-security-dir' are valid." .. warning:: Space-separated string is obsolete now. Use a list of string as value now. -Example: ``(\"--ssh\" \"HOSTNAME\")``. -Types same as `ein:console-security-dir' are valid." +Setting to use IPython profile named \"YOUR-IPYTHON-PROFILE\":: + + (setq ein:console-args '(\"--profile\" \"YOUR-IPYTHON-PROFILE\")) + +Together with `ein:console-security-dir', you can open IPython +console connecting to a remote kernel.:: + + (setq ein:console-args '(\"--ssh\" \"HOSTNAME\")) + (setq ein:console-security-dir \"PATH/TO/SECURITY/DIR\") + +You can setup `ein:console-args' per server basis using alist form:: + + (setq ein:console-args + '((8888 . '(\"--profile\" \"PROFILE\")) + (8889 . '(\"--ssh\" \"HOSTNAME\")) + (default . '(\"--profile\" \"default\")))) + +If you want to use more complex setting, you can set a function to it:: + + (setq ein:console-args + (lambda (url-or-port) '(\"--ssh\" \"HOSTNAME\"))) + +See also: `ein:console-security-dir'." :type '(choice (string :tag "Arguments to IPython" "--profile nbserver --ssh HOSTNAME") From ac417dd6cf4b1ee5492b5cd2d66017120d9feb29 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 6 Dec 2012 22:30:59 +0100 Subject: [PATCH 7/9] Adapt defcustom type to the current ein:console-args --- lisp/ein-console.el | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lisp/ein-console.el b/lisp/ein-console.el index e8b2cf9..ac5c321 100644 --- a/lisp/ein-console.el +++ b/lisp/ein-console.el @@ -131,18 +131,17 @@ If you want to use more complex setting, you can set a function to it:: See also: `ein:console-security-dir'." :type '(choice - (string :tag "Arguments to IPython" - "--profile nbserver --ssh HOSTNAME") + (repeat (string :tag "Arguments to IPython" "--profile")) (alist :tag "Arguments mapping" :key-type (choice :tag "URL or PORT" (string :tag "URL" "http://127.0.0.1:8888") (integer :tag "PORT" 8888) (const default)) - :value-type (string :tag "Arguments to IPython" - "--profile nbserver --ssh HOSTNAME")) + :value-type + (repeat (string :tag "Arguments to IPython" "--profile"))) (function :tag "Additional arguments getter" (lambda (url-or-port) - (format "--ssh %s" url-or-port)))) + (list "--ssh" (format "%s" url-or-port))))) :group 'ein) (defun ein:console-security-dir-get (url-or-port) From 83b420431c7d0d784de272cb0a46863a09213adb Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 6 Dec 2012 22:47:45 +0100 Subject: [PATCH 8/9] Update ein:console-open docstring --- lisp/ein-console.el | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lisp/ein-console.el b/lisp/ein-console.el index ac5c321..cd34a7a 100644 --- a/lisp/ein-console.el +++ b/lisp/ein-console.el @@ -189,10 +189,13 @@ Use list of string instead of space separated string.") "Open IPython console. To use this function, `ein:console-security-dir' and `ein:console-args' must be set properly. -This function requires `Fabian Gallina's python.el`_ for now; +This function works best with the new python.el_ which is shipped +with Emacs 24.2 or later. If you don't have it, this function +opens a \"plain\" command line interpreter (comint) buffer where +you cannot use fancy stuff such as TAB completion. It should be possible to support python-mode.el. Patches are welcome! -.. _`Fabian Gallina's python.el`: https://github.com/fgallina/python.el" +.. _python.el: https://github.com/fgallina/python.el" (interactive) (if (fboundp 'python-shell-switch-to-shell) (let ((cmd (mapconcat #'shell-quote-argument From 93b5e30705c7311645a9dcd26909069b687a3864 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 6 Dec 2012 22:49:53 +0100 Subject: [PATCH 9/9] Update changelog --- doc/source/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/index.rst b/doc/source/index.rst index a1f6bf1..702cc2b 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -543,6 +543,7 @@ Change Log v0.2 ---- +* :el:symbol:`ein:console-open` works without `python.el`_. * Expand code cell output on execution. (`#88 `_). * Improve :el:symbol:`ein:completer-dot-complete` and