From b67cb4f2db765b54c1fc147422609d2dba883650 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Wed, 29 Apr 2020 21:00:41 +0200 Subject: [PATCH 01/23] subparsers that are parents become macros - implement output to dir (--directory) to output separate tool per subparser - prepare to include global macro file (for including requirements, citations, help, ...) --- argparse2tool/cmdline2gxml/__init__.py | 4 + argparse2tool/dropins/argparse/__init__.py | 104 ++++++++++++++++----- 2 files changed, 87 insertions(+), 21 deletions(-) diff --git a/argparse2tool/cmdline2gxml/__init__.py b/argparse2tool/cmdline2gxml/__init__.py index 91bfef9..3ac8d19 100644 --- a/argparse2tool/cmdline2gxml/__init__.py +++ b/argparse2tool/cmdline2gxml/__init__.py @@ -23,4 +23,8 @@ def __init__(self): def process_arguments(self): self.parser.add_argument('--generate_galaxy_xml', action='store_true') + self.parser.add_argument('-d', '--directory', + help='Directory to store CWL tool descriptions') + self.parser.add_argument('-m', '--macro', + help='A global macro file to include in all tools') return vars(self.parser.parse_args()) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index 0cf9e26..c1d636c 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -21,6 +21,15 @@ setattr(__selfmodule__, x, getattr(ap, x)) tools = [] +# set of prog names where the argparser actually should be represented +# as a macro, i.e. XYZ will be in the set if there is a +# ArgumentParser(..., parents=[XYZ], ...) +macros = set() + +# mapping tools to list of required macros (which are actually identical to the +# parents parameter passed ArgumentParser. but I could not find out how this is +# stored in the created ArgumentParser objects +used_macros = dict() class ArgumentParser(ap.ArgumentParser): @@ -37,10 +46,16 @@ def __init__(self, argument_default=None, conflict_handler='error', add_help=True): + global macros + global used_macros self.argument_list = [] self.argument_names = [] tools.append(self) + if len(parents) > 0: + p = set([_.prog.split()[-1] for _ in parents]) + macros = macros.union(p) + used_macros[prog.split()[-1]] = p super(ArgumentParser, self).__init__(prog=prog, usage=usage, description=description, @@ -128,6 +143,25 @@ def parse_args_cwl(self, *args, **kwargs): sys.exit(0) def parse_args_galaxy(self, *args, **kwargs): + global used_macros + + directory = kwargs.get('directory', None) + macro = kwargs.get('macro', None) + + # since macros can also make use of macros (i.e. the parent relation + # specified in the arguments can be nester) we need to extend the + # used macros such that really all are included + ext_used_macros = dict() + for tool, macros in used_macros.items(): + ext_used_macros[tool] = set() + q = list(macros) + while len(q) > 0: + m = q.pop() + if m in used_macros: + q.extend(used_macros[m]) + ext_used_macros[tool].add(m) + used_macros = ext_used_macros + for argp in tools: # make subparser description out of its help message if argp._subparsers: @@ -139,28 +173,53 @@ def parse_args_galaxy(self, *args, **kwargs): subparser.choices[choice_action.dest].description = choice_action.help else: if kwargs.get('command', argp.prog) in argp.prog: - data = self._parse_args_galaxy_argp(argp) - print(data) + data = self._parse_args_galaxy_argp(argp, macro) + if directory: + if directory[-1] != '/': + directory += '/' + filename = argp.prog.split()[-1] + ".xml" + filename = directory + filename + with open(filename, 'a') as f: + f.write(data) + else: + print(data) else: continue sys.exit(0) - def _parse_args_galaxy_argp(self, argp): + def _parse_args_galaxy_argp(self, argp, macro): + global macros + global used_macros + 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() + + tid = argp.prog.split()[-1] + + # get the list of file names of the used macros + mx = used_macros.get(tid, []) + mx = ["%s.xml" % _ for _ in mx] + + if tid not in macros: + tpe = gxt.Tool + if macro: + mx.append(macro) + else: + tpe = gxt.MacrosTool + + tool = tpe(argp.prog, + argp.prog.replace(" ", "_"), + version, + argp.description, + "python "+argp.prog, + interpreter=None, + version_command='python %s --version' % argp.prog, + macros=mx) + + inputs = tool.inputs + outputs = tool.outputs at = agt.ArgparseGalaxyTranslation() # Only build up arguments if the user actually requests it @@ -177,16 +236,19 @@ def _parse_args_galaxy_argp(self, argp): 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) + if tid in used_macros: + for m in used_macros[tid]: + inputs.append(gxtp.ExpandIO(m + "_inmacro")) + outputs.append(gxtp.ExpandIO(m + "_outmacro")) + + if tid not in macros: + # 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() From 00d302daeef6cd1b87d2cbec016670a3d2e3f52b Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Thu, 30 Apr 2020 10:22:07 +0200 Subject: [PATCH 02/23] install galaxyxml from branch corresponding to https://github.com/hexylena/galaxyxml/pull/18 --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 47738a6..665ef4d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,8 @@ script: - pip install -q -U planemo flake8 xmldiff - pip install -q -r requirements.txt - python setup.py install +# temporarily install galaxyxml from branch https://github.com/hexylena/galaxyxml/pull/18 +- git clone -b topic/macros https://github.com/bernt-matthias/galaxyxml.git && cd galaxyxml && python setup.py install && cd .. - flake8 argparse2tool - PYTHONPATH=$(argparse2tool) python examples/example.py --generate_galaxy_xml > tmp.xml - echo '' > tmp-sub.xml # wrap in extra level From a8991dffbcd2920e29b66b810f9221e4b64388a7 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Thu, 30 Apr 2020 10:38:08 +0200 Subject: [PATCH 03/23] adapt travis test for submodules - output to dir, diff dirs, planemo dir --- examples/example-sub/bar.xml | 37 ++++++++++++++++++++++++++++++++++ examples/example-sub/foo.xml | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 examples/example-sub/bar.xml create mode 100644 examples/example-sub/foo.xml diff --git a/examples/example-sub/bar.xml b/examples/example-sub/bar.xml new file mode 100644 index 0000000..1e8e0d2 --- /dev/null +++ b/examples/example-sub/bar.xml @@ -0,0 +1,37 @@ + + + + + + python example-sub.py bar --version + $default]]> + + + + + + + + + + + + + + + + + + diff --git a/examples/example-sub/foo.xml b/examples/example-sub/foo.xml new file mode 100644 index 0000000..17da279 --- /dev/null +++ b/examples/example-sub/foo.xml @@ -0,0 +1,39 @@ + + + + + + python example-sub.py foo --version + $default]]> + + + + + + + + + + + + + + + + From d1fceab1e22b4b2503f56e1142cc0531ef560670 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Thu, 30 Apr 2020 12:01:17 +0200 Subject: [PATCH 04/23] switch to py3.7 for testing since dicts (and hopefully also attributes) are ordered --- .travis.yml | 2 +- examples/example-sub/bar.xml | 8 ++++---- examples/example-sub/foo.xml | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 665ef4d..0dc0344 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: python python: -- '3.6' +- '3.7' install: pip install -r requirements.txt script: - pip install -q -U setuptools diff --git a/examples/example-sub/bar.xml b/examples/example-sub/bar.xml index 1e8e0d2..383375f 100644 --- a/examples/example-sub/bar.xml +++ b/examples/example-sub/bar.xml @@ -21,17 +21,17 @@ - - + + - + - diff --git a/examples/example-sub/foo.xml b/examples/example-sub/foo.xml index 17da279..68de07f 100644 --- a/examples/example-sub/foo.xml +++ b/examples/example-sub/foo.xml @@ -22,18 +22,18 @@ $true > $default]]> - + - + - + - From c01eede0498899044881fb7df0c9c1c133170e27 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Thu, 30 Apr 2020 12:54:15 +0200 Subject: [PATCH 05/23] update example.py --- examples/example.xml | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/examples/example.xml b/examples/example.xml index ccaba3f..d3a89e6 100644 --- a/examples/example.xml +++ b/examples/example.xml @@ -1,7 +1,7 @@ - + Process some integers. - + $default]]> - - - + + + - - - - + + + + - - - - + + + + - - + + - + - From f9372265edd84f2fedd8eb4dd4b4824747dd292b Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Thu, 30 Apr 2020 18:27:03 +0200 Subject: [PATCH 06/23] extend example-sub for parent parsers --- examples/example-sub.py | 23 ++++---- examples/example-sub/bar.xml | 88 ++++++++++++++++++++++++++++++ examples/example-sub/baz.xml | 26 +++++++++ examples/example-sub/foo.xml | 102 +++++++++++++++++++++++++++++++++++ examples/example-sub/qux.xml | 14 +++++ 5 files changed, 243 insertions(+), 10 deletions(-) create mode 100644 examples/example-sub/baz.xml create mode 100644 examples/example-sub/qux.xml diff --git a/examples/example-sub.py b/examples/example-sub.py index b5da603..4cb8418 100644 --- a/examples/example-sub.py +++ b/examples/example-sub.py @@ -6,27 +6,30 @@ subparsers = parent.add_subparsers() -parser_foo = subparsers.add_parser('foo') -parser_bar = subparsers.add_parser('bar') +parser_qux = subparsers.add_parser('qux', add_help=False) +parser_baz = subparsers.add_parser('baz', parents=[parser_qux], add_help=False) -parser_foo.add_argument('keyword', metavar='Q', type=str, nargs=1, help='action keyword') +parser_foo = subparsers.add_parser('foo', parents=[parser_baz]) +parser_bar = subparsers.add_parser('bar', parents=[parser_qux]) +parser_foo.add_argument('keyword', metavar='Q', type=str, nargs=1, + help='action keyword') parser_foo.add_argument('integers', metavar='N', type=int, nargs='+', - help='an integer for the accumulator') - + help='an integer for the accumulator') parser_foo.add_argument('--sum', '-s', dest='accumulate', action='store_const', - const=sum, default=max, help='sum the integers (default: find the max)') - + const=sum, default=max, + help='sum the integers (default: find the max)') parser_foo.add_argument('--foo', nargs='?', help='foo help') parser_foo.add_argument('--bar', nargs='*', default=[1, 2, 3], help='BAR!') parser_foo.add_argument('--true', action='store_true', help='Store a true') + parser_bar.add_argument('--false', action='store_false', help='Store a false') parser_bar.add_argument('--append', action='append', help='Append a value') - parser_bar.add_argument('--nargs2', nargs=2, help='nargs2') - parser_bar.add_argument('--mode', choices=['rock', 'paper', 'scissors'], default='scissors') +parser_bar.add_argument('--version', action='version', version='2.0') +parser_baz.add_argument('--baz', metavar='BAZ', type=str, nargs=1, help='baz help') +parser_qux.add_argument('--qux', metavar='QUX', type=str, nargs=1, help='qux help') -parser_bar.add_argument('--version', action='version', version='2.0') args = parent.parse_args() diff --git a/examples/example-sub/bar.xml b/examples/example-sub/bar.xml index 383375f..a30aa8c 100644 --- a/examples/example-sub/bar.xml +++ b/examples/example-sub/bar.xml @@ -35,3 +35,91 @@ + + + + qux.xml + + + + + python example-sub.py bar --version + $default]]> + + + + + + + + + + + + + + + + + + + + + + + + qux.xml + + + + + python example-sub.py bar --version + $default]]> + + + + + + + + + + + + + + + + + + + + diff --git a/examples/example-sub/baz.xml b/examples/example-sub/baz.xml new file mode 100644 index 0000000..514a686 --- /dev/null +++ b/examples/example-sub/baz.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/example-sub/foo.xml b/examples/example-sub/foo.xml index 68de07f..9c77ede 100644 --- a/examples/example-sub/foo.xml +++ b/examples/example-sub/foo.xml @@ -37,3 +37,105 @@ $true + + + + baz.xml + qux.xml + + + + + python example-sub.py foo --version + $default]]> + + + + + + + + + + + + + + + + + + + + + + + + qux.xml + baz.xml + + + + + python example-sub.py foo --version + $default]]> + + + + + + + + + + + + + + + + + + + + diff --git a/examples/example-sub/qux.xml b/examples/example-sub/qux.xml new file mode 100644 index 0000000..56e63de --- /dev/null +++ b/examples/example-sub/qux.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + From cfb73fbd5bc2cfd92b0688c0b695e38d225e57b6 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Thu, 30 Apr 2020 18:55:46 +0200 Subject: [PATCH 07/23] beautify tool name and id - remove '.py' extension if existent - replace '-' by '_' - for id: replace ' ' by '_' also overwrite tool xml files (wo warning) --- argparse2tool/dropins/argparse/__init__.py | 14 +++- examples/example-sub/bar.xml | 83 +------------------ examples/example-sub/baz.xml | 22 +----- examples/example-sub/foo.xml | 92 +--------------------- examples/example-sub/qux.xml | 14 +--- 5 files changed, 21 insertions(+), 204 deletions(-) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index c1d636c..d3e788c 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -179,7 +179,7 @@ def parse_args_galaxy(self, *args, **kwargs): directory += '/' filename = argp.prog.split()[-1] + ".xml" filename = directory + filename - with open(filename, 'a') as f: + with open(filename, 'w') as f: f.write(data) else: print(data) @@ -196,6 +196,14 @@ def _parse_args_galaxy_argp(self, argp, macro): except AttributeError: # handle the potential absence of print_version version = '1.0' + prog = argp.prog + if prog is not None: + prog = prog.replace("-", "_") + prog = prog.replace(".py ", " ") + if prog.endswith(".py"): + prog = prog[:-3] + + tid = argp.prog.split()[-1] # get the list of file names of the used macros @@ -209,8 +217,8 @@ def _parse_args_galaxy_argp(self, argp, macro): else: tpe = gxt.MacrosTool - tool = tpe(argp.prog, - argp.prog.replace(" ", "_"), + tool = tpe(prog, + prog.replace(" ", "_"), version, argp.description, "python "+argp.prog, diff --git a/examples/example-sub/bar.xml b/examples/example-sub/bar.xml index a30aa8c..767408b 100644 --- a/examples/example-sub/bar.xml +++ b/examples/example-sub/bar.xml @@ -1,85 +1,4 @@ - - - - - - python example-sub.py bar --version - $default]]> - - - - - - - - - - - - - - - - - - - - - - qux.xml - - - - - python example-sub.py bar --version - $default]]> - - - - - - - - - - - - - - - - - - - - - + qux.xml diff --git a/examples/example-sub/baz.xml b/examples/example-sub/baz.xml index 514a686..00dff10 100644 --- a/examples/example-sub/baz.xml +++ b/examples/example-sub/baz.xml @@ -1,26 +1,12 @@ - - - + + - - - - - - - - - - - - - + diff --git a/examples/example-sub/foo.xml b/examples/example-sub/foo.xml index 9c77ede..33580c1 100644 --- a/examples/example-sub/foo.xml +++ b/examples/example-sub/foo.xml @@ -1,94 +1,4 @@ - - - - - - python example-sub.py foo --version - $default]]> - - - - - - - - - - - - - - - - - - - - baz.xml - qux.xml - - - - - python example-sub.py foo --version - $default]]> - - - - - - - - - - - - - - - - - - - - - + qux.xml diff --git a/examples/example-sub/qux.xml b/examples/example-sub/qux.xml index 56e63de..483250a 100644 --- a/examples/example-sub/qux.xml +++ b/examples/example-sub/qux.xml @@ -1,14 +1,8 @@ - - - - - - - - - + + + - + From 7f6fa0c0bac0529beec32bc692d6c2676616a977 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Thu, 30 Apr 2020 19:55:23 +0200 Subject: [PATCH 08/23] fix prog/tool naming issues --- argparse2tool/__init__.py | 8 +++++ argparse2tool/dropins/argparse/__init__.py | 33 +++++++++---------- examples/example-sub.py | 2 +- .../{bar.xml => example_sub_bar.xml} | 12 +++---- .../{baz.xml => example_sub_baz.xml} | 8 ++--- .../{foo.xml => example_sub_foo.xml} | 22 ++++++------- .../{qux.xml => example_sub_qux.xml} | 0 examples/example.xml | 2 +- 8 files changed, 46 insertions(+), 41 deletions(-) rename examples/example-sub/{bar.xml => example_sub_bar.xml} (84%) rename examples/example-sub/{baz.xml => example_sub_baz.xml} (54%) rename examples/example-sub/{foo.xml => example_sub_foo.xml} (76%) rename examples/example-sub/{qux.xml => example_sub_qux.xml} (100%) diff --git a/argparse2tool/__init__.py b/argparse2tool/__init__.py index f87bc88..0245d43 100644 --- a/argparse2tool/__init__.py +++ b/argparse2tool/__init__.py @@ -48,3 +48,11 @@ def load_conflicting_package(name, not_name, module_number): imp.load_module(random_name, f, pathname, desc) return sys.modules[random_name] return None + + +def remove_extension(name): + if name is not None: + name = name.replace('.py', '') + name = name.replace('-', '_') + name = name.replace(' ', '_') + return name diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index d3e788c..696a6b2 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -1,6 +1,9 @@ import re import sys -from argparse2tool import load_argparse +from argparse2tool import ( + load_argparse, + remove_extension +) from argparse2tool.cmdline2gxml import Arg2GxmlParser from argparse2tool.cmdline2cwl import Arg2CWLParser @@ -53,9 +56,9 @@ def __init__(self, self.argument_names = [] tools.append(self) if len(parents) > 0: - p = set([_.prog.split()[-1] for _ in parents]) + p = set([remove_extension(_.prog) for _ in parents]) macros = macros.union(p) - used_macros[prog.split()[-1]] = p + used_macros[remove_extension(prog)] = p super(ArgumentParser, self).__init__(prog=prog, usage=usage, description=description, @@ -177,7 +180,7 @@ def parse_args_galaxy(self, *args, **kwargs): if directory: if directory[-1] != '/': directory += '/' - filename = argp.prog.split()[-1] + ".xml" + filename = remove_extension(argp.prog) + ".xml" filename = directory + filename with open(filename, 'w') as f: f.write(data) @@ -196,21 +199,15 @@ def _parse_args_galaxy_argp(self, argp, macro): except AttributeError: # handle the potential absence of print_version version = '1.0' - prog = argp.prog - if prog is not None: - prog = prog.replace("-", "_") - prog = prog.replace(".py ", " ") - if prog.endswith(".py"): - prog = prog[:-3] - + prog = remove_extension(argp.prog) - tid = argp.prog.split()[-1] + # tid = argp.prog.split()[-1] # get the list of file names of the used macros - mx = used_macros.get(tid, []) - mx = ["%s.xml" % _ for _ in mx] + mx = used_macros.get(prog, []) + mx = sorted(["%s.xml" % _.split(" ")[-1] for _ in mx]) - if tid not in macros: + if prog not in macros: tpe = gxt.Tool if macro: mx.append(macro) @@ -244,12 +241,12 @@ def _parse_args_galaxy_argp(self, argp, macro): else: outputs.append(gxt_parameter) - if tid in used_macros: - for m in used_macros[tid]: + if prog in used_macros: + for m in sorted(used_macros[prog]): inputs.append(gxtp.ExpandIO(m + "_inmacro")) outputs.append(gxtp.ExpandIO(m + "_outmacro")) - if tid not in macros: + if prog not in macros: # TODO: replace with argparse-esque library to do this. stdout = gxtp.OutputData('default', 'txt') stdout.command_line_override = '> $default' diff --git a/examples/example-sub.py b/examples/example-sub.py index 4cb8418..20a77bd 100644 --- a/examples/example-sub.py +++ b/examples/example-sub.py @@ -17,7 +17,7 @@ parser_foo.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser_foo.add_argument('--sum', '-s', dest='accumulate', action='store_const', - const=sum, default=max, + const=sum, default=max, help='sum the integers (default: find the max)') parser_foo.add_argument('--foo', nargs='?', help='foo help') parser_foo.add_argument('--bar', nargs='*', default=[1, 2, 3], help='BAR!') diff --git a/examples/example-sub/bar.xml b/examples/example-sub/example_sub_bar.xml similarity index 84% rename from examples/example-sub/bar.xml rename to examples/example-sub/example_sub_bar.xml index 767408b..095a0fe 100644 --- a/examples/example-sub/bar.xml +++ b/examples/example-sub/example_sub_bar.xml @@ -1,7 +1,7 @@ - + - qux.xml + example_sub_qux.xml @@ -18,8 +18,8 @@ #if $mode and $mode is not None: --mode $mode #end if -@QUX_INMACRO@ -@QUX_OUTMACRO@ +@EXAMPLE_SUB_QUX_INMACRO@ +@EXAMPLE_SUB_QUX_OUTMACRO@ > $default]]> @@ -34,10 +34,10 @@ - + - + diff --git a/examples/example-sub/baz.xml b/examples/example-sub/example_sub_baz.xml similarity index 54% rename from examples/example-sub/baz.xml rename to examples/example-sub/example_sub_baz.xml index 00dff10..92fa962 100644 --- a/examples/example-sub/baz.xml +++ b/examples/example-sub/example_sub_baz.xml @@ -1,12 +1,12 @@ - +@EXAMPLE_SUB_QUX_INMACRO@]]> + - + - + diff --git a/examples/example-sub/foo.xml b/examples/example-sub/example_sub_foo.xml similarity index 76% rename from examples/example-sub/foo.xml rename to examples/example-sub/example_sub_foo.xml index 33580c1..f607d3d 100644 --- a/examples/example-sub/foo.xml +++ b/examples/example-sub/example_sub_foo.xml @@ -1,8 +1,8 @@ - + - qux.xml - baz.xml + example_sub_baz.xml + example_sub_qux.xml @@ -23,10 +23,10 @@ $sum --bar "$repeat_var_4" $true -@QUX_INMACRO@ -@BAZ_INMACRO@ -@QUX_OUTMACRO@ -@BAZ_OUTMACRO@ +@EXAMPLE_SUB_BAZ_INMACRO@ +@EXAMPLE_SUB_QUX_INMACRO@ +@EXAMPLE_SUB_BAZ_OUTMACRO@ +@EXAMPLE_SUB_QUX_OUTMACRO@ > $default]]> @@ -39,12 +39,12 @@ $true - - + + - - + + diff --git a/examples/example-sub/qux.xml b/examples/example-sub/example_sub_qux.xml similarity index 100% rename from examples/example-sub/qux.xml rename to examples/example-sub/example_sub_qux.xml diff --git a/examples/example.xml b/examples/example.xml index d3a89e6..4b58581 100644 --- a/examples/example.xml +++ b/examples/example.xml @@ -1,4 +1,4 @@ - + Process some integers. From c5c38fbef82ca568e4ef31154fecae16e0959656 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Tue, 22 Dec 2020 17:38:57 +0100 Subject: [PATCH 09/23] create sections from Groups create elements from _actions --- argparse2tool/dropins/argparse/__init__.py | 54 +++++++++++++++---- .../argparse/argparse_galaxy_translation.py | 5 ++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index 696a6b2..43f8c90 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -225,21 +225,57 @@ def _parse_args_galaxy_argp(self, argp, macro): inputs = tool.inputs outputs = tool.outputs + sections = dict() at = agt.ArgparseGalaxyTranslation() - # Only build up arguments if the user actually requests it - for result in argp.argument_list: + + for group in argp._action_groups: + if group in [argp._positionals, argp._optionals]: + continue + argument_type = group.__class__.__name__ + methodToCall = getattr(at, argument_type) + sections[group] = methodToCall(group, tool=tool) + + for action in argp._actions: # I am SO thankful they return the argument here. SO useful. - argument_type = result.__class__.__name__ + argument_type = action.__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) + gxt_parameter = methodToCall(action, tool=tool) + if gxt_parameter is None: + # TODO ERROR MESSAGE + continue + + if not isinstance(gxt_parameter, gxtp.InputParameter): + outputs.append(gxt_parameter) + if action.container in sections: + sections[action.container].append(gxt_parameter) + else: + inputs.append(gxt_parameter) + else: + # TODO ERROR MESSAGE + pass + + for section in sections: + inputs.append(sections[section]) + + +# print("argp._action_groups %s" % argp._action_groups) +# # 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__ +# print("_parse_args_galaxy_argp %s type %s [%s]" % (getattr(result, "title", "no title"), argument_type, hasattr(at, argument_type))) +# # 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) if prog in used_macros: for m in sorted(used_macros[prog]): diff --git a/argparse2tool/dropins/argparse/argparse_galaxy_translation.py b/argparse2tool/dropins/argparse/argparse_galaxy_translation.py index b451d4c..b64aaf4 100644 --- a/argparse2tool/dropins/argparse/argparse_galaxy_translation.py +++ b/argparse2tool/dropins/argparse/argparse_galaxy_translation.py @@ -247,3 +247,8 @@ def _StoreConstAction(self, param, **kwargs): gxparam = gxtp.BooleanParam(flag_wo_dashes, label=param.help, num_dashes=num_dashes) return gxparam + + def _ArgumentGroup(self, param, **kwargs): + return gxtp.Section(name= param.title.replace(" ", "_"), + title=param.title, + help=param.description) From dd75615c88240c63652ba0bd02adbdc70c71e35f Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Wed, 23 Dec 2020 09:26:29 +0100 Subject: [PATCH 10/23] update travis --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0dc0344..aa408a3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,9 +6,9 @@ script: - pip install -q -U setuptools - pip install -q -U planemo flake8 xmldiff - pip install -q -r requirements.txt -- python setup.py install +- python3 -m pip install --no-deps --no-cache-dir --force-reinstall . # temporarily install galaxyxml from branch https://github.com/hexylena/galaxyxml/pull/18 -- git clone -b topic/macros https://github.com/bernt-matthias/galaxyxml.git && cd galaxyxml && python setup.py install && cd .. +- git clone -b topic/macros https://github.com/bernt-matthias/galaxyxml.git && cd galaxyxml && python3 -m pip install --no-deps --no-cache-dir --force-reinstall . && cd .. - flake8 argparse2tool - PYTHONPATH=$(argparse2tool) python examples/example.py --generate_galaxy_xml > tmp.xml - echo '' > tmp-sub.xml # wrap in extra level From ed8cc1545fc554b6f2dd54e3eed0b10646aa9539 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Wed, 23 Dec 2020 09:31:51 +0100 Subject: [PATCH 11/23] update travis flake exceptions --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 414381f..4d62be9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ script: - python3 -m pip install --no-deps --no-cache-dir --force-reinstall . # temporarily install galaxyxml from branch https://github.com/hexylena/galaxyxml/pull/18 - git clone -b topic/macros https://github.com/bernt-matthias/galaxyxml.git && cd galaxyxml && python3 -m pip install --no-deps --no-cache-dir --force-reinstall . && cd .. -- flake8 argparse2tool --ignore=E2,E3,E4,E5,W3,W505 +- flake8 argparse2tool --ignore=C901,E2,E3,E4,E5,W3,W505 - PYTHONPATH=$(argparse2tool) python examples/example.py --generate_galaxy_xml > tmp.xml - xmldiff tmp.xml examples/example.xml - planemo lint --report_level all --fail_level error --xsd tmp.xml From 56d1a935644788f064285d6c7dab6ac08372fb44 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Wed, 23 Dec 2020 09:32:10 +0100 Subject: [PATCH 12/23] fix whitespace on empty line --- argparse2tool/dropins/argparse/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index 43f8c90..5c1a468 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -246,7 +246,6 @@ def _parse_args_galaxy_argp(self, argp, macro): if gxt_parameter is None: # TODO ERROR MESSAGE continue - if not isinstance(gxt_parameter, gxtp.InputParameter): outputs.append(gxt_parameter) if action.container in sections: From c24b32355803a038b8c5d7c6a78d33d5038e33d7 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Wed, 23 Dec 2020 09:55:02 +0100 Subject: [PATCH 13/23] adapt tests for macros --- .travis.yml | 14 ++--- examples/example-sub-bar.xml | 25 ++++++--- examples/example-sub-foo.xml | 32 ++++++++---- examples/example-sub.xml | 99 ++++++++++++++++++++++++++---------- examples/example.xml | 12 +++-- 5 files changed, 128 insertions(+), 54 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4d62be9..68e648e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,23 +12,23 @@ script: # temporarily install galaxyxml from branch https://github.com/hexylena/galaxyxml/pull/18 - git clone -b topic/macros https://github.com/bernt-matthias/galaxyxml.git && cd galaxyxml && python3 -m pip install --no-deps --no-cache-dir --force-reinstall . && cd .. - flake8 argparse2tool --ignore=C901,E2,E3,E4,E5,W3,W505 -- PYTHONPATH=$(argparse2tool) python examples/example.py --generate_galaxy_xml > tmp.xml +- PYTHONPATH=$(argparse2tool) python examples/example.py --generate_galaxy_xml -m examples/macros.xml > tmp.xml - xmldiff tmp.xml examples/example.xml -- planemo lint --report_level all --fail_level error --xsd tmp.xml +- planemo lint -skip tests --report_level all --fail_level error --xsd tmp.xml # Galaxy tool generation for example with subparsers -- generating one large (invalid) tool - echo '' > tmp-sub.xml # wrap in extra level -- PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml >> tmp-sub.xml +- PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml -m examples/macros.xml >> tmp-sub.xml - echo '' >> tmp-sub.xml - xmldiff tmp-sub.xml <(echo ""; cat examples/example-sub.xml; echo "") # Galaxy tool generation for example with subparsers -- generating separate tools -- PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml --command foo > tmp-sub-foo.xml -- PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml --command bar > tmp-sub-bar.xml +- PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml -m examples/macros.xml --command foo > tmp-sub-foo.xml +- PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml -m examples/macros.xml --command bar > tmp-sub-bar.xml - xmldiff tmp-sub-foo.xml examples/example-sub-foo.xml -- planemo lint --report_level all --fail_level error --xsd tmp-sub-foo.xml +- planemo lint --skip tests --report_level all --fail_level error --xsd tmp-sub-foo.xml - xmldiff tmp-sub-bar.xml examples/example-sub-bar.xml -- planemo lint --report_level all --fail_level error --xsd tmp-sub-bar.xml +- planemo lint --skip tests --report_level all --fail_level error --xsd tmp-sub-bar.xml - PYTHONPATH=$(argparse2tool) python examples/example.py --generate_cwl_tool > tmp.cwl - PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_cwl_tool > tmp-sub.cwl diff --git a/examples/example-sub-bar.xml b/examples/example-sub-bar.xml index e2462ee..8db4301 100644 --- a/examples/example-sub-bar.xml +++ b/examples/example-sub-bar.xml @@ -1,8 +1,11 @@ - + - - - + + example_sub_qux.xml + examples/macros.xml + + + $default]]> - - + + - + + - + + diff --git a/examples/example-sub-foo.xml b/examples/example-sub-foo.xml index 8eacac6..3aea0e1 100644 --- a/examples/example-sub-foo.xml +++ b/examples/example-sub-foo.xml @@ -1,8 +1,12 @@ - + - - - + + example_sub_baz.xml + example_sub_qux.xml + examples/macros.xml + + + $default]]> - + - + - + + + - + + diff --git a/examples/example-sub.xml b/examples/example-sub.xml index 56dae02..980580f 100644 --- a/examples/example-sub.xml +++ b/examples/example-sub.xml @@ -1,8 +1,35 @@ - + + + + + + + + + + + + + + + + + + + + + + + - - - + + example_sub_baz.xml + example_sub_qux.xml + examples/macros.xml + + + $default]]> - - - + + + - - - - + + + + - + + + - + + - + - - - - python example-sub.py bar --version + + example_sub_qux.xml + examples/macros.xml + + + + $default]]> - - - + + + - - + + - + + - + + + diff --git a/examples/example.xml b/examples/example.xml index 4b58581..5839e5b 100644 --- a/examples/example.xml +++ b/examples/example.xml @@ -1,8 +1,10 @@ Process some integers. - - - + + examples/macros.xml + + + From b40d46eb09f2db5a48a1b19f571fc4d30d90f061 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Wed, 23 Dec 2020 13:37:22 +0100 Subject: [PATCH 14/23] allow to use multiple global macro files and test cleanup --- .travis.yml | 21 ++++++--- argparse2tool/cmdline2gxml/__init__.py | 2 +- argparse2tool/dropins/argparse/__init__.py | 2 +- examples/example-sub-bar.xml | 47 ------------------- examples/example-sub-foo.xml | 54 ---------------------- examples/example-sub/example_sub_bar.xml | 11 +++-- examples/example-sub/example_sub_baz.xml | 2 +- examples/example-sub/example_sub_foo.xml | 13 ++++-- examples/example-sub/example_sub_qux.xml | 2 +- examples/macros.xml | 27 +++++++++++ examples/macros_tests.xml | 16 +++++++ 11 files changed, 77 insertions(+), 120 deletions(-) delete mode 100644 examples/example-sub-bar.xml delete mode 100644 examples/example-sub-foo.xml create mode 100644 examples/macros.xml create mode 100644 examples/macros_tests.xml diff --git a/.travis.yml b/.travis.yml index 68e648e..5db478d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ script: - flake8 argparse2tool --ignore=C901,E2,E3,E4,E5,W3,W505 - PYTHONPATH=$(argparse2tool) python examples/example.py --generate_galaxy_xml -m examples/macros.xml > tmp.xml - xmldiff tmp.xml examples/example.xml -- planemo lint -skip tests --report_level all --fail_level error --xsd tmp.xml +- planemo lint --skip tests --report_level all --fail_level error --xsd tmp.xml # Galaxy tool generation for example with subparsers -- generating one large (invalid) tool - echo '' > tmp-sub.xml # wrap in extra level @@ -22,13 +22,22 @@ script: - echo '' >> tmp-sub.xml - xmldiff tmp-sub.xml <(echo ""; cat examples/example-sub.xml; echo "") -# Galaxy tool generation for example with subparsers -- generating separate tools +# Galaxy tool generation for example with subparsers -- generating separate tools (this does not generate macro xmls automatically) - PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml -m examples/macros.xml --command foo > tmp-sub-foo.xml - PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml -m examples/macros.xml --command bar > tmp-sub-bar.xml -- xmldiff tmp-sub-foo.xml examples/example-sub-foo.xml -- planemo lint --skip tests --report_level all --fail_level error --xsd tmp-sub-foo.xml -- xmldiff tmp-sub-bar.xml examples/example-sub-bar.xml -- planemo lint --skip tests --report_level all --fail_level error --xsd tmp-sub-bar.xml +- xmldiff tmp-sub-foo.xml examples/example-sub/example_sub_foo.xml +- xmldiff tmp-sub-bar.xml examples/example-sub/example_sub_bar.xml + +# Galaxy tool generation for example with subparsers -- generating all tools and macros at once also include multiple macro files +- mkdir tmp && cp examples/macros*xml tmp/ +- PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml -m examples/macros.xml -m examples/macros_tests.xml --directory tmp +- xmldiff tmp/example_sub_foo.xml examples/example-sub/example_sub_foo.xml +- planemo lint --skip tests --report_level all --fail_level error --xsd tmp/example_sub_foo.xml +- xmldiff tmp/example_sub_foo.xml examples/example-sub/example_sub_foo.xml +- planemo lint --skip tests --report_level all --fail_level error --xsd tmp/example_sub_foo.xml +- xmldiff tmp/example_sub_baz.xml examples/example-sub/example_sub_baz.xml +- xmldiff tmp/example_sub_qux.xml examples/example-sub/example_sub_qux.xml + - PYTHONPATH=$(argparse2tool) python examples/example.py --generate_cwl_tool > tmp.cwl - PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_cwl_tool > tmp-sub.cwl diff --git a/argparse2tool/cmdline2gxml/__init__.py b/argparse2tool/cmdline2gxml/__init__.py index fe0d0b9..8f04d68 100644 --- a/argparse2tool/cmdline2gxml/__init__.py +++ b/argparse2tool/cmdline2gxml/__init__.py @@ -25,7 +25,7 @@ def process_arguments(self): self.parser.add_argument('--generate_galaxy_xml', action='store_true') self.parser.add_argument('-d', '--directory', help='Directory to store CWL tool descriptions') - self.parser.add_argument('-m', '--macro', + self.parser.add_argument('-m', '--macro', action="append", help='A global macro file to include in all tools') self.parser.add_argument('--command', action='store', default="") return vars(self.parser.parse_args()) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index 5c1a468..cdcf329 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -210,7 +210,7 @@ def _parse_args_galaxy_argp(self, argp, macro): if prog not in macros: tpe = gxt.Tool if macro: - mx.append(macro) + mx.extend(macro) else: tpe = gxt.MacrosTool diff --git a/examples/example-sub-bar.xml b/examples/example-sub-bar.xml deleted file mode 100644 index 8db4301..0000000 --- a/examples/example-sub-bar.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - example_sub_qux.xml - examples/macros.xml - - - - - $default]]> - - - - - - - - - - - - - - - - - - - - - - - diff --git a/examples/example-sub-foo.xml b/examples/example-sub-foo.xml deleted file mode 100644 index 3aea0e1..0000000 --- a/examples/example-sub-foo.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - - example_sub_baz.xml - example_sub_qux.xml - examples/macros.xml - - - - - $default]]> - - - - - - - - - - - - - - - - - - - - - - - diff --git a/examples/example-sub/example_sub_bar.xml b/examples/example-sub/example_sub_bar.xml index 095a0fe..386e986 100644 --- a/examples/example-sub/example_sub_bar.xml +++ b/examples/example-sub/example_sub_bar.xml @@ -2,11 +2,12 @@ example_sub_qux.xml + examples/macros.xml + examples/macros_tests.xml - - - - python example-sub.py bar --version + + + diff --git a/examples/example-sub/example_sub_baz.xml b/examples/example-sub/example_sub_baz.xml index 92fa962..debf8e3 100644 --- a/examples/example-sub/example_sub_baz.xml +++ b/examples/example-sub/example_sub_baz.xml @@ -1,5 +1,5 @@ - diff --git a/examples/example-sub/example_sub_foo.xml b/examples/example-sub/example_sub_foo.xml index f607d3d..46c7b71 100644 --- a/examples/example-sub/example_sub_foo.xml +++ b/examples/example-sub/example_sub_foo.xml @@ -3,11 +3,12 @@ example_sub_baz.xml example_sub_qux.xml + examples/macros.xml + examples/macros_tests.xml - - - - python example-sub.py foo --version + + + diff --git a/examples/example-sub/example_sub_qux.xml b/examples/example-sub/example_sub_qux.xml index 483250a..264e043 100644 --- a/examples/example-sub/example_sub_qux.xml +++ b/examples/example-sub/example_sub_qux.xml @@ -1,5 +1,5 @@ - + diff --git a/examples/macros.xml b/examples/macros.xml new file mode 100644 index 0000000..9bd6ed4 --- /dev/null +++ b/examples/macros.xml @@ -0,0 +1,27 @@ + + + 4.4 + 0 + + + bash + + + + + + + + + + + + + doi:10.1093/nar/gkw343 + + + + + + + diff --git a/examples/macros_tests.xml b/examples/macros_tests.xml new file mode 100644 index 0000000..ba6ea77 --- /dev/null +++ b/examples/macros_tests.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + From d97c469fecdb89e3e0be81064ae2a143ef8d6db7 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Wed, 23 Dec 2020 16:59:48 +0100 Subject: [PATCH 15/23] fixes to make output parameters work --- argparse2tool/dropins/argparse/__init__.py | 3 ++- argparse2tool/dropins/argparse/argparse_galaxy_translation.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index cdcf329..fe2f210 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -246,9 +246,10 @@ def _parse_args_galaxy_argp(self, argp, macro): if gxt_parameter is None: # TODO ERROR MESSAGE continue + print(gxt_parameter, isinstance(gxt_parameter, gxtp.InputParameter)) if not isinstance(gxt_parameter, gxtp.InputParameter): outputs.append(gxt_parameter) - if action.container in sections: + elif action.container in sections: sections[action.container].append(gxt_parameter) else: inputs.append(gxt_parameter) diff --git a/argparse2tool/dropins/argparse/argparse_galaxy_translation.py b/argparse2tool/dropins/argparse/argparse_galaxy_translation.py index 26763de..019af7c 100644 --- a/argparse2tool/dropins/argparse/argparse_galaxy_translation.py +++ b/argparse2tool/dropins/argparse/argparse_galaxy_translation.py @@ -30,7 +30,7 @@ def __gxtp_param_from_type(self, param, flag, label, num_dashes, gxparam_extra_k gxparam = gxtp.DataParam(flag, label=label, num_dashes=num_dashes, **gxparam_extra_kwargs) elif isinstance(param.type, FileType): if 'w' in param.type._mode: - gxparam = gxtp.OutputParameter( + gxparam = gxtp.OutputData( flag, format='data', default=default, label=label, num_dashes=num_dashes, **gxparam_extra_kwargs ) From f298662ce2061dadafbacb45c2d3d574f0c72f95 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Mon, 4 Jan 2021 18:19:59 +0100 Subject: [PATCH 16/23] fix typo in argument help --- argparse2tool/cmdline2gxml/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/argparse2tool/cmdline2gxml/__init__.py b/argparse2tool/cmdline2gxml/__init__.py index 8f04d68..050157a 100644 --- a/argparse2tool/cmdline2gxml/__init__.py +++ b/argparse2tool/cmdline2gxml/__init__.py @@ -24,7 +24,7 @@ def __init__(self): def process_arguments(self): self.parser.add_argument('--generate_galaxy_xml', action='store_true') self.parser.add_argument('-d', '--directory', - help='Directory to store CWL tool descriptions') + help='Directory to store tool descriptions') self.parser.add_argument('-m', '--macro', action="append", help='A global macro file to include in all tools') self.parser.add_argument('--command', action='store', default="") From 13a4166a4c1cf95bb68cfacaf40f3c615d13c29e Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Mon, 4 Jan 2021 18:21:12 +0100 Subject: [PATCH 17/23] copy macros to destination --- argparse2tool/dropins/argparse/__init__.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index fe2f210..e4392ad 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -1,4 +1,6 @@ +import os.path import re +import shutil import sys from argparse2tool import ( load_argparse, @@ -151,6 +153,12 @@ def parse_args_galaxy(self, *args, **kwargs): directory = kwargs.get('directory', None) macro = kwargs.get('macro', None) + # copy macros to destination dir + if directory and macro: + for m in macro: + mbase = os.path.basename(m) + shutil.copyfile(m, os.path.join(directory, mbase)) + # since macros can also make use of macros (i.e. the parent relation # specified in the arguments can be nester) we need to extend the # used macros such that really all are included From fe898763ccdd50be811d681a26f4e06a79f92762 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Tue, 5 Jan 2021 11:14:23 +0100 Subject: [PATCH 18/23] remove debug statements --- argparse2tool/dropins/argparse/__init__.py | 1 - argparse2tool/dropins/argparse/argparse_galaxy_translation.py | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index e4392ad..2d4f494 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -254,7 +254,6 @@ def _parse_args_galaxy_argp(self, argp, macro): if gxt_parameter is None: # TODO ERROR MESSAGE continue - print(gxt_parameter, isinstance(gxt_parameter, gxtp.InputParameter)) if not isinstance(gxt_parameter, gxtp.InputParameter): outputs.append(gxt_parameter) elif action.container in sections: diff --git a/argparse2tool/dropins/argparse/argparse_galaxy_translation.py b/argparse2tool/dropins/argparse/argparse_galaxy_translation.py index 019af7c..3278e39 100644 --- a/argparse2tool/dropins/argparse/argparse_galaxy_translation.py +++ b/argparse2tool/dropins/argparse/argparse_galaxy_translation.py @@ -119,7 +119,7 @@ def __args_from_nargs(self, param, repeat_name, repeat_var_name, positional, fla gxrepeat_cli_after = '' gxrepeat_cli_before = """\n#set %s = '" "'.join([ str($var.%s) for $var in $%s ])""" % (repeat_var_name, flag, repeat_name) else: - raise Exception("TODO: Handle argparse.REMAINDER") + raise Exception("Unknown nargs value %s" % param.nargs) return (gxrepeat_args, gxrepeat_kwargs, gxrepeat_cli_after, gxrepeat_cli_before, gxrepeat_cli_actual, gxparam_cli_before, gxparam_cli_after) @@ -148,7 +148,6 @@ def _StoreAction(self, param, tool=None): gxrepeat = None self.repeat_count += 1 gxparam_extra_kwargs = {} - if not param.required: gxparam_extra_kwargs['optional'] = True From fbf008595975fbea36d8e26002db928863d3207c Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Tue, 5 Jan 2021 11:31:05 +0100 Subject: [PATCH 19/23] travis fixes --- .travis.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5db478d..c1743ab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,7 +22,7 @@ script: - echo '' >> tmp-sub.xml - xmldiff tmp-sub.xml <(echo ""; cat examples/example-sub.xml; echo "") -# Galaxy tool generation for example with subparsers -- generating separate tools (this does not generate macro xmls automatically) +# Galaxy tool generation for example with subparsers -- generating separate tools (this does not generate macro xml files automatically) - PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml -m examples/macros.xml --command foo > tmp-sub-foo.xml - PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml -m examples/macros.xml --command bar > tmp-sub-bar.xml - xmldiff tmp-sub-foo.xml examples/example-sub/example_sub_foo.xml @@ -31,10 +31,11 @@ script: # Galaxy tool generation for example with subparsers -- generating all tools and macros at once also include multiple macro files - mkdir tmp && cp examples/macros*xml tmp/ - PYTHONPATH=$(argparse2tool) python examples/example-sub.py --generate_galaxy_xml -m examples/macros.xml -m examples/macros_tests.xml --directory tmp +- ls tmp/ - xmldiff tmp/example_sub_foo.xml examples/example-sub/example_sub_foo.xml - planemo lint --skip tests --report_level all --fail_level error --xsd tmp/example_sub_foo.xml -- xmldiff tmp/example_sub_foo.xml examples/example-sub/example_sub_foo.xml -- planemo lint --skip tests --report_level all --fail_level error --xsd tmp/example_sub_foo.xml +- xmldiff tmp/example_sub_bar.xml examples/example-sub/example_sub_bar.xml +- planemo lint --skip tests --report_level all --fail_level error --xsd tmp/example_sub_bar.xml - xmldiff tmp/example_sub_baz.xml examples/example-sub/example_sub_baz.xml - xmldiff tmp/example_sub_qux.xml examples/example-sub/example_sub_qux.xml From e69336c80a8506c3c90ecbecbb79be6881f68dd8 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Tue, 5 Jan 2021 12:15:07 +0100 Subject: [PATCH 20/23] use basename of macros in xml import if directory is specified. --- argparse2tool/dropins/argparse/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index 2d4f494..6ef16ef 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -155,9 +155,9 @@ def parse_args_galaxy(self, *args, **kwargs): # copy macros to destination dir if directory and macro: - for m in macro: - mbase = os.path.basename(m) - shutil.copyfile(m, os.path.join(directory, mbase)) + for i, m in enumerate(macro): + macro[i] = os.path.basename(m) + shutil.copyfile(m, os.path.join(directory, macro[i])) # since macros can also make use of macros (i.e. the parent relation # specified in the arguments can be nester) we need to extend the From b2660ad18d3783289b0a408f5da5de15030fce93 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Tue, 5 Jan 2021 15:47:10 +0100 Subject: [PATCH 21/23] add missing actions - print error message if parameter can not be translated - cwl: implement StoreConst, Help, and Version actions - xml: implement HelpAction --- argparse2tool/dropins/argparse/__init__.py | 8 +- .../argparse/argparse_cwl_translation.py | 11 ++ .../argparse/argparse_galaxy_translation.py | 3 + examples/example-sub.cwl | 109 +++++++++++++++++- examples/example-sub.py | 10 +- 5 files changed, 129 insertions(+), 12 deletions(-) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index 6ef16ef..b762a2d 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -129,6 +129,8 @@ def parse_args_cwl(self, *args, **kwargs): tool.inputs.append(cwlt_parameter) if isinstance(cwlt_parameter, cwlt.OutputParam): tool.outputs.append(cwlt_parameter) + else: + print("%s not implemented (%s)" % (argument_type, result.option_strings), file=sys.stderr) if argp.epilog is not None: tool.description += argp.epilog @@ -251,8 +253,7 @@ def _parse_args_galaxy_argp(self, argp, macro): if hasattr(at, argument_type): methodToCall = getattr(at, argument_type) gxt_parameter = methodToCall(action, tool=tool) - if gxt_parameter is None: - # TODO ERROR MESSAGE + if gxt_parameter is None: # e.g. for help and version actions continue if not isinstance(gxt_parameter, gxtp.InputParameter): outputs.append(gxt_parameter) @@ -261,8 +262,7 @@ def _parse_args_galaxy_argp(self, argp, macro): else: inputs.append(gxt_parameter) else: - # TODO ERROR MESSAGE - pass + print("%s not implemented (%s)" % (argument_type, result.option_strings), file=sys.stderr) for section in sections: inputs.append(sections[section]) diff --git a/argparse2tool/dropins/argparse/argparse_cwl_translation.py b/argparse2tool/dropins/argparse/argparse_cwl_translation.py index 11950f6..9fa4dbc 100644 --- a/argparse2tool/dropins/argparse/argparse_cwl_translation.py +++ b/argparse2tool/dropins/argparse/argparse_cwl_translation.py @@ -77,6 +77,12 @@ def __args_from_nargs(self, param): return param + def _VersionAction(self, param, tool=None): + pass + + def _HelpAction(self, param, tool=None): + pass + def _StoreAction(self, param): param = self.__args_from_nargs(param) cwlparam = self.__cwl_param_from_type(param) @@ -110,6 +116,11 @@ def __StoreBoolAction(self, param): cwlparam = self.__cwl_param_from_type(param) return cwlparam + def _StoreConstAction(self, param): + param.type = bool + cwlparam = self.__cwl_param_from_type(param) + return cwlparam + @staticmethod def get_cwl_type(py_type): """ diff --git a/argparse2tool/dropins/argparse/argparse_galaxy_translation.py b/argparse2tool/dropins/argparse/argparse_galaxy_translation.py index 3278e39..a07b5f5 100644 --- a/argparse2tool/dropins/argparse/argparse_galaxy_translation.py +++ b/argparse2tool/dropins/argparse/argparse_galaxy_translation.py @@ -138,6 +138,9 @@ def _VersionAction(self, param, tool=None): # Count the repeats for unique names # TODO improve + def _HelpAction(self, param, tool=None): + pass + def _StoreAction(self, param, tool=None): """ Parse argparse arguments action type of "store", the default. diff --git a/examples/example-sub.cwl b/examples/example-sub.cwl index db95e46..fcd0d41 100644 --- a/examples/example-sub.cwl +++ b/examples/example-sub.cwl @@ -1,5 +1,73 @@ #!/usr/bin/env cwl-runner -# This tool description was generated automatically by argparse2tool +# This tool description was generated automatically by argparse2tool ver. 0.4.9 +# To generate again: $ example-sub.py --generate_cwl_tool +# Help: $ example --help_arg2cwl + +cwlVersion: v1.0 + +class: CommandLineTool +baseCommand: ['example-sub.py', 'qux'] + +doc: | + None + +inputs: + + qux: + type: + - "null" + - type: array + items: string + + doc: qux help + inputBinding: + prefix: --qux + + +outputs: + [] + +#!/usr/bin/env cwl-runner +# This tool description was generated automatically by argparse2tool ver. 0.4.9 +# To generate again: $ example-sub.py --generate_cwl_tool +# Help: $ example --help_arg2cwl + +cwlVersion: v1.0 + +class: CommandLineTool +baseCommand: ['example-sub.py', 'baz'] + +doc: | + None + +inputs: + + qux: + type: + - "null" + - type: array + items: array + + doc: qux help + inputBinding: + prefix: --qux + + baz: + type: + - "null" + - type: array + items: string + + doc: baz help + inputBinding: + prefix: --baz + + +outputs: + [] + +#!/usr/bin/env cwl-runner +# This tool description was generated automatically by argparse2tool ver. 0.4.9 # To generate again: $ example-sub.py --generate_cwl_tool # Help: $ example --help_arg2cwl @@ -13,6 +81,26 @@ doc: | inputs: + qux: + type: + - "null" + - type: array + items: array + + doc: qux help + inputBinding: + prefix: --qux + + baz: + type: + - "null" + - type: array + items: array + + doc: baz help + inputBinding: + prefix: --baz + keyword: type: type: array @@ -31,6 +119,13 @@ inputs: inputBinding: position: 2 + accumulate: + type: ["null", boolean] + default: + doc: sum the integers (default - find the max) + inputBinding: + prefix: -s + foo: type: ["null", string] doc: foo help @@ -60,7 +155,7 @@ outputs: [] #!/usr/bin/env cwl-runner -# This tool description was generated automatically by argparse2tool +# This tool description was generated automatically by argparse2tool ver. 0.4.9 # To generate again: $ example-sub.py --generate_cwl_tool # Help: $ example --help_arg2cwl @@ -74,6 +169,16 @@ doc: | inputs: + qux: + type: + - "null" + - type: array + items: array + + doc: qux help + inputBinding: + prefix: --qux + false: type: ["null", boolean] default: True diff --git a/examples/example-sub.py b/examples/example-sub.py index 20a77bd..51fdd5c 100644 --- a/examples/example-sub.py +++ b/examples/example-sub.py @@ -3,15 +3,15 @@ parent = argparse.ArgumentParser(description='Process some integers.', prefix_chars='-+', epilog="here's some epilog text", formatter_class=argparse.ArgumentDefaultsHelpFormatter) - subparsers = parent.add_subparsers() parser_qux = subparsers.add_parser('qux', add_help=False) +parser_qux.add_argument('--qux', metavar='QUX', type=str, nargs=1, help='qux help') + parser_baz = subparsers.add_parser('baz', parents=[parser_qux], add_help=False) +parser_baz.add_argument('--baz', metavar='BAZ', type=str, nargs=1, help='baz help') parser_foo = subparsers.add_parser('foo', parents=[parser_baz]) -parser_bar = subparsers.add_parser('bar', parents=[parser_qux]) - parser_foo.add_argument('keyword', metavar='Q', type=str, nargs=1, help='action keyword') parser_foo.add_argument('integers', metavar='N', type=int, nargs='+', @@ -23,13 +23,11 @@ parser_foo.add_argument('--bar', nargs='*', default=[1, 2, 3], help='BAR!') parser_foo.add_argument('--true', action='store_true', help='Store a true') +parser_bar = subparsers.add_parser('bar', parents=[parser_qux]) parser_bar.add_argument('--false', action='store_false', help='Store a false') parser_bar.add_argument('--append', action='append', help='Append a value') parser_bar.add_argument('--nargs2', nargs=2, help='nargs2') parser_bar.add_argument('--mode', choices=['rock', 'paper', 'scissors'], default='scissors') parser_bar.add_argument('--version', action='version', version='2.0') -parser_baz.add_argument('--baz', metavar='BAZ', type=str, nargs=1, help='baz help') -parser_qux.add_argument('--qux', metavar='QUX', type=str, nargs=1, help='qux help') - args = parent.parse_args() From b242b25cc64814ae74c08f48e9ad825c4ecf7755 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Mon, 8 Feb 2021 12:14:19 +0100 Subject: [PATCH 22/23] just my latest changes --- argparse2tool/dropins/argparse/__init__.py | 8 +++++--- .../dropins/argparse/argparse_galaxy_translation.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index b762a2d..e7f79de 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -65,7 +65,7 @@ def __init__(self, usage=usage, description=description, epilog=epilog, - parents=parents, + parents=[], formatter_class=formatter_class, prefix_chars=prefix_chars, fromfile_prefix_chars=fromfile_prefix_chars, @@ -208,7 +208,7 @@ def _parse_args_galaxy_argp(self, argp, macro): version = self.print_version() or '1.0' except AttributeError: # handle the potential absence of print_version version = '1.0' - + print("_parse_args_galaxy_argp _subparsers %s" % argp._subparsers) prog = remove_extension(argp.prog) # tid = argp.prog.split()[-1] @@ -253,6 +253,8 @@ def _parse_args_galaxy_argp(self, argp, macro): if hasattr(at, argument_type): methodToCall = getattr(at, argument_type) gxt_parameter = methodToCall(action, tool=tool) + ## print(action, gxt_parameter) + print(action.option_strings, action.container) if gxt_parameter is None: # e.g. for help and version actions continue if not isinstance(gxt_parameter, gxtp.InputParameter): @@ -262,7 +264,7 @@ def _parse_args_galaxy_argp(self, argp, macro): else: inputs.append(gxt_parameter) else: - print("%s not implemented (%s)" % (argument_type, result.option_strings), file=sys.stderr) + print("%s not implemented (%s)" % (argument_type, action.option_strings), file=sys.stderr) for section in sections: inputs.append(sections[section]) diff --git a/argparse2tool/dropins/argparse/argparse_galaxy_translation.py b/argparse2tool/dropins/argparse/argparse_galaxy_translation.py index a07b5f5..2345108 100644 --- a/argparse2tool/dropins/argparse/argparse_galaxy_translation.py +++ b/argparse2tool/dropins/argparse/argparse_galaxy_translation.py @@ -1,7 +1,7 @@ -import galaxyxml.tool.parameters as gxtp from collections import Counter from pydoc import locate +import galaxyxml.tool.parameters as gxtp class ArgparseGalaxyTranslation(object): From 4b6cfa4fcbc95160f73ac2563f69d4367170b9d0 Mon Sep 17 00:00:00 2001 From: Matthias Bernt Date: Mon, 8 Feb 2021 19:02:40 +0100 Subject: [PATCH 23/23] differentiate parents handling for galaxy xml --- argparse2tool/dropins/argparse/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/argparse2tool/dropins/argparse/__init__.py b/argparse2tool/dropins/argparse/__init__.py index e7f79de..9f11d5e 100644 --- a/argparse2tool/dropins/argparse/__init__.py +++ b/argparse2tool/dropins/argparse/__init__.py @@ -61,11 +61,15 @@ def __init__(self, p = set([remove_extension(_.prog) for _ in parents]) macros = macros.union(p) used_macros[remove_extension(prog)] = p + + if '--generate_galaxy_xml' in sys.argv: + parents = [] + super(ArgumentParser, self).__init__(prog=prog, usage=usage, description=description, epilog=epilog, - parents=[], + parents=parents, formatter_class=formatter_class, prefix_chars=prefix_chars, fromfile_prefix_chars=fromfile_prefix_chars,