From 5866a4cd2120fb59793c03ee126b8d615bb32212 Mon Sep 17 00:00:00 2001 From: Carsten Behring Date: Wed, 30 Nov 2022 21:50:59 +0100 Subject: [PATCH 1/3] added 2 lifecycle methods before and after tnitialisation --- src/libpython_clj2/python.clj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libpython_clj2/python.clj b/src/libpython_clj2/python.clj index 8e82a54..b1178d1 100644 --- a/src/libpython_clj2/python.clj +++ b/src/libpython_clj2/python.clj @@ -42,7 +42,7 @@ user> (py/py. np linspace 2 3 :num 10) (set! *warn-on-reflection* true) - +(defn- no-op []) (defn initialize! "Initialize the python library. If library path is not provided, then the system @@ -97,6 +97,7 @@ user> (py/py. np linspace 2 3 :num 10) (let [python-edn-opts (-> (try (slurp "python.edn") (catch java.io.FileNotFoundException _ "{}")) clojure.edn/read-string) + _ ((requiring-resolve (get python-edn-opts :pre-initialize-fn 'libpython-clj2.python/no-op))) options (merge python-edn-opts options) info (py-info/detect-startup-info options) _ (log/infof "Startup info %s" info) @@ -132,6 +133,7 @@ user> (py/py. np linspace 2 3 :num 10) (io-redirect/redirect-io!)) (finally (py-ffi/unlock-gil gilstate)))) + ((requiring-resolve (get python-edn-opts :post-initialize-fn 'libpython-clj2.python/no-op))) :ok) :already-initialized)) From 96d9e98e438baa1efea94405811ef4cd8847b9c7 Mon Sep 17 00:00:00 2001 From: Carsten Behring Date: Wed, 30 Nov 2022 22:48:13 +0100 Subject: [PATCH 2/3] document the 2 hooks --- src/libpython_clj2/python.clj | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libpython_clj2/python.clj b/src/libpython_clj2/python.clj index b1178d1..09078c8 100644 --- a/src/libpython_clj2/python.clj +++ b/src/libpython_clj2/python.clj @@ -64,6 +64,24 @@ user> (py/py. np linspace 2 3 :num 10) {:python-executable \"env/bin/python\"} ``` + Additionaly the file can contain two keys which can can refer to custom hooks + to run code just before and just after python is initialised. + Typical use case for this is to setup / verify the python virtual enviornment + to be used. + + ``` + :pre-initialize-fn my-ns/my-venv-setup-fn! + :post-initialize-fn my-ns/my-venv-validate-fn! + + ``` + + A :pre-initialize-fn could for example shell out and setup a python + virtual enviornment. + + The :post-initialize-fn can use all functions from ns `libpython-clj2.python` + as libpython-clj is initialised alreday andc ould for example be used to validate + that later needed libraries can be loaded via calling `import-module`. + The file MUST be named `python.edn` and be in the root of the classpath. With a `python.edn` file in place, the `initialize!` function may be called with no arguments and the options will be read from the file. If arguments are From fbf8f343782f55712ae36361ae60daae2f0aa524 Mon Sep 17 00:00:00 2001 From: Carsten Behring Date: Thu, 1 Dec 2022 19:42:06 +0100 Subject: [PATCH 3/3] more idiomatic --- src/libpython_clj2/python.clj | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/libpython_clj2/python.clj b/src/libpython_clj2/python.clj index 09078c8..16f1f32 100644 --- a/src/libpython_clj2/python.clj +++ b/src/libpython_clj2/python.clj @@ -42,8 +42,6 @@ user> (py/py. np linspace 2 3 :num 10) (set! *warn-on-reflection* true) -(defn- no-op []) - (defn initialize! "Initialize the python library. If library path is not provided, then the system attempts to execute a simple python program and have python return system info. @@ -115,7 +113,7 @@ user> (py/py. np linspace 2 3 :num 10) (let [python-edn-opts (-> (try (slurp "python.edn") (catch java.io.FileNotFoundException _ "{}")) clojure.edn/read-string) - _ ((requiring-resolve (get python-edn-opts :pre-initialize-fn 'libpython-clj2.python/no-op))) + _ (some-> python-edn-opts :pre-initialize-fn requiring-resolve (apply [])) options (merge python-edn-opts options) info (py-info/detect-startup-info options) _ (log/infof "Startup info %s" info) @@ -151,11 +149,10 @@ user> (py/py. np linspace 2 3 :num 10) (io-redirect/redirect-io!)) (finally (py-ffi/unlock-gil gilstate)))) - ((requiring-resolve (get python-edn-opts :post-initialize-fn 'libpython-clj2.python/no-op))) + (some-> python-edn-opts :post-initialize-fn requiring-resolve (apply [])) :ok) :already-initialized)) - (defmacro stack-resource-context "Create a stack-based resource context. All python objects allocated within this context will be released at the termination of this context.