diff options
Diffstat (limited to 'lisp/org/ob-python.el')
| -rw-r--r-- | lisp/org/ob-python.el | 276 |
1 files changed, 276 insertions, 0 deletions
diff --git a/lisp/org/ob-python.el b/lisp/org/ob-python.el new file mode 100644 index 00000000000..c082188bea7 --- /dev/null +++ b/lisp/org/ob-python.el | |||
| @@ -0,0 +1,276 @@ | |||
| 1 | ;;; ob-python.el --- org-babel functions for python evaluation | ||
| 2 | |||
| 3 | ;; Copyright (C) 2009, 2010 Free Software Foundation | ||
| 4 | |||
| 5 | ;; Author: Eric Schulte, Dan Davison | ||
| 6 | ;; Keywords: literate programming, reproducible research | ||
| 7 | ;; Homepage: http://orgmode.org | ||
| 8 | ;; Version: 7.01 | ||
| 9 | |||
| 10 | ;; This file is part of GNU Emacs. | ||
| 11 | |||
| 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 13 | ;; it under the terms of the GNU General Public License as published by | ||
| 14 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 15 | ;; (at your option) any later version. | ||
| 16 | |||
| 17 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 20 | ;; GNU General Public License for more details. | ||
| 21 | |||
| 22 | ;; You should have received a copy of the GNU General Public License | ||
| 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | ||
| 24 | |||
| 25 | ;;; Commentary: | ||
| 26 | |||
| 27 | ;; Org-Babel support for evaluating python source code. | ||
| 28 | |||
| 29 | ;;; Code: | ||
| 30 | (require 'ob) | ||
| 31 | (require 'ob-ref) | ||
| 32 | (require 'ob-comint) | ||
| 33 | (require 'ob-eval) | ||
| 34 | (eval-when-compile (require 'cl)) | ||
| 35 | |||
| 36 | (declare-function org-remove-indentation "org" ) | ||
| 37 | (declare-function py-shell "ext:python-mode" (&optional argprompt)) | ||
| 38 | (declare-function run-python "ext:python" (&optional cmd noshow new)) | ||
| 39 | |||
| 40 | (add-to-list 'org-babel-tangle-lang-exts '("python" . "py")) | ||
| 41 | |||
| 42 | (defvar org-babel-default-header-args:python '()) | ||
| 43 | |||
| 44 | (defvar org-babel-python-command "python" | ||
| 45 | "Name of command for executing python code.") | ||
| 46 | |||
| 47 | (defvar org-babel-python-mode (if (featurep 'xemacs) 'python-mode 'python) | ||
| 48 | "Preferred python mode for use in running python interactively.") | ||
| 49 | |||
| 50 | (defun org-babel-expand-body:python (body params &optional processed-params) | ||
| 51 | "Expand BODY according to PARAMS, return the expanded body." | ||
| 52 | (concat | ||
| 53 | (mapconcat ;; define any variables | ||
| 54 | (lambda (pair) | ||
| 55 | (format "%s=%s" | ||
| 56 | (car pair) | ||
| 57 | (org-babel-python-var-to-python (cdr pair)))) | ||
| 58 | (nth 1 (or processed-params (org-babel-process-params params))) "\n") | ||
| 59 | "\n" (org-babel-trim body) "\n")) | ||
| 60 | |||
| 61 | (defun org-babel-execute:python (body params) | ||
| 62 | "Execute a block of Python code with Babel. | ||
| 63 | This function is called by `org-babel-execute-src-block'." | ||
| 64 | (let* ((processed-params (org-babel-process-params params)) | ||
| 65 | (session (org-babel-python-initiate-session (first processed-params))) | ||
| 66 | (result-params (nth 2 processed-params)) | ||
| 67 | (result-type (nth 3 processed-params)) | ||
| 68 | (full-body (org-babel-expand-body:python | ||
| 69 | body params processed-params)) | ||
| 70 | (result (org-babel-python-evaluate | ||
| 71 | session full-body result-type result-params))) | ||
| 72 | (or (cdr (assoc :file params)) | ||
| 73 | (org-babel-reassemble-table | ||
| 74 | result | ||
| 75 | (org-babel-pick-name (nth 4 processed-params) | ||
| 76 | (cdr (assoc :colnames params))) | ||
| 77 | (org-babel-pick-name (nth 5 processed-params) | ||
| 78 | (cdr (assoc :rownames params))))))) | ||
| 79 | |||
| 80 | (defun org-babel-prep-session:python (session params) | ||
| 81 | "Prepare SESSION according to the header arguments in PARAMS." | ||
| 82 | (let* ((session (org-babel-python-initiate-session session)) | ||
| 83 | (vars (org-babel-ref-variables params)) | ||
| 84 | (var-lines (mapcar ;; define any variables | ||
| 85 | (lambda (pair) | ||
| 86 | (format "%s=%s" | ||
| 87 | (car pair) | ||
| 88 | (org-babel-python-var-to-python (cdr pair)))) | ||
| 89 | vars))) | ||
| 90 | (org-babel-comint-in-buffer session | ||
| 91 | (mapc (lambda (var) | ||
| 92 | (end-of-line 1) (insert var) (comint-send-input) | ||
| 93 | (org-babel-comint-wait-for-output session)) var-lines)) | ||
| 94 | session)) | ||
| 95 | |||
| 96 | (defun org-babel-load-session:python (session body params) | ||
| 97 | "Load BODY into SESSION." | ||
| 98 | (save-window-excursion | ||
| 99 | (let ((buffer (org-babel-prep-session:python session params))) | ||
| 100 | (with-current-buffer buffer | ||
| 101 | (goto-char (process-mark (get-buffer-process (current-buffer)))) | ||
| 102 | (insert (org-babel-chomp body))) | ||
| 103 | buffer))) | ||
| 104 | |||
| 105 | ;; helper functions | ||
| 106 | |||
| 107 | (defun org-babel-python-var-to-python (var) | ||
| 108 | "Convert an elisp value to a python variable. | ||
| 109 | Convert an elisp value, VAR, into a string of python source code | ||
| 110 | specifying a variable of the same value." | ||
| 111 | (if (listp var) | ||
| 112 | (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]") | ||
| 113 | (if (equal var 'hline) | ||
| 114 | "None" | ||
| 115 | (format | ||
| 116 | (if (and (stringp var) (string-match "[\n\r]" var)) "\"\"%S\"\"" "%S") | ||
| 117 | var)))) | ||
| 118 | |||
| 119 | (defun org-babel-python-table-or-string (results) | ||
| 120 | "Convert RESULTS into an appropriate elisp value. | ||
| 121 | If the results look like a list or tuple, then convert them into an | ||
| 122 | Emacs-lisp table, otherwise return the results as a string." | ||
| 123 | ((lambda (res) | ||
| 124 | (if (listp res) | ||
| 125 | (mapcar (lambda (el) (if (equal el 'None) 'hline el)) res) | ||
| 126 | res)) | ||
| 127 | (org-babel-read | ||
| 128 | (if (or (string-match "^\\[.+\\]$" results) | ||
| 129 | (string-match "^(.+)$" results)) | ||
| 130 | (org-babel-read | ||
| 131 | (concat "'" | ||
| 132 | (replace-regexp-in-string | ||
| 133 | "\\[" "(" (replace-regexp-in-string | ||
| 134 | "\\]" ")" (replace-regexp-in-string | ||
| 135 | ", " " " (replace-regexp-in-string | ||
| 136 | "'" "\"" results t)))))) | ||
| 137 | results)))) | ||
| 138 | |||
| 139 | (defvar org-babel-python-buffers '((:default . nil))) | ||
| 140 | |||
| 141 | (defun org-babel-python-session-buffer (session) | ||
| 142 | "Return the buffer associated with SESSION." | ||
| 143 | (cdr (assoc session org-babel-python-buffers))) | ||
| 144 | |||
| 145 | (defun org-babel-python-initiate-session-by-key (&optional session) | ||
| 146 | "Initiate a python session. | ||
| 147 | If there is not a current inferior-process-buffer in SESSION | ||
| 148 | then create. Return the initialized session." | ||
| 149 | (require org-babel-python-mode) | ||
| 150 | (save-window-excursion | ||
| 151 | (let* ((session (if session (intern session) :default)) | ||
| 152 | (python-buffer (org-babel-python-session-buffer session))) | ||
| 153 | (cond | ||
| 154 | ((and (equal 'python org-babel-python-mode) | ||
| 155 | (fboundp 'run-python)) ; python.el | ||
| 156 | (run-python)) | ||
| 157 | ((and (equal 'python-mode org-babel-python-mode) | ||
| 158 | (fboundp 'py-shell)) ; python-mode.el | ||
| 159 | ;; `py-shell' creates a buffer whose name is the value of | ||
| 160 | ;; `py-which-bufname' with '*'s at the beginning and end | ||
| 161 | (let* ((bufname (if python-buffer | ||
| 162 | (replace-regexp-in-string ;; zap surrounding * | ||
| 163 | "^\\*\\([^*]+\\)\\*$" "\\1" python-buffer) | ||
| 164 | (concat "Python-" (symbol-name session)))) | ||
| 165 | (py-which-bufname bufname)) | ||
| 166 | (py-shell) | ||
| 167 | (setq python-buffer (concat "*" bufname "*")))) | ||
| 168 | (t | ||
| 169 | (error "No function available for running an inferior python."))) | ||
| 170 | (setq org-babel-python-buffers | ||
| 171 | (cons (cons session python-buffer) | ||
| 172 | (assq-delete-all session org-babel-python-buffers))) | ||
| 173 | session))) | ||
| 174 | |||
| 175 | (defun org-babel-python-initiate-session (&optional session params) | ||
| 176 | "Create a session named SESSION according to PARAMS." | ||
| 177 | (unless (string= session "none") | ||
| 178 | (org-babel-python-session-buffer | ||
| 179 | (org-babel-python-initiate-session-by-key session)))) | ||
| 180 | |||
| 181 | (defvar org-babel-python-eoe-indicator "'org_babel_python_eoe'" | ||
| 182 | "A string to indicate that evaluation has completed.") | ||
| 183 | (defvar org-babel-python-wrapper-method | ||
| 184 | " | ||
| 185 | def main(): | ||
| 186 | %s | ||
| 187 | |||
| 188 | open('%s', 'w').write( str(main()) )") | ||
| 189 | (defvar org-babel-python-pp-wrapper-method | ||
| 190 | " | ||
| 191 | import pprint | ||
| 192 | def main(): | ||
| 193 | %s | ||
| 194 | |||
| 195 | open('%s', 'w').write( pprint.pformat(main()) )") | ||
| 196 | |||
| 197 | (defun org-babel-python-evaluate | ||
| 198 | (buffer body &optional result-type result-params) | ||
| 199 | "Pass BODY to the Python process in BUFFER. | ||
| 200 | If RESULT-TYPE equals 'output then return a list of the outputs | ||
| 201 | of the statements in BODY, if RESULT-TYPE equals 'value then | ||
| 202 | return the value of the last statement in BODY, as elisp." | ||
| 203 | (if (not buffer) | ||
| 204 | ;; external process evaluation | ||
| 205 | (case result-type | ||
| 206 | (output (org-babel-eval org-babel-python-command body)) | ||
| 207 | (value (let ((tmp-file (make-temp-file "org-babel-python-results-"))) | ||
| 208 | (org-babel-eval org-babel-python-command | ||
| 209 | (format | ||
| 210 | (if (member "pp" result-params) | ||
| 211 | org-babel-python-pp-wrapper-method | ||
| 212 | org-babel-python-wrapper-method) | ||
| 213 | (mapconcat | ||
| 214 | (lambda (line) (format "\t%s" line)) | ||
| 215 | (split-string | ||
| 216 | (org-remove-indentation | ||
| 217 | (org-babel-trim body)) | ||
| 218 | "[\r\n]") "\n") | ||
| 219 | tmp-file)) | ||
| 220 | ((lambda (raw) | ||
| 221 | (if (or (member "code" result-params) | ||
| 222 | (member "pp" result-params)) | ||
| 223 | raw | ||
| 224 | (org-babel-python-table-or-string raw))) | ||
| 225 | (org-babel-eval-read-file tmp-file))))) | ||
| 226 | ;; comint session evaluation | ||
| 227 | (flet ((dump-last-value (tmp-file pp) | ||
| 228 | (mapc | ||
| 229 | (lambda (statement) (insert statement) (comint-send-input)) | ||
| 230 | (if pp | ||
| 231 | (list | ||
| 232 | "import pp" | ||
| 233 | (format "open('%s', 'w').write(pprint.pformat(_))" tmp-file)) | ||
| 234 | (list (format "open('%s', 'w').write(str(_))" tmp-file))))) | ||
| 235 | (input-body (body) | ||
| 236 | (mapc (lambda (statement) (insert statement) (comint-send-input)) | ||
| 237 | (split-string (org-babel-trim body) "[\r\n]+")) | ||
| 238 | (comint-send-input) (comint-send-input))) | ||
| 239 | (case result-type | ||
| 240 | (output | ||
| 241 | (mapconcat | ||
| 242 | #'org-babel-trim | ||
| 243 | (butlast | ||
| 244 | (org-babel-comint-with-output | ||
| 245 | (buffer org-babel-python-eoe-indicator t body) | ||
| 246 | (let ((comint-process-echoes nil)) | ||
| 247 | (input-body body) | ||
| 248 | (insert org-babel-python-eoe-indicator) | ||
| 249 | (comint-send-input))) 2) "\n")) | ||
| 250 | (value | ||
| 251 | ((lambda (results) | ||
| 252 | (if (or (member "code" result-params) (member "pp" result-params)) | ||
| 253 | results | ||
| 254 | (org-babel-python-table-or-string results))) | ||
| 255 | (let ((tmp-file (make-temp-file "org-babel-python-results-"))) | ||
| 256 | (org-babel-comint-with-output | ||
| 257 | (buffer org-babel-python-eoe-indicator t body) | ||
| 258 | (let ((comint-process-echoes nil)) | ||
| 259 | (input-body body) | ||
| 260 | (dump-last-value tmp-file (member "pp" result-params)) | ||
| 261 | (comint-send-input) (comint-send-input) | ||
| 262 | (insert org-babel-python-eoe-indicator) | ||
| 263 | (comint-send-input))) | ||
| 264 | (org-babel-eval-read-file tmp-file)))))))) | ||
| 265 | |||
| 266 | (defun org-babel-python-read-string (string) | ||
| 267 | "Strip 's from around python string" | ||
| 268 | (if (string-match "^'\\([^\000]+\\)'$" string) | ||
| 269 | (match-string 1 string) | ||
| 270 | string)) | ||
| 271 | |||
| 272 | (provide 'ob-python) | ||
| 273 | |||
| 274 | ;; arch-tag: f19b6c3d-dfcb-4a1a-9ce0-45ade1ebc212 | ||
| 275 | |||
| 276 | ;;; ob-python.el ends here | ||