def parse_args_galaxy(self, *args, **kwargs): try: version = self.print_version() or '1.0' except AttributeError: # handle the potential absence of print_version version = '1.0' self.tool = gxt.Tool(self.prog, self.prog, version, self.description, self.prog, interpreter='python', version_command='python %s --version' % sys.argv[0]) self.inputs = gxtp.Inputs() self.outputs = gxtp.Outputs() self.at = agt.ArgparseGalaxyTranslation() # Only build up arguments if the user actually requests it for result in self.argument_list: # I am SO thankful they return the argument here. SO useful. argument_type = result.__class__.__name__ # http://stackoverflow.com/a/3071 if hasattr(self.at, argument_type): methodToCall = getattr(self.at, argument_type) gxt_parameter = methodToCall(result, tool=self.tool) if gxt_parameter is not None: if isinstance(gxt_parameter, gxtp.InputParameter): self.inputs.append(gxt_parameter) else: self.outputs.append(gxt_parameter) # TODO: replace with argparse-esque library to do this. stdout = gxtp.OutputData('default', 'txt') stdout.command_line_override = '> $default' self.outputs.append(stdout) self.tool.inputs = self.inputs self.tool.outputs = self.outputs if self.epilog is not None: self.tool.help = self.epilog else: self.tool.help = "TODO: Write help" data = self.tool.export() print(data) sys.exit()
def _parse_args_galaxy_argp(self, argp): try: version = self.print_version() or '1.0' except AttributeError: # handle the potential absence of print_version version = '1.0' tool = gxt.Tool(argp.prog, argp.prog.replace(" ", "_"), version, argp.description, "python " + argp.prog, interpreter=None, version_command='python %s --version' % argp.prog) inputs = gxtp.Inputs() outputs = gxtp.Outputs() at = agt.ArgparseGalaxyTranslation() # Only build up arguments if the user actually requests it for result in argp.argument_list: # I am SO thankful they return the argument here. SO useful. argument_type = result.__class__.__name__ # http://stackoverflow.com/a/3071 if hasattr(at, argument_type): methodToCall = getattr(at, argument_type) gxt_parameter = methodToCall(result, tool=tool) if gxt_parameter is not None: if isinstance(gxt_parameter, gxtp.InputParameter): inputs.append(gxt_parameter) else: outputs.append(gxt_parameter) # TODO: replace with argparse-esque library to do this. stdout = gxtp.OutputData('default', 'txt') stdout.command_line_override = '> $default' outputs.append(stdout) tool.inputs = inputs tool.outputs = outputs if argp.epilog is not None: tool.help = argp.epilog else: tool.help = "TODO: Write help" return tool.export()
def add_input_file(self, input_obj): """ Add an input to the tool (XML: <inputs>). :param input_obj: Input object. :type input_obj: :class:`tooldog.biotool_model.Input` """ LOGGER.debug("Adding input to GalaxyToolGen object...") if not hasattr(self.tool, 'inputs'): self.tool.inputs = gxtp.Inputs() # Build parameter self.input_ct += 1 data_uri = input_obj.data_type.get_edam_id() # Give unique name to the input name = 'INPUT' + str(self.input_ct) # Get all different format for this input list_formats = [] if not input_obj.formats: list_formats.append(self.etog.get_datatype(edam_data=data_uri)) else: for format_obj in input_obj.formats: format_uri = format_obj.get_edam_id() list_formats.append( self.etog.get_datatype(edam_data=data_uri, edam_format=format_uri)) formats = ', '.join(list_formats) # Create the parameter param = gxtp.DataParam(name, label=input_obj.data_type.term, help=input_obj.description, format=formats) # Override the corresponding arguments in the command line param.command_line_override = '--' + name + ' $' + name # Write comment about this param param.node.insert(0, etree.Comment(FIXME)) param.node.insert(0, etree.Comment(PARAM_COMMENT % (self.biotool_id))) # Appends parameter to inputs self.tool.inputs.append(param)
def __init__(self, args=None): # noqa """ prepare command line cl for running the tool here and prepare elements needed for galaxyxml tool generation """ self.ourcwd = os.getcwd() self.collections = [] if len(args.collection) > 0: try: self.collections = [ json.loads(x) for x in args.collection if len(x.strip()) > 1 ] except Exception: print( f"--collections parameter {str(args.collection)} is malformed - should be a dictionary" ) try: self.infiles = [ json.loads(x) for x in args.input_files if len(x.strip()) > 1 ] except Exception: print( f"--input_files parameter {str(args.input_files)} is malformed - should be a dictionary" ) try: self.outfiles = [ json.loads(x) for x in args.output_files if len(x.strip()) > 1 ] except Exception: print( f"--output_files parameter {args.output_files} is malformed - should be a dictionary" ) try: self.addpar = [ json.loads(x) for x in args.additional_parameters if len(x.strip()) > 1 ] except Exception: print( f"--additional_parameters {args.additional_parameters} is malformed - should be a dictionary" ) try: self.selpar = [ json.loads(x) for x in args.selecttext_parameters if len(x.strip()) > 1 ] except Exception: print( f"--selecttext_parameters {args.selecttext_parameters} is malformed - should be a dictionary" ) self.args = args self.cleanuppar() self.lastxclredirect = None self.xmlcl = [] self.is_positional = self.args.parampass == "positional" if self.args.sysexe: if " " in self.args.sysexe: self.executeme = self.args.sysexe.split(" ") else: self.executeme = [ self.args.sysexe, ] else: if self.args.packages: self.executeme = [ self.args.packages.split(",")[0].split(":")[0].strip(), ] else: self.executeme = None aXCL = self.xmlcl.append assert args.parampass in [ "0", "argparse", "positional", ], 'args.parampass must be "0","positional" or "argparse"' self.tool_name = re.sub("[^a-zA-Z0-9_]+", "", args.tool_name) self.tool_id = self.tool_name self.newtool = gxt.Tool( self.tool_name, self.tool_id, self.args.tool_version, self.args.tool_desc, FAKEEXE, ) self.newtarpath = "%s_not_tested.toolshed.gz" % self.tool_name self.tooloutdir = "./tfout" self.repdir = "./toolgen" self.testdir = os.path.join(self.tooloutdir, "test-data") if not os.path.exists(self.tooloutdir): os.mkdir(self.tooloutdir) if not os.path.exists(self.testdir): os.mkdir(self.testdir) if not os.path.exists(self.repdir): os.mkdir(self.repdir) self.tinputs = gxtp.Inputs() self.toutputs = gxtp.Outputs() self.testparam = [] if self.args.script_path: self.prepScript() if self.args.command_override: scos = open(self.args.command_override, "r").readlines() self.command_override = [x.rstrip() for x in scos] else: self.command_override = None if self.args.test_override: stos = open(self.args.test_override, "r").readlines() self.test_override = [x.rstrip() for x in stos] else: self.test_override = None if self.args.script_path: for ex in self.executeme: aXCL(ex) aXCL("$runme") else: for ex in self.executeme: aXCL(ex) if self.args.parampass == "0": self.clsimple() else: if self.args.parampass == "positional": self.prepclpos() self.clpositional() else: self.prepargp() self.clargparse()