Skip to content

Commit dcdcc77

Browse files
authored
added load-file function (#176)
* added load-file function * added docu for loading clojure file in embedded * added finally * proposal of single clojure-init function * fixed fn name and args * added load_clojure_file
1 parent bd586da commit dcdcc77

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

cljbridge.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def py_dict_to_keyword_map(py_dict):
138138
return hash_map
139139

140140

141-
def init_clojure_repl(**kw_args):
141+
def init_jvm(**kw_args):
142142
"""Initialize clojure with extra arguments specifically for embedding a cider-nrepl
143143
server. Then start an nrepl server. The port will both be printed to stdout and
144144
output to a .nrepl_server file. This function does not return as it leaves the GIL
@@ -157,10 +157,36 @@ def init_clojure_repl(**kw_args):
157157
bind=\"0.0.0.0\""""
158158
javabridge.start_vm(run_headless=True, class_path=repl_classpath(**kw_args))
159159
init_clojure_runtime()
160+
if "load_user_clj" in kw_args:
161+
resolve_call_fn("clojure.core/load-file",
162+
"user.clj")
160163
init_libpy_embedded()
161-
resolve_call_fn("libpython-clj2.embedded/start-repl!",
162-
py_dict_to_keyword_map(kw_args))
163164

165+
if "start_repl" in kw_args:
166+
resolve_call_fn("libpython-clj2.embedded/start-repl!",
167+
py_dict_to_keyword_map(kw_args))
168+
169+
if "kill_vm_after" in kw_args:
170+
javabridge.kill_vm()
171+
172+
def load_clojure_file(**kw_args):
173+
"""Initializes clojure and loads and runs a Clojure file. This function load the specified clojure file
174+
and kills then the jvm and returns.
175+
176+
Keyword arguments are:
177+
178+
* `classpath_args` - List of additional arguments that be passed to the clojure
179+
process when building the classpath.
180+
* `clj_file` Clojure file to be loaded with Clojure `load-file` fn
181+
"""
182+
javabridge.start_vm(run_headless=True, class_path=repl_classpath(**kw_args))
183+
init_clojure_runtime()
184+
init_libpy_embedded()
185+
try:
186+
resolve_call_fn("clojure.core/load-file",
187+
kw_args["clj_file"])
188+
finally:
189+
javabridge.kill_vm()
164190

165191
class GenericJavaObj:
166192
__str__ = javabridge.make_method("toString", "()Ljava/lang/String;")

topics/embedded.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ in the main module -
154154
Printing from Clojure: [1 2 3 4 Embedded Clojure FTW!!]
155155
>>>
156156
```
157+
## Loading and running a Clojure file in embedded mode
158+
159+
We can runs as well a .clj file in embedded mode.
160+
The following does this without an interactive pytho shell, it just runs the provided clj file with
161+
`clojure.core/load-file`
162+
163+
```bash
164+
python3 -c 'import cljbridge;cljbridge.load_clojure_file(clj_file="my-file.clj")'
165+
```
157166

158167

159168
## Are You Not Entertained???

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy