Skip to content

Commit b67cb4f

Browse files
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, ...)
1 parent a60c290 commit b67cb4f

File tree

2 files changed

+87
-21
lines changed

2 files changed

+87
-21
lines changed

argparse2tool/cmdline2gxml/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ def __init__(self):
2323

2424
def process_arguments(self):
2525
self.parser.add_argument('--generate_galaxy_xml', action='store_true')
26+
self.parser.add_argument('-d', '--directory',
27+
help='Directory to store CWL tool descriptions')
28+
self.parser.add_argument('-m', '--macro',
29+
help='A global macro file to include in all tools')
2630
return vars(self.parser.parse_args())

argparse2tool/dropins/argparse/__init__.py

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
setattr(__selfmodule__, x, getattr(ap, x))
2222

2323
tools = []
24+
# set of prog names where the argparser actually should be represented
25+
# as a macro, i.e. XYZ will be in the set if there is a
26+
# ArgumentParser(..., parents=[XYZ], ...)
27+
macros = set()
28+
29+
# mapping tools to list of required macros (which are actually identical to the
30+
# parents parameter passed ArgumentParser. but I could not find out how this is
31+
# stored in the created ArgumentParser objects
32+
used_macros = dict()
2433

2534

2635
class ArgumentParser(ap.ArgumentParser):
@@ -37,10 +46,16 @@ def __init__(self,
3746
argument_default=None,
3847
conflict_handler='error',
3948
add_help=True):
49+
global macros
50+
global used_macros
4051

4152
self.argument_list = []
4253
self.argument_names = []
4354
tools.append(self)
55+
if len(parents) > 0:
56+
p = set([_.prog.split()[-1] for _ in parents])
57+
macros = macros.union(p)
58+
used_macros[prog.split()[-1]] = p
4459
super(ArgumentParser, self).__init__(prog=prog,
4560
usage=usage,
4661
description=description,
@@ -128,6 +143,25 @@ def parse_args_cwl(self, *args, **kwargs):
128143
sys.exit(0)
129144

130145
def parse_args_galaxy(self, *args, **kwargs):
146+
global used_macros
147+
148+
directory = kwargs.get('directory', None)
149+
macro = kwargs.get('macro', None)
150+
151+
# since macros can also make use of macros (i.e. the parent relation
152+
# specified in the arguments can be nester) we need to extend the
153+
# used macros such that really all are included
154+
ext_used_macros = dict()
155+
for tool, macros in used_macros.items():
156+
ext_used_macros[tool] = set()
157+
q = list(macros)
158+
while len(q) > 0:
159+
m = q.pop()
160+
if m in used_macros:
161+
q.extend(used_macros[m])
162+
ext_used_macros[tool].add(m)
163+
used_macros = ext_used_macros
164+
131165
for argp in tools:
132166
# make subparser description out of its help message
133167
if argp._subparsers:
@@ -139,28 +173,53 @@ def parse_args_galaxy(self, *args, **kwargs):
139173
subparser.choices[choice_action.dest].description = choice_action.help
140174
else:
141175
if kwargs.get('command', argp.prog) in argp.prog:
142-
data = self._parse_args_galaxy_argp(argp)
143-
print(data)
176+
data = self._parse_args_galaxy_argp(argp, macro)
177+
if directory:
178+
if directory[-1] != '/':
179+
directory += '/'
180+
filename = argp.prog.split()[-1] + ".xml"
181+
filename = directory + filename
182+
with open(filename, 'a') as f:
183+
f.write(data)
184+
else:
185+
print(data)
144186
else:
145187
continue
146188
sys.exit(0)
147189

148-
def _parse_args_galaxy_argp(self, argp):
190+
def _parse_args_galaxy_argp(self, argp, macro):
191+
global macros
192+
global used_macros
193+
149194
try:
150195
version = self.print_version() or '1.0'
151196
except AttributeError: # handle the potential absence of print_version
152197
version = '1.0'
153-
tool = gxt.Tool(
154-
argp.prog,
155-
argp.prog.replace(" ", "_"),
156-
version,
157-
argp.description,
158-
"python "+argp.prog,
159-
interpreter=None,
160-
version_command='python %s --version' % argp.prog)
161-
162-
inputs = gxtp.Inputs()
163-
outputs = gxtp.Outputs()
198+
199+
tid = argp.prog.split()[-1]
200+
201+
# get the list of file names of the used macros
202+
mx = used_macros.get(tid, [])
203+
mx = ["%s.xml" % _ for _ in mx]
204+
205+
if tid not in macros:
206+
tpe = gxt.Tool
207+
if macro:
208+
mx.append(macro)
209+
else:
210+
tpe = gxt.MacrosTool
211+
212+
tool = tpe(argp.prog,
213+
argp.prog.replace(" ", "_"),
214+
version,
215+
argp.description,
216+
"python "+argp.prog,
217+
interpreter=None,
218+
version_command='python %s --version' % argp.prog,
219+
macros=mx)
220+
221+
inputs = tool.inputs
222+
outputs = tool.outputs
164223

165224
at = agt.ArgparseGalaxyTranslation()
166225
# Only build up arguments if the user actually requests it
@@ -177,16 +236,19 @@ def _parse_args_galaxy_argp(self, argp):
177236
else:
178237
outputs.append(gxt_parameter)
179238

180-
# TODO: replace with argparse-esque library to do this.
181-
stdout = gxtp.OutputData('default', 'txt')
182-
stdout.command_line_override = '> $default'
183-
outputs.append(stdout)
239+
if tid in used_macros:
240+
for m in used_macros[tid]:
241+
inputs.append(gxtp.ExpandIO(m + "_inmacro"))
242+
outputs.append(gxtp.ExpandIO(m + "_outmacro"))
243+
244+
if tid not in macros:
245+
# TODO: replace with argparse-esque library to do this.
246+
stdout = gxtp.OutputData('default', 'txt')
247+
stdout.command_line_override = '> $default'
248+
outputs.append(stdout)
184249

185-
tool.inputs = inputs
186-
tool.outputs = outputs
187250
if argp.epilog is not None:
188251
tool.help = argp.epilog
189252
else:
190253
tool.help = "TODO: Write help"
191-
192254
return tool.export()

0 commit comments

Comments
 (0)