diff options
| author | Bastien Guerry | 2011-07-28 17:13:49 +0200 |
|---|---|---|
| committer | Bastien Guerry | 2011-07-28 17:13:49 +0200 |
| commit | 3ab2c837b302b01fff610f7b83050ab7e703477c (patch) | |
| tree | efa67ed523bbda4d41488ae6b9ad2782941ddcf2 /lisp/org/ob-python.el | |
| parent | 44a8054f971837447e80d618b6e0c2a77778a2ee (diff) | |
| download | emacs-3ab2c837b302b01fff610f7b83050ab7e703477c.tar.gz emacs-3ab2c837b302b01fff610f7b83050ab7e703477c.zip | |
Merge changes from Org 7.4 to current Org 7.7.
Diffstat (limited to 'lisp/org/ob-python.el')
| -rw-r--r-- | lisp/org/ob-python.el | 134 |
1 files changed, 76 insertions, 58 deletions
diff --git a/lisp/org/ob-python.el b/lisp/org/ob-python.el index b53513a212c..0fe0b4fefd8 100644 --- a/lisp/org/ob-python.el +++ b/lisp/org/ob-python.el | |||
| @@ -1,11 +1,11 @@ | |||
| 1 | ;;; ob-python.el --- org-babel functions for python evaluation | 1 | ;;; ob-python.el --- org-babel functions for python evaluation |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 2009-2011 Free Software Foundation | 3 | ;; Copyright (C) 2009, 2010 Free Software Foundation |
| 4 | 4 | ||
| 5 | ;; Author: Eric Schulte, Dan Davison | 5 | ;; Author: Eric Schulte, Dan Davison |
| 6 | ;; Keywords: literate programming, reproducible research | 6 | ;; Keywords: literate programming, reproducible research |
| 7 | ;; Homepage: http://orgmode.org | 7 | ;; Homepage: http://orgmode.org |
| 8 | ;; Version: 7.4 | 8 | ;; Version: 7.7 |
| 9 | 9 | ||
| 10 | ;; This file is part of GNU Emacs. | 10 | ;; This file is part of GNU Emacs. |
| 11 | 11 | ||
| @@ -35,8 +35,10 @@ | |||
| 35 | 35 | ||
| 36 | (declare-function org-remove-indentation "org" ) | 36 | (declare-function org-remove-indentation "org" ) |
| 37 | (declare-function py-shell "ext:python-mode" (&optional argprompt)) | 37 | (declare-function py-shell "ext:python-mode" (&optional argprompt)) |
| 38 | (declare-function py-toggle-shells "ext:python-mode" (arg)) | ||
| 38 | (declare-function run-python "ext:python" (&optional cmd noshow new)) | 39 | (declare-function run-python "ext:python" (&optional cmd noshow new)) |
| 39 | 40 | ||
| 41 | (defvar org-babel-tangle-lang-exts) | ||
| 40 | (add-to-list 'org-babel-tangle-lang-exts '("python" . "py")) | 42 | (add-to-list 'org-babel-tangle-lang-exts '("python" . "py")) |
| 41 | 43 | ||
| 42 | (defvar org-babel-default-header-args:python '()) | 44 | (defvar org-babel-default-header-args:python '()) |
| @@ -45,7 +47,8 @@ | |||
| 45 | "Name of command for executing python code.") | 47 | "Name of command for executing python code.") |
| 46 | 48 | ||
| 47 | (defvar org-babel-python-mode (if (featurep 'xemacs) 'python-mode 'python) | 49 | (defvar org-babel-python-mode (if (featurep 'xemacs) 'python-mode 'python) |
| 48 | "Preferred python mode for use in running python interactively.") | 50 | "Preferred python mode for use in running python interactively. |
| 51 | This will typically be either 'python or 'python-mode.") | ||
| 49 | 52 | ||
| 50 | (defvar org-src-preserve-indentation) | 53 | (defvar org-src-preserve-indentation) |
| 51 | 54 | ||
| @@ -65,13 +68,12 @@ This function is called by `org-babel-execute-src-block'." | |||
| 65 | params (org-babel-variable-assignments:python params))) | 68 | params (org-babel-variable-assignments:python params))) |
| 66 | (result (org-babel-python-evaluate | 69 | (result (org-babel-python-evaluate |
| 67 | session full-body result-type result-params preamble))) | 70 | session full-body result-type result-params preamble))) |
| 68 | (or (cdr (assoc :file params)) | 71 | (org-babel-reassemble-table |
| 69 | (org-babel-reassemble-table | 72 | result |
| 70 | result | 73 | (org-babel-pick-name (cdr (assoc :colname-names params)) |
| 71 | (org-babel-pick-name (cdr (assoc :colname-names params)) | 74 | (cdr (assoc :colnames params))) |
| 72 | (cdr (assoc :colnames params))) | 75 | (org-babel-pick-name (cdr (assoc :rowname-names params)) |
| 73 | (org-babel-pick-name (cdr (assoc :rowname-names params)) | 76 | (cdr (assoc :rownames params)))))) |
| 74 | (cdr (assoc :rownames params))))))) | ||
| 75 | 77 | ||
| 76 | (defun org-babel-prep-session:python (session params) | 78 | (defun org-babel-prep-session:python (session params) |
| 77 | "Prepare SESSION according to the header arguments in PARAMS. | 79 | "Prepare SESSION according to the header arguments in PARAMS. |
| @@ -129,6 +131,7 @@ Emacs-lisp table, otherwise return the results as a string." | |||
| 129 | "Return the buffer associated with SESSION." | 131 | "Return the buffer associated with SESSION." |
| 130 | (cdr (assoc session org-babel-python-buffers))) | 132 | (cdr (assoc session org-babel-python-buffers))) |
| 131 | 133 | ||
| 134 | (defvar py-default-interpreter) | ||
| 132 | (defun org-babel-python-initiate-session-by-key (&optional session) | 135 | (defun org-babel-python-initiate-session-by-key (&optional session) |
| 133 | "Initiate a python session. | 136 | "Initiate a python session. |
| 134 | If there is not a current inferior-process-buffer in SESSION | 137 | If there is not a current inferior-process-buffer in SESSION |
| @@ -143,9 +146,13 @@ then create. Return the initialized session." | |||
| 143 | (run-python)) | 146 | (run-python)) |
| 144 | ((and (eq 'python-mode org-babel-python-mode) | 147 | ((and (eq 'python-mode org-babel-python-mode) |
| 145 | (fboundp 'py-shell)) ; python-mode.el | 148 | (fboundp 'py-shell)) ; python-mode.el |
| 149 | ;; Make sure that py-which-bufname is initialized, as otherwise | ||
| 150 | ;; it will be overwritten the first time a Python buffer is | ||
| 151 | ;; created. | ||
| 152 | (py-toggle-shells py-default-interpreter) | ||
| 146 | ;; `py-shell' creates a buffer whose name is the value of | 153 | ;; `py-shell' creates a buffer whose name is the value of |
| 147 | ;; `py-which-bufname' with '*'s at the beginning and end | 154 | ;; `py-which-bufname' with '*'s at the beginning and end |
| 148 | (let* ((bufname (if python-buffer | 155 | (let* ((bufname (if (and python-buffer (buffer-live-p python-buffer)) |
| 149 | (replace-regexp-in-string ;; zap surrounding * | 156 | (replace-regexp-in-string ;; zap surrounding * |
| 150 | "^\\*\\([^*]+\\)\\*$" "\\1" python-buffer) | 157 | "^\\*\\([^*]+\\)\\*$" "\\1" python-buffer) |
| 151 | (concat "Python-" (symbol-name session)))) | 158 | (concat "Python-" (symbol-name session)))) |
| @@ -196,29 +203,33 @@ open('%s', 'w').write( pprint.pformat(main()) )") | |||
| 196 | If RESULT-TYPE equals 'output then return standard output as a | 203 | If RESULT-TYPE equals 'output then return standard output as a |
| 197 | string. If RESULT-TYPE equals 'value then return the value of the | 204 | string. If RESULT-TYPE equals 'value then return the value of the |
| 198 | last statement in BODY, as elisp." | 205 | last statement in BODY, as elisp." |
| 199 | (case result-type | 206 | ((lambda (raw) |
| 200 | (output (org-babel-eval org-babel-python-command | 207 | (if (or (member "code" result-params) |
| 201 | (concat (if preamble (concat preamble "\n") "") body))) | 208 | (member "pp" result-params) |
| 202 | (value (let ((tmp-file (org-babel-temp-file "python-"))) | 209 | (and (member "output" result-params) |
| 203 | (org-babel-eval org-babel-python-command | 210 | (not (member "table" result-params)))) |
| 204 | (concat | 211 | raw |
| 205 | (if preamble (concat preamble "\n") "") | 212 | (org-babel-python-table-or-string (org-babel-trim raw)))) |
| 206 | (format | 213 | (case result-type |
| 207 | (if (member "pp" result-params) | 214 | (output (org-babel-eval org-babel-python-command |
| 208 | org-babel-python-pp-wrapper-method | 215 | (concat (if preamble (concat preamble "\n") "") |
| 209 | org-babel-python-wrapper-method) | 216 | body))) |
| 210 | (mapconcat | 217 | (value (let ((tmp-file (org-babel-temp-file "python-"))) |
| 211 | (lambda (line) (format "\t%s" line)) | 218 | (org-babel-eval |
| 212 | (split-string | 219 | org-babel-python-command |
| 213 | (org-remove-indentation | 220 | (concat |
| 214 | (org-babel-trim body)) | 221 | (if preamble (concat preamble "\n") "") |
| 215 | "[\r\n]") "\n") | 222 | (format |
| 216 | (org-babel-process-file-name tmp-file 'noquote)))) | 223 | (if (member "pp" result-params) |
| 217 | ((lambda (raw) | 224 | org-babel-python-pp-wrapper-method |
| 218 | (if (or (member "code" result-params) | 225 | org-babel-python-wrapper-method) |
| 219 | (member "pp" result-params)) | 226 | (mapconcat |
| 220 | raw | 227 | (lambda (line) (format "\t%s" line)) |
| 221 | (org-babel-python-table-or-string raw))) | 228 | (split-string |
| 229 | (org-remove-indentation | ||
| 230 | (org-babel-trim body)) | ||
| 231 | "[\r\n]") "\n") | ||
| 232 | (org-babel-process-file-name tmp-file 'noquote)))) | ||
| 222 | (org-babel-eval-read-file tmp-file)))))) | 233 | (org-babel-eval-read-file tmp-file)))))) |
| 223 | 234 | ||
| 224 | (defun org-babel-python-evaluate-session | 235 | (defun org-babel-python-evaluate-session |
| @@ -227,10 +238,11 @@ last statement in BODY, as elisp." | |||
| 227 | If RESULT-TYPE equals 'output then return standard output as a | 238 | If RESULT-TYPE equals 'output then return standard output as a |
| 228 | string. If RESULT-TYPE equals 'value then return the value of the | 239 | string. If RESULT-TYPE equals 'value then return the value of the |
| 229 | last statement in BODY, as elisp." | 240 | last statement in BODY, as elisp." |
| 230 | (flet ((dump-last-value | 241 | (flet ((send-wait () (comint-send-input nil t) (sleep-for 0 5)) |
| 242 | (dump-last-value | ||
| 231 | (tmp-file pp) | 243 | (tmp-file pp) |
| 232 | (mapc | 244 | (mapc |
| 233 | (lambda (statement) (insert statement) (comint-send-input)) | 245 | (lambda (statement) (insert statement) (send-wait)) |
| 234 | (if pp | 246 | (if pp |
| 235 | (list | 247 | (list |
| 236 | "import pprint" | 248 | "import pprint" |
| @@ -239,34 +251,39 @@ last statement in BODY, as elisp." | |||
| 239 | (list (format "open('%s', 'w').write(str(_))" | 251 | (list (format "open('%s', 'w').write(str(_))" |
| 240 | (org-babel-process-file-name tmp-file 'noquote)))))) | 252 | (org-babel-process-file-name tmp-file 'noquote)))))) |
| 241 | (input-body (body) | 253 | (input-body (body) |
| 242 | (mapc (lambda (statement) (insert statement) (comint-send-input)) | 254 | (mapc (lambda (line) (insert line) (send-wait)) |
| 243 | (split-string (org-babel-trim body) "[\r\n]+")) | 255 | (split-string body "[\r\n]")) |
| 244 | (comint-send-input) (comint-send-input))) | 256 | (send-wait))) |
| 245 | (case result-type | 257 | ((lambda (results) |
| 246 | (output | 258 | (unless (string= (substring org-babel-python-eoe-indicator 1 -1) results) |
| 247 | (mapconcat | 259 | (if (or (member "code" result-params) |
| 248 | #'org-babel-trim | 260 | (member "pp" result-params) |
| 249 | (butlast | 261 | (and (member "output" result-params) |
| 250 | (org-babel-comint-with-output | 262 | (not (member "table" result-params)))) |
| 251 | (session org-babel-python-eoe-indicator t body) | 263 | results |
| 252 | (let ((comint-process-echoes nil)) | 264 | (org-babel-python-table-or-string results)))) |
| 253 | (input-body body) | 265 | (case result-type |
| 254 | (insert org-babel-python-eoe-indicator) | 266 | (output |
| 255 | (comint-send-input))) 2) "\n")) | 267 | (mapconcat |
| 256 | (value | 268 | #'org-babel-trim |
| 257 | ((lambda (results) | 269 | (butlast |
| 258 | (if (or (member "code" result-params) (member "pp" result-params)) | ||
| 259 | results | ||
| 260 | (org-babel-python-table-or-string results))) | ||
| 261 | (let ((tmp-file (org-babel-temp-file "python-"))) | ||
| 262 | (org-babel-comint-with-output | 270 | (org-babel-comint-with-output |
| 263 | (session org-babel-python-eoe-indicator t body) | 271 | (session org-babel-python-eoe-indicator t body) |
| 272 | (input-body body) | ||
| 273 | (send-wait) (send-wait) | ||
| 274 | (insert org-babel-python-eoe-indicator) | ||
| 275 | (send-wait)) | ||
| 276 | 2) "\n")) | ||
| 277 | (value | ||
| 278 | (let ((tmp-file (org-babel-temp-file "python-"))) | ||
| 279 | (org-babel-comint-with-output | ||
| 280 | (session org-babel-python-eoe-indicator nil body) | ||
| 264 | (let ((comint-process-echoes nil)) | 281 | (let ((comint-process-echoes nil)) |
| 265 | (input-body body) | 282 | (input-body body) |
| 266 | (dump-last-value tmp-file (member "pp" result-params)) | 283 | (dump-last-value tmp-file (member "pp" result-params)) |
| 267 | (comint-send-input) (comint-send-input) | 284 | (send-wait) (send-wait) |
| 268 | (insert org-babel-python-eoe-indicator) | 285 | (insert org-babel-python-eoe-indicator) |
| 269 | (comint-send-input))) | 286 | (send-wait))) |
| 270 | (org-babel-eval-read-file tmp-file))))))) | 287 | (org-babel-eval-read-file tmp-file))))))) |
| 271 | 288 | ||
| 272 | (defun org-babel-python-read-string (string) | 289 | (defun org-babel-python-read-string (string) |
| @@ -277,5 +294,6 @@ last statement in BODY, as elisp." | |||
| 277 | 294 | ||
| 278 | (provide 'ob-python) | 295 | (provide 'ob-python) |
| 279 | 296 | ||
| 297 | ;; arch-tag: f19b6c3d-dfcb-4a1a-9ce0-45ade1ebc212 | ||
| 280 | 298 | ||
| 281 | ;;; ob-python.el ends here | 299 | ;;; ob-python.el ends here |