22
22
from glob import glob as _glob
23
23
24
24
from .._Utils import _try_import
25
+
25
26
from .. import _Utils
26
27
27
28
import os as _os
35
36
# Set the default node directory.
36
37
_node_dir = _os .path .dirname (__file__ ) + "/_nodes"
37
38
38
- __all__ = ["list" , "help" , "run" , "setNodeDirectory" ]
39
+ __all__ = ["list" , "help" , "run" , "setNodeDirectory" , "getNodeDirectory" ]
39
40
40
41
41
42
def list ():
@@ -89,7 +90,7 @@ def help(name):
89
90
print (proc .stdout )
90
91
91
92
92
- def run (name , args = {}):
93
+ def run (name , args = {}, work_dir = None ):
93
94
"""
94
95
Run a node.
95
96
@@ -102,6 +103,10 @@ def run(name, args={}):
102
103
args : dict
103
104
A dictionary of arguments to be passed to the node.
104
105
106
+ work_dir : str, optional
107
+ The working directory in which to run the node. If not specified,
108
+ the current working directory is used.
109
+
105
110
Returns
106
111
-------
107
112
@@ -111,9 +116,18 @@ def run(name, args={}):
111
116
112
117
# Validate the input.
113
118
119
+ if not isinstance (name , str ):
120
+ raise TypeError ("'name' must be of type 'str'." )
121
+
114
122
if not isinstance (args , dict ):
115
123
raise TypeError ("'args' must be of type 'dict'." )
116
124
125
+ if work_dir is not None :
126
+ if not isinstance (work_dir , str ):
127
+ raise TypeError ("'work_dir' must be of type 'str'." )
128
+ else :
129
+ work_dir = _os .getcwd ()
130
+
117
131
# Apped the node directory name.
118
132
full_name = _node_dir + "/" + name
119
133
@@ -122,45 +136,50 @@ def run(name, args={}):
122
136
if not _os .path .isfile (full_name + ".py" ):
123
137
raise ValueError (
124
138
"Cannot find node: '%s'. " % name
139
+ + "in directory '%s'. " % _node_dir
125
140
+ "Run 'Node.list()' to see available nodes!"
126
141
)
127
142
else :
128
143
full_name += ".py"
129
144
130
- # Write a YAML configuration file for the BioSimSpace node.
131
- if len (args ) > 0 :
132
- with open ("input.yaml" , "w" ) as file :
133
- _yaml .dump (args , file , default_flow_style = False )
145
+ with _Utils .chdir (work_dir ):
146
+ # Write a YAML configuration file for the BioSimSpace node.
147
+ if len (args ) > 0 :
148
+ with open ("input.yaml" , "w" ) as file :
149
+ _yaml .dump (args , file , default_flow_style = False )
134
150
135
- # Create the command.
136
- command = "%s/python %s --config input.yaml" % (
137
- _SireBase .getBinDir (),
138
- full_name ,
139
- )
140
-
141
- # No arguments.
142
- else :
143
- command = "%s/python %s" % (_SireBase .getBinDir (), full_name )
151
+ # Create the command.
152
+ command = "%s/python %s --config input.yaml" % (
153
+ _SireBase .getBinDir (),
154
+ full_name ,
155
+ )
144
156
145
- # Run the node as a subprocess.
146
- proc = _subprocess .run (
147
- _Utils .command_split (command ), shell = False , text = True , stderr = _subprocess .PIPE
148
- )
157
+ # No arguments.
158
+ else :
159
+ command = "%s/python %s" % (_SireBase .getBinDir (), full_name )
160
+
161
+ # Run the node as a subprocess.
162
+ proc = _subprocess .run (
163
+ _Utils .command_split (command ),
164
+ shell = False ,
165
+ text = True ,
166
+ stderr = _subprocess .PIPE ,
167
+ )
149
168
150
- if proc .returncode == 0 :
151
- # Read the output YAML file into a dictionary.
152
- with open ("output.yaml" , "r" ) as file :
153
- output = _yaml .safe_load (file )
169
+ if proc .returncode == 0 :
170
+ # Read the output YAML file into a dictionary.
171
+ with open ("output.yaml" , "r" ) as file :
172
+ output = _yaml .safe_load (file )
154
173
155
- # Delete the redundant YAML files.
156
- _os .remove ("input.yaml" )
157
- _os .remove ("output.yaml" )
174
+ # Delete the redundant YAML files.
175
+ _os .remove ("input.yaml" )
176
+ _os .remove ("output.yaml" )
158
177
159
- return output
178
+ return output
160
179
161
- else :
162
- # Print the standard error, decoded as UTF-8.
163
- print (proc .stderr )
180
+ else :
181
+ # Print the standard error, decoded as UTF-8.
182
+ print (proc .stderr )
164
183
165
184
166
185
def setNodeDirectory (dir ):
@@ -179,3 +198,16 @@ def setNodeDirectory(dir):
179
198
180
199
global _node_dir
181
200
_node_dir = dir
201
+
202
+
203
+ def getNodeDirectory ():
204
+ """
205
+ Get the directory of the node library.
206
+
207
+ Returns
208
+ -------
209
+
210
+ dir : str
211
+ The path to the node library.
212
+ """
213
+ return _node_dir
0 commit comments