Skip to content

Commit b7a1188

Browse files
add example and test for MacrosTool
- plus docs - also return at least some TODO in the CLI generated for output collections
1 parent 2ee9f78 commit b7a1188

File tree

5 files changed

+132
-6
lines changed

5 files changed

+132
-6
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ script:
99
- flake8 galaxyxml --ignore=E2,E3,E4,E5,W3,W505,C901,E501
1010
- python examples/example.py > tmp.xml
1111
- xmldiff tmp.xml examples/tool.xml
12+
- python examples/example_macros.py > tmp.xml
13+
- xmldiff tmp.xml examples/example_macros.xml
1214
- nosetests --with-coverage --cover-package=galaxyxml
1315
deploy:
1416
provider: pypi

examples/example_macros.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env python
2+
import galaxyxml.tool as gxt
3+
import galaxyxml.tool.parameters as gxtp
4+
5+
# examplify the use of MacrosTool
6+
#
7+
8+
tool = gxt.MacrosTool(
9+
name="aragorn",
10+
id="aragorn",
11+
version="1.2.36",
12+
description="Aragorn is a tRNA finder",
13+
executable="aragorn.exe",
14+
version_command="aragorn.exe --version",
15+
)
16+
17+
inputs = tool.inputs
18+
outputs = tool.outputs
19+
20+
# Add requirements
21+
requirements = gxtp.Requirements()
22+
requirements.append(gxtp.Requirement("package", "samtools", version="1.0.0"))
23+
requirements.append(gxtp.Container("docker", "one_super_image"))
24+
tool.requirements = requirements
25+
26+
# A parameter
27+
param = gxtp.BooleanParam("flag", label="Flag label", help="Flag help", num_dashes=1)
28+
# Yes I know this is rubbish. Please make a PR!!
29+
param.space_between_arg = " "
30+
inputs.append(param)
31+
32+
33+
# A float in a section
34+
section = gxtp.Section("float_section", "Float section")
35+
param = gxtp.FloatParam("float", label="Float label", help="Float help", value=0, num_dashes=1)
36+
param.space_between_arg = " "
37+
section.append(param)
38+
inputs.append(section)
39+
40+
# A conditional
41+
param = gxtp.Conditional("cond", label="Conditional")
42+
param.append(gxtp.SelectParam("Select", options={"hi": "1", "bye": "2"}))
43+
when_a = gxtp.When(value="hi")
44+
when_b = gxtp.When(value="bye")
45+
when_b.append(gxtp.IntegerParam("some_int", value=0, num_dashes=1, label="Advanced value"))
46+
param.append(when_a)
47+
param.append(when_b)
48+
inputs.append(param)
49+
50+
# Integer parameters
51+
param_min = gxtp.IntegerParam("int_min", label="int_min label", help="int_min help", value=0, num_dashes=1)
52+
param_max = gxtp.IntegerParam("int_max", label="int_max label", help="int_max help", value=0, num_dashes=1)
53+
54+
posint = gxtp.IntegerParam("posint", label="posint label", positional=True, help="posinthelp", value=0, num_dashes=2)
55+
56+
param_min.command_line_override = "-i$int_min,$int_max"
57+
param_max.command_line_override = ""
58+
param_min.space_between_arg = " "
59+
param_max.space_between_arg = " "
60+
inputs.append(param_min)
61+
inputs.append(param_max)
62+
inputs.append(posint)
63+
64+
# Add Select with options from_file with columns and filter
65+
param = gxtp.SelectParam("select_local")
66+
options = gxtp.Options(from_file="loc_file.loc")
67+
column_a = gxtp.Column("name", 0)
68+
options.append(column_a)
69+
column_b = gxtp.Column("value", 1)
70+
options.append(column_b)
71+
filter_a = gxtp.Filter("sort_by", name="sorted", column="1")
72+
options.append(filter_a)
73+
param.append(options)
74+
inputs.append(param)
75+
76+
# Configfiles
77+
configfiles = gxtp.Configfiles()
78+
configfiles.append(gxtp.Configfile(name="testing", text="Hello <> World"))
79+
configfiles.append(gxtp.ConfigfileDefaultInputs(name="inputs"))
80+
81+
# Outputs
82+
param = gxtp.OutputData("output", format="tabular", num_dashes=1)
83+
param.space_between_arg = " "
84+
outputs.append(param)
85+
# Collection
86+
collection = gxtp.OutputCollection("supercollection", label="a small label")
87+
discover = gxtp.DiscoverDatasets("(?P&lt;designation&gt;.+)\.pdf.fasta", format="fasta")
88+
collection.append(discover)
89+
outputs.append(collection)
90+
91+
tool.inputs = inputs
92+
tool.outputs = outputs
93+
tool.help = "HI"
94+
tool.configfiles = configfiles
95+
96+
# Add Tests sections
97+
tool.tests = gxtp.Tests()
98+
test_a = gxtp.Test()
99+
param = gxtp.TestParam("float", value=5.4)
100+
test_a.append(param)
101+
test_out = gxtp.TestOutput(name="output", value="file.out")
102+
test_a.append(test_out)
103+
tool.tests.append(test_a)
104+
105+
106+
# Add comment to the wrapper
107+
tool.add_comment("This tool descriptor has been generated using galaxyxml.")
108+
109+
print(tool.export())

