Added extra parameters to allow more control over the generation of Galaxy stubs
Luis de la Garza
9 years ago
32 | 32 | return self.msg |
33 | 33 | def __unicode__(self): |
34 | 34 | return self.msg |
35 | ||
36 | class ExitCode: | |
37 | def __init__(self, code_range="", level="", description=""): | |
38 | self.range = code_range | |
39 | self.level = level | |
40 | self.description = description | |
35 | 41 | |
36 | 42 | def main(argv=None): # IGNORE:C0111 |
37 | 43 | '''Command line options.''' |
77 | 83 | |
78 | 84 | try: |
79 | 85 | # Setup argument parser |
80 | parser = ArgumentParser(description=program_license, formatter_class=RawDescriptionHelpFormatter, add_help=True) | |
86 | parser = ArgumentParser(prog="GalaxyConfigGenerator", description=program_license, formatter_class=RawDescriptionHelpFormatter, add_help=True) | |
81 | 87 | parser.add_argument("-i", "--input-file", dest="input_file", help="provide a single input CTD file to convert", required=True) |
82 | 88 | parser.add_argument("-o", "--output-file", dest="output_file", help="provide a single output Galaxy wrapper file", required=True) |
83 | 89 | parser.add_argument("-a", "--add-to-command-line", dest="add_to_command_line", help="adds content to the command line", default="", required=False) |
86 | 92 | "validated against emptiness or being equal to 'None'", required=False) |
87 | 93 | parser.add_argument("-q", "--quote-parameters", dest="quote_parameters", action="store_true", default=False, |
88 | 94 | help="if true, each parameter in the generated command line will be quoted", required=False) |
89 | parser.add_argument("-b", "--blacklisted-parameters", dest="blacklisted_parameters", default="", | |
90 | help="comma separated list of parameters that will be ignored and won't appear on the galaxy stub", required=False) | |
91 | parser.add_argument("-p", "--package-requirements", dest="package_requirements", default="", | |
92 | help="comma separated list of required galaxy packages", required=False) | |
95 | parser.add_argument("-b", "--blacklisted-parameter", dest="blacklisted_parameters", default=[], nargs="+", action="append", | |
96 | help="list of parameters that will be ignored and won't appear on the galaxy stub", required=False) | |
97 | parser.add_argument("-p", "--package-requirement", dest="package_requirements", default=[], nargs="+", action="append", | |
98 | help="list of required galaxy packages", required=False) | |
99 | parser.add_argument("-x", "--exit-code", dest="exit_codes", default=[], nargs="+", action="append", | |
100 | help="list of <stdio> galaxy exit codes, in the following format: range=<range>,level=<level>,description=<description>,\n" + | |
101 | "example: --exit-codes \"range=3:4,level=fatal,description=Out of memory\"") | |
93 | 102 | # verbosity will be added later on, will not waste time on this now |
94 | 103 | # parser.add_argument("-v", "--verbose", dest="verbose", action="count", help="set verbosity level [default: %(default)s]") |
95 | 104 | parser.add_argument("-V", "--version", action='version', version=program_version_message) |
103 | 112 | |
104 | 113 | #if verbose > 0: |
105 | 114 | # print("Verbose mode on") |
106 | convert( | |
107 | input_file, | |
115 | convert(input_file, | |
108 | 116 | output_file, |
109 | 117 | add_to_command_line=args.add_to_command_line, |
110 | 118 | whitespace_validation=args.whitespace_validation, |
111 | 119 | quote_parameters=args.quote_parameters, |
112 | blacklisted_parameters=map(strip, args.blacklisted_parameters.split(",")), | |
113 | package_requirements=map(strip, args.package_requirements.split(","))) | |
120 | # remember that blacklisted_parameters, package_requirements and exit_codes are lists of lists of strings | |
121 | blacklisted_parameters=[item for sublist in args.blacklisted_parameters for item in sublist], | |
122 | package_requirements=[item for sublist in args.package_requirements for item in sublist], | |
123 | exit_codes=convert_exit_codes([item for sublist in args.exit_codes for item in sublist])) | |
114 | 124 | return 0 |
115 | 125 | |
116 | 126 | except KeyboardInterrupt: |
133 | 143 | traceback.print_exc() |
134 | 144 | return 2 |
135 | 145 | |
146 | def convert_exit_codes(exit_codes_raw): | |
147 | # input is in the format: | |
148 | # range=3:4,level=fatal,description=Out of memory | |
149 | exit_codes = [] | |
150 | for exit_code_raw in exit_codes_raw: | |
151 | exit_code = ExitCode() | |
152 | for key_value in exit_code_raw.split(","): | |
153 | # each key_value contains something like range=3 or description=whatever | |
154 | # so we can split again by using "=" | |
155 | key_value_split = key_value.split("=") | |
156 | setattr(exit_code, key_value_split[0], key_value_split[1].strip()) | |
157 | exit_codes.append(exit_code) | |
158 | return exit_codes | |
159 | ||
136 | 160 | def convert(input_file, output_file, **kwargs): |
137 | 161 | # first, generate a model |
138 | 162 | print("Parsing CTD from [%s]" % input_file) |
144 | 168 | create_description(doc, tool, model) |
145 | 169 | create_requirements(doc, tool, model, kwargs["package_requirements"]) |
146 | 170 | create_command(doc, tool, model, **kwargs) |
147 | create_inputs(doc, tool, model, **kwargs) | |
148 | create_outputs(doc, tool, model, **kwargs) | |
171 | create_inputs(doc, tool, model, kwargs["blacklisted_parameters"]) | |
172 | create_outputs(doc, tool, model, kwargs["blacklisted_parameters"]) | |
173 | create_exit_codes(doc, tool, model, kwargs["exit_codes"]) | |
149 | 174 | create_help(doc, tool, model) |
150 | 175 | |
151 | 176 | # finally, serialize the tool |
244 | 269 | def get_galaxy_parameter_name(param_name): |
245 | 270 | return "param_%s" % param_name |
246 | 271 | |
247 | def create_inputs(doc, tool, model, **kwargs): | |
272 | def create_inputs(doc, tool, model, blacklisted_parameters): | |
248 | 273 | inputs_node = doc.createElement("inputs") |
249 | 274 | # treat all non output-file parameters as inputs |
250 | 275 | for param in extract_parameters(model): |
251 | if param.name in kwargs["blacklisted_parameters"]: | |
276 | if param.name in blacklisted_parameters: | |
252 | 277 | # let's not use an extra level of indentation and use NOP |
253 | 278 | continue |
254 | 279 | if param.type is not _OutFile: |
377 | 402 | checked_value = "true" |
378 | 403 | param_node.setAttribute("checked", checked_value) |
379 | 404 | |
380 | def create_outputs(doc, tool, model, **kwargs): | |
405 | def create_outputs(doc, tool, model, blacklisted_parameters): | |
381 | 406 | outputs_node = doc.createElement("outputs") |
382 | 407 | for param in extract_parameters(model): |
383 | if param.name in kwargs["blacklisted_parameters"]: | |
408 | if param.name in blacklisted_parameters: | |
384 | 409 | # let's not use an extra level of indentation and use NOP |
385 | 410 | continue |
386 | 411 | if param.type is _OutFile: |
403 | 428 | |
404 | 429 | return data_node |
405 | 430 | |
431 | def create_exit_codes(doc, tool, model, exit_codes): | |
432 | if len(exit_codes) > 0: | |
433 | stdio_node = doc.createElement("stdio") | |
434 | for exit_code in exit_codes: | |
435 | exit_code_node = doc.createElement("exit_code") | |
436 | exit_code_node.setAttribute("range", exit_code.range) | |
437 | exit_code_node.setAttribute("level", exit_code.level) | |
438 | exit_code_node.setAttribute("description", exit_code.description) | |
439 | stdio_node.appendChild(exit_code_node) | |
440 | tool.appendChild(stdio_node) | |
441 | ||
406 | 442 | def create_help(doc, tool, model): |
407 | 443 | manual = None |
408 | 444 | doc_url = None |
409 | 445 | if 'manual' in model.opt_attribs.keys(): |
410 | model.opt_attribs["manual"] | |
446 | manual = model.opt_attribs["manual"] | |
411 | 447 | if 'docurl' in model.opt_attribs.keys(): |
412 | model.opt_attribs["docurl"] | |
448 | doc_url = model.opt_attribs["docurl"] | |
413 | 449 | help_text = "No help available" |
414 | 450 | if manual is not None: |
415 | 451 | help_text = manual |
416 | 452 | if doc_url is not None: |
417 | help_text = ("" if manual is None else manual) + " For more information, visit %s" % doc_url | |
453 | help_text = ("" if manual is None else manual) + "\nFor more information, visit %s" % doc_url | |
418 | 454 | |
419 | 455 | help_node = doc.createElement("help") |
456 | # TODO: do we need CDATA Section here? | |
420 | 457 | help_node.appendChild(doc.createTextNode(help_text)) |
421 | 458 | tool.appendChild(help_node) |
422 | 459 |