|
1 | 1 | (ns clojure-python.core |
2 | 2 | (:use (clojure.core.*)) |
3 | 3 | (:import (org.python.util PythonInterpreter) |
4 | | - (org.python.core.*))) |
5 | | - |
6 | | -;; instantiate a python interpreter in the python namespace |
7 | | -;(def *interp* (new org.python.util.PythonInterpreter)) |
| 4 | + (org.python.core.*) |
| 5 | + (java.lang System String) |
| 6 | + (java.util Properties ))) |
8 | 7 |
|
9 | 8 | (defn init |
10 | 9 | "this may later take keywords and initialize other things |
11 | 10 | for now it is just used to specify python library paths" |
12 | | - ([interp libpath] |
13 | | - (doto interp |
| 11 | + ([py libpath] |
| 12 | + (doto py |
14 | 13 | (.exec "import sys") |
15 | 14 | (.exec (str "sys.path.append('" libpath "')")))) |
16 | | - ([interp libpath & more] |
17 | | - (init interp libpath) |
| 15 | + ([py libpath & more] |
| 16 | + (init py libpath) |
18 | 17 | (apply init more))) |
19 | 18 |
|
20 | 19 | (defmacro py-import |
|
90 | 89 | (if (seq funcs) |
91 | 90 | (dorun (map #(import-fn module %) funcs))))) |
92 | 91 |
|
93 | | -(defn python [name & {:keys |
94 | | - [pre-properties |
95 | | - post-properties |
96 | | - argv |
97 | | - sys-path |
98 | | - modules]}] |
99 | | - (do |
100 | | - (PythonInterpreter/initialize pre-properties post-properties argv) |
101 | | - (let [interp (PythonInterpreter.)] |
| 92 | +(defn properties [h] |
| 93 | + {:pre (map? h)} |
| 94 | + (let [p (Properties.)] |
| 95 | + (dorun (map #(.setProperty p (str (first %)) (str (second %))) h)) |
| 96 | + p)) |
| 97 | + |
| 98 | +(defn python |
| 99 | + [name & {:keys [post-properties argv sys-path modules] |
| 100 | + :or {post-properties {} argv []}}] |
| 101 | + (let [pre (System/getProperties) |
| 102 | + post (properties post-properties) |
| 103 | + args (into-array String (map str argv))] |
| 104 | + ;; Initialization has to occur before any interpreter instances are |
| 105 | + ;; created. |
| 106 | + (PythonInterpreter/initialize pre post args) |
| 107 | + (let [py (PythonInterpreter.)] |
102 | 108 | (if (seq sys-path) |
103 | | - (init interp sys-path)) |
| 109 | + (init py sys-path)) |
104 | 110 | (loop [mods modules] |
105 | | - (let [[m args] (first mods) k (:funcs args) o (:objs args)] |
106 | | - (python-mod interp m :funcs k :objs o) |
107 | | - (recur (rest mods)))) |
108 | | - interp))) |
| 111 | + (println (count mods)) |
| 112 | + (if (seq mods) |
| 113 | + (let [[m args] (first mods) k (:funcs args) o (:objs args)] |
| 114 | + (python-mod py m :funcs k :objs o) |
| 115 | + (recur (rest mods))))) |
| 116 | + py))) |
109 | 117 |
|
110 | 118 | (defmacro __ |
111 | 119 | "access attribute of class or attribute of attribute of (and so on) class" |
|
0 commit comments