examples/example_macros.xml

Whitespace-only changes.

galaxyxml/tool/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,22 @@ def export(self, keep_old_command=False):
212212

213213

214214
class MacrosTool(Tool):
215+
"""
216+
creates a <macros> tag containing macros and tokens
217+
for the inputs and outputs:
215218
219+
for the inputs
220+
221+
- a macro `<xml name="ID_inmacro">` containing all the inputs
222+
- a token `<token name="ID_INMACRO">` containing the CLI for the inputs
223+
224+
where ID is the id used in initialization.
225+
226+
analogously for the outputs `ID_outmacro` and `ID_OUTMACRO`
227+
are created.
228+
229+
TODO all other elements, like requirements are currently ignored
230+
"""
216231
def __init__(self, *args, **kwargs):
217232
super(MacrosTool, self).__init__(*args, **kwargs)
218233
self.root = etree.Element('macros')
@@ -235,6 +250,7 @@ def export(self, keep_old_command=False): # noqa
235250
command_line.append(export_xml.inputs.cli())
236251
except Exception as e:
237252
logger.warning(str(e))
253+
raise
238254

239255
# Add command section
240256
command_node = etree.SubElement(export_xml.root, 'token', {"name": "%s_INMACRO" % self.id.upper()})

galaxyxml/tool/parameters/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ class Macros(XMLParam):
7777
name = "macros"
7878

7979
def acceptable_child(self, child):
80-
return isinstance(child, Macro) \
81-
or isinstance(child, Import)
80+
return isinstance(child, (Macro, Import))
8281

8382

8483
class Macro(XMLParam):
@@ -91,8 +90,7 @@ def __init__(self, name):
9190
super(Macro, self).__init__(**passed_kwargs)
9291

9392
def acceptable_child(self, child):
94-
return issubclass(type(child), XMLParam) \
95-
and not isinstance(child, Macro)
93+
return issubclass(type(child), XMLParam) and not isinstance(child, Macro)
9694

9795

9896
class Import(XMLParam):
@@ -103,8 +101,7 @@ def __init__(self, value):
103101
self.node.text = value
104102

105103
def acceptable_child(self, child):
106-
return issubclass(type(child), XMLParam) \
107-
and not isinstance(child, Macro)
104+
return issubclass(type(child), XMLParam) and not isinstance(child, Macro)
108105

109106

110107
class Expand(XMLParam):
@@ -789,6 +786,8 @@ def __init__(
789786
def acceptable_child(self, child):
790787
return isinstance(child, OutputData) or isinstance(child, OutputFilter) or isinstance(child, DiscoverDatasets)
791788

789+
def command_line(self):
790+
return "## TODO CLI for OutputCollection %s" % self.name
792791

793792
class DiscoverDatasets(XMLParam):
794793
name = "discover_datasets"

0 commit comments

Comments
 (0)