Skip to content

Commit f863ce3

Browse files
Merge pull request #450 from NeuroML/feat-provide-jnml-wrapper
Feat: provide `jnml` command that will use the bundled jar
2 parents c4c5e2c + 3b0ba50 commit f863ce3

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

pyneuroml/runners.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ def run_jneuroml(
705705
report_jnml_output: bool = True,
706706
exit_on_fail: bool = False,
707707
return_string: bool = False,
708+
output_prefix: str = " jNeuroML >> ",
708709
) -> typing.Union[typing.Tuple[bool, str], bool]:
709710
"""Run jnml with provided arguments.
710711
@@ -726,6 +727,8 @@ def run_jneuroml(
726727
:type exit_on_fail: bool
727728
:param return_string: toggle whether the output string should be returned
728729
:type return_string: bool
730+
:param output_prefix: string to prefix the returned jNeuroML output with
731+
:type output_prefix: str
729732
730733
:returns: either a bool, or a Tuple (bool, str) depending on the value of
731734
return_string: True of jnml ran successfully, False if not; along with the
@@ -756,7 +759,7 @@ def run_jneuroml(
756759
try:
757760
command = f'java -Xmx{max_memory} {pre_jar} -jar "{jar_path}" {pre_args} {target_file} {post_args}'
758761
retcode, output = execute_command_in_dir(
759-
command, exec_in_dir, verbose=verbose, prefix=" jNeuroML >> "
762+
command, exec_in_dir, verbose=verbose, prefix=output_prefix
760763
)
761764

762765
if retcode != 0:
@@ -919,7 +922,7 @@ def execute_command_in_dir_with_realtime_output(
919922
raise e
920923

921924
if not p.returncode == 0:
922-
logger.critical(
925+
logger.error(
923926
"*** Problem running command (return code: %s): [%s]"
924927
% (p.returncode, command)
925928
)
@@ -990,14 +993,14 @@ def execute_command_in_dir(
990993
return return_string.decode("utf-8")
991994

992995
except subprocess.CalledProcessError as e:
993-
logger.critical("*** Problem running last command: %s" % e)
996+
logger.error("*** Problem running last command: %s" % e)
994997

995998
print("####################################################################")
996999
print("%s%s" % (prefix, e.output.decode().replace("\n", "\n" + prefix)))
9971000
print("####################################################################")
9981001
return (e.returncode, e.output.decode())
9991002
except Exception as e:
1000-
logger.critical("*** Unknown problem running command: %s" % e)
1003+
logger.error("*** Unknown problem running command: %s" % e)
10011004
return (-1, str(e))
10021005

10031006

pyneuroml/utils/jnmlwrapper.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Simple wrapper around jNeuroML to allow users to use jnml using the version
4+
bundled in PyNeuroML
5+
6+
File: pyneuroml/utils/jnmlwrapper.py
7+
8+
Copyright 2025 NeuroML contributors
9+
Author: Ankur Sinha <sanjay DOT ankur AT gmail DOT com>
10+
"""
11+
12+
import logging
13+
import os
14+
import sys
15+
16+
from ..runners import run_jneuroml
17+
18+
19+
def __jnmlwrapper():
20+
"""Wrapper around jNeuroML jar shipped with PyNeuroML.
21+
22+
The following environment variables can be set:
23+
24+
- JNML_MAX_MEMORY_LOCAL: set the maximum memory available to the Java
25+
Virtual Machine (default: 400M)
26+
27+
"""
28+
max_memory = os.getenv("JNML_MAX_MEMORY_LOCAL", "400M")
29+
logging.getLogger("pyneuroml.runners").setLevel(logging.CRITICAL)
30+
31+
retstat, output = run_jneuroml(
32+
pre_args=" ".join(sys.argv[1:]),
33+
target_file="",
34+
post_args="",
35+
max_memory=max_memory,
36+
report_jnml_output=False,
37+
output_prefix="",
38+
return_string=True,
39+
exit_on_fail=False,
40+
)
41+
42+
# if command ran successfully, print the output
43+
# if it didn't, `run_jneuroml` will throw a critical error
44+
if retstat is True:
45+
print(output)
46+
sys.exit(0)
47+
else:
48+
sys.exit(1)

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ console_scripts =
5959
pynml-sonata = neuromllite.SonataReader:main
6060
pynml-xpp = pyneuroml.xppaut:main
6161
pynml-swc2nml = pyneuroml.swc.ExportNML:main
62+
jnml = pyneuroml.utils.jnmlwrapper:__jnmlwrapper
6263

6364
[options.package_data]
6465
* =

test-ghactions.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ echo "## Testing all CLI tools"
2828
full_path=$(command -v pynml)
2929
bin_location=$(dirname $full_path)
3030

31-
for f in ${bin_location}/pynml*
31+
for f in ${bin_location}/pynml* ${bin_location}/jnml
3232
do
3333
current_exec=$(basename $f)
3434
echo "-> Testing $current_exec runs"

0 commit comments

Comments
 (0)