Skip to content

Commit 208c4cc

Browse files
subparsers that are a parent are macros
each subparser that is a parent of another is becomes a two macros (in/out) and a token (commandline). implemented in MacrosTool
1 parent 59a8a7d commit 208c4cc

File tree

2 files changed

+100
-9
lines changed

2 files changed

+100
-9
lines changed

galaxyxml/tool/__init__.py

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
import logging
33
from lxml import etree
44
from galaxyxml import Util, GalaxyXML
5-
from galaxyxml.tool.parameters import XMLParam
5+
from galaxyxml.tool.parameters import (
6+
Import,
7+
Inputs,
8+
Macro,
9+
Macros,
10+
Outputs,
11+
XMLParam
12+
)
613

714
VALID_TOOL_TYPES = ('data_source', 'data_source_async')
815
VALID_URL_METHODS = ('get', 'post')
@@ -15,8 +22,9 @@ class Tool(GalaxyXML):
1522

1623
def __init__(self, name, id, version, description, executable, hidden=False,
1724
tool_type=None, URL_method=None, workflow_compatible=True,
18-
interpreter=None, version_command='interpreter filename.exe --version'):
19-
25+
interpreter=None, version_command='interpreter filename.exe --version',
26+
macros=None):
27+
self.id = id
2028
self.executable = executable
2129
self.interpreter = interpreter
2230
kwargs = {
@@ -53,6 +61,15 @@ def __init__(self, name, id, version, description, executable, hidden=False,
5361

5462
description_node = etree.SubElement(self.root, 'description')
5563
description_node.text = description
64+
if macros:
65+
self.macros = Macros()
66+
if isinstance(macros, list):
67+
for m in macros:
68+
self.macros.append(Import(m))
69+
else:
70+
self.macros.append(Import(macros))
71+
self.inputs = Inputs()
72+
self.outputs = Outputs()
5673

5774
def add_comment(self, comment_txt):
5875
comment = etree.Comment(comment_txt)
@@ -80,8 +97,11 @@ def clean_command_string(self, command_line):
8097
return '\n'.join(clean)
8198

8299
def export(self, keep_old_command=False): # noqa
83-
84100
export_xml = copy.deepcopy(self)
101+
try:
102+
export_xml.append(export_xml.macros)
103+
except Exception:
104+
pass
85105

86106
try:
87107
export_xml.append(export_xml.edam_operations)
@@ -108,6 +128,7 @@ def export(self, keep_old_command=False): # noqa
108128
command_line.append(export_xml.inputs.cli())
109129
except Exception as e:
110130
logger.warning(str(e))
131+
raise
111132

112133
try:
113134
command_line.append(export_xml.outputs.cli())
@@ -165,3 +186,55 @@ def export(self, keep_old_command=False): # noqa
165186
pass
166187

167188
return super(Tool, export_xml).export()
189+
190+
191+
class MacrosTool(Tool):
192+
193+
def __init__(self, *args, **kwargs):
194+
super(MacrosTool, self).__init__(*args, **kwargs)
195+
self.root = etree.Element('macros')
196+
self.inputs = Macro("%s_inmacro" % self.id)
197+
self.outputs = Macro("%s_outmacro" % self.id)
198+
199+
200+
def export(self, keep_old_command=False): # noqa
201+
202+
export_xml = copy.deepcopy(self)
203+
204+
try:
205+
for child in export_xml.macros:
206+
export_xml.append(child)
207+
except Exception:
208+
pass
209+
210+
command_line = []
211+
try:
212+
command_line.append(export_xml.inputs.cli())
213+
except Exception as e:
214+
logger.warning(str(e))
215+
216+
# Add command section
217+
command_node = etree.SubElement(export_xml.root, 'token', {"name": "%s_INMACRO" % self.id.upper()})
218+
actual_cli = "%s" % (export_xml.clean_command_string(command_line))
219+
command_node.text = etree.CDATA(actual_cli.strip())
220+
221+
command_line = []
222+
try:
223+
command_line.append(export_xml.outputs.cli())
224+
except Exception:
225+
pass
226+
command_node = etree.SubElement(export_xml.root, 'token', {"name": "%s_OUTMACRO" % self.id.upper()})
227+
actual_cli = "%s" % (export_xml.clean_command_string(command_line))
228+
command_node.text = etree.CDATA(actual_cli.strip())
229+
230+
try:
231+
export_xml.append(export_xml.inputs)
232+
except Exception:
233+
pass
234+
235+
try:
236+
export_xml.append(export_xml.outputs)
237+
except Exception:
238+
pass
239+
240+
return super(Tool, export_xml).export()

galaxyxml/tool/parameters/__init__.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ class Macros(XMLParam):
5252
name = "macros"
5353

5454
def acceptable_child(self, child):
55-
return isinstance(child, Macro)
55+
return isinstance(child, Macro) \
56+
or isinstance(child, Import)
5657

5758

5859
class Macro(XMLParam):
@@ -62,7 +63,19 @@ def __init__(self, name):
6263
params = Util.clean_kwargs(locals().copy())
6364
passed_kwargs = {}
6465
passed_kwargs['name'] = params['name']
65-
super(Expand, self).__init__(**passed_kwargs)
66+
super(Macro, self).__init__(**passed_kwargs)
67+
68+
def acceptable_child(self, child):
69+
return issubclass(type(child), XMLParam) \
70+
and not isinstance(child, Macro)
71+
72+
73+
class Import(XMLParam):
74+
name = "import"
75+
76+
def __init__(self, value):
77+
super(Import, self).__init__()
78+
self.node.text = value
6679

6780
def acceptable_child(self, child):
6881
return issubclass(type(child), XMLParam) \
@@ -81,6 +94,13 @@ def __init__(self, macro):
8194
passed_kwargs['macro'] = params['macro']
8295
super(Expand, self).__init__(**passed_kwargs)
8396

97+
def command_line(self):
98+
"""
99+
need to define empty command line contribution
100+
since Expand can be child of Inputs/Outputs
101+
"""
102+
return ""
103+
84104

85105
class ExpandIO(Expand):
86106
"""
@@ -182,7 +202,6 @@ def acceptable_child(self, child):
182202
return issubclass(type(child), EdamTopic) \
183203
or isinstance(child, Expand)
184204

185-
186205
def has_topic(self, edam_topic):
187206
"""
188207
Check the presence of a given edam_topic.
@@ -369,7 +388,6 @@ def acceptable_child(self, child):
369388
or isinstance(child, Expand)
370389

371390

372-
373391
class Repeat(InputParameter):
374392
name = 'repeat'
375393

@@ -401,7 +419,7 @@ def __init__(self, name, **kwargs):
401419
super(Conditional, self).__init__(**params)
402420

403421
def acceptable_child(self, child):
404-
return (issubclass(type(child), InputParameter) \
422+
return (issubclass(type(child), InputParameter)
405423
and not isinstance(child, Conditional)) \
406424
or isinstance(child, Expand)
407425

0 commit comments

Comments
 (0)