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