from.._Utilsimport_try_importimportosas_os_yaml=_try_import("yaml")# Set the default node directory._node_dir=_os.path.dirname(__file__)+"/_nodes"__all__=["list","help","run","setNodeDirectory","getNodeDirectory"]
[docs]deflist():"""Return a list of the available nodes."""fromglobimportglobas_glob# Glob all Python scripts in the _nodes directory.nodes=_glob("%s/*.py"%_node_dir)# Strip the extension.nodes=[_os.path.basename(x).split(".py")[0]forxinnodes]returnnodes
[docs]defhelp(name):""" Print the help message for the named node. Parameters ---------- name : str The name of the node. """from..import_Utilsfromsire.legacyimportBaseas_SireBaseimportsubprocessas_subprocessifnotisinstance(name,str):raiseTypeError("'name' must be of type 'str'.")# Apped the node directory name.full_name=_node_dir+"/"+name# Make sure the node exists.ifnot_os.path.isfile(full_name):ifnot_os.path.isfile(full_name+".py"):raiseValueError("Cannot find node: '%s'. "%name+"Run 'Node.list()' to see available nodes!")else:full_name+=".py"# Create the command.command="%s/python %s --help"%(_SireBase.getBinDir(),full_name)# Run the node as a subprocess.proc=_subprocess.run(_Utils.command_split(command),shell=False,text=True,stdout=_subprocess.PIPE)# Print the standard output, decoded as UTF-8.print(proc.stdout)
[docs]defrun(name,args={},work_dir=None):""" Run a node. Parameters ---------- name : str The name of the node. args : dict A dictionary of arguments to be passed to the node. work_dir : str, optional The working directory in which to run the node. If not specified, the current working directory is used. Note that inputs should use absolute paths if this is set. Returns ------- output : dict A dictionary containing the output of the node. """importsubprocessas_subprocessfromsire.legacyimportBaseas_SireBasefrom..import_Utils# Validate the input.ifnotisinstance(name,str):raiseTypeError("'name' must be of type 'str'.")ifnotisinstance(args,dict):raiseTypeError("'args' must be of type 'dict'.")ifwork_dirisnotNone:ifnotisinstance(work_dir,str):raiseTypeError("'work_dir' must be of type 'str'.")else:work_dir=_os.getcwd()# Apped the node directory name.full_name=_node_dir+"/"+name# Make sure the node exists.ifnot_os.path.isfile(full_name):ifnot_os.path.isfile(full_name+".py"):raiseValueError("Cannot find node: '%s'. "%name+"in directory '%s'. "%_node_dir+"Run 'Node.list()' to see available nodes!")else:full_name+=".py"with_Utils.cd(work_dir):# Write a YAML configuration file for the BioSimSpace node.iflen(args)>0:withopen("input.yaml","w")asfile:_yaml.dump(args,file,default_flow_style=False)# Create the command.command="%s/python %s --config input.yaml"%(_SireBase.getBinDir(),full_name,)# No arguments.else:command="%s/python %s"%(_SireBase.getBinDir(),full_name)# Run the node as a subprocess.proc=_subprocess.run(_Utils.command_split(command),shell=False,text=True,stderr=_subprocess.PIPE,)ifproc.returncode==0:# Read the output YAML file into a dictionary.withopen("output.yaml","r")asfile:output=_yaml.safe_load(file)# Delete the redundant YAML files._os.remove("input.yaml")_os.remove("output.yaml")returnoutputelse:# Print the standard error, decoded as UTF-8.print(proc.stderr)
[docs]defsetNodeDirectory(dir):""" Set the directory of the node library. Parameters ---------- dir : str The path to the node library. """ifnot_os.path.isdir(dir):raiseIOError("Node directory '%s' doesn't exist!"%dir)# Use the absolute path.dir=_os.path.abspath(dir)global_node_dir_node_dir=dir
defgetNodeDirectory():""" Get the directory of the node library. Returns ------- dir : str The path to the node library. """return_node_dir