@@ -42,8 +42,6 @@ user> (py/py. np linspace 2 3 :num 10)
42
42
43
43
(set! *warn-on-reflection* true )
44
44
45
-
46
-
47
45
(defn initialize!
48
46
" Initialize the python library. If library path is not provided, then the system
49
47
attempts to execute a simple python program and have python return system info.
@@ -64,6 +62,24 @@ user> (py/py. np linspace 2 3 :num 10)
64
62
{:python-executable \" env/bin/python\" }
65
63
```
66
64
65
+ Additionaly the file can contain two keys which can can refer to custom hooks
66
+ to run code just before and just after python is initialised.
67
+ Typical use case for this is to setup / verify the python virtual enviornment
68
+ to be used.
69
+
70
+ ```
71
+ :pre-initialize-fn my-ns/my-venv-setup-fn!
72
+ :post-initialize-fn my-ns/my-venv-validate-fn!
73
+
74
+ ```
75
+
76
+ A :pre-initialize-fn could for example shell out and setup a python
77
+ virtual enviornment.
78
+
79
+ The :post-initialize-fn can use all functions from ns `libpython-clj2.python`
80
+ as libpython-clj is initialised alreday andc ould for example be used to validate
81
+ that later needed libraries can be loaded via calling `import-module`.
82
+
67
83
The file MUST be named `python.edn` and be in the root of the classpath.
68
84
With a `python.edn` file in place, the `initialize!` function may be called
69
85
with no arguments and the options will be read from the file. If arguments are
@@ -97,6 +113,7 @@ user> (py/py. np linspace 2 3 :num 10)
97
113
(let [python-edn-opts (-> (try (slurp " python.edn" )
98
114
(catch java.io.FileNotFoundException _ " {}" ))
99
115
clojure.edn/read-string)
116
+ _ (some-> python-edn-opts :pre-initialize-fn requiring-resolve (apply []))
100
117
options (merge python-edn-opts options)
101
118
info (py-info/detect-startup-info options)
102
119
_ (log/infof " Startup info %s" info)
@@ -132,10 +149,10 @@ user> (py/py. np linspace 2 3 :num 10)
132
149
(io-redirect/redirect-io! ))
133
150
(finally
134
151
(py-ffi/unlock-gil gilstate))))
152
+ (some-> python-edn-opts :post-initialize-fn requiring-resolve (apply []))
135
153
:ok )
136
154
:already-initialized ))
137
155
138
-
139
156
(defmacro stack-resource-context
140
157
" Create a stack-based resource context. All python objects allocated within this
141
158
context will be released at the termination of this context.
0 commit comments