import os
from shutil import rmtree
from string import Template
from tempfile import mkdtemp
from galaxy.tools.loader import load_tool, template_macro_params
from galaxy.util import parse_xml
SIMPLE_TOOL_WITH_MACRO = """external.xml"""
SIMPLE_MACRO = Template("""
$tool_version
""")
def test_loader():
class TestToolDirectory(object):
def __init__(self):
self.temp_directory = mkdtemp()
def __enter__(self):
return self
def __exit__(self, type, value, tb):
rmtree(self.temp_directory)
def write(self, contents, name="tool.xml"):
open(os.path.join(self.temp_directory, name), "w").write(contents)
def load(self, name="tool.xml", preprocess=True):
path = os.path.join(self.temp_directory, name)
if preprocess:
return load_tool(path)
else:
return parse_xml(path)
# Test simple macro replacement.
with TestToolDirectory() as tool_dir:
tool_dir.write('''
''')
xml = tool_dir.load(preprocess=False)
assert xml.find("inputs") is None
xml = tool_dir.load(preprocess=True)
assert xml.find("inputs") is not None
# Test importing macros from external files
with TestToolDirectory() as tool_dir:
tool_dir.write(SIMPLE_TOOL_WITH_MACRO)
tool_dir.write(SIMPLE_MACRO.substitute(tool_version="2.0"), name="external.xml")
xml = tool_dir.load(preprocess=False)
assert xml.find("inputs") is None
xml = tool_dir.load(preprocess=True)
assert xml.find("inputs") is not None
# Test macros with unnamed yield statements.
with TestToolDirectory() as tool_dir:
tool_dir.write('''
''')
xml = tool_dir.load()
assert xml.find("inputs").find("input").get("name") == "first_input"
# Test recursive macro applications.
with TestToolDirectory() as tool_dir:
tool_dir.write('''
''')
xml = tool_dir.load()
assert xml.find("inputs").findall("input")[0].get("name") == "first_input"
assert xml.find("inputs").findall("input")[1].get("name") == "second_input"
assert xml.find("inputs").findall("input")[2].get("name") == "third_input"
# Test recursive macro applications.
with TestToolDirectory() as tool_dir:
tool_dir.write('''
''')
xml = tool_dir.load()
assert xml.find("inputs").findall("input")[0].get("name") == "first_input"
assert xml.find("inputs").findall("input")[1].get("name") == "second_input"
assert xml.find("inputs").findall("input")[2].get("name") == "third_input"
with TestToolDirectory() as tool_dir:
tool_dir.write('''
''')
xml = tool_dir.load()
assert xml.find("inputs").findall("param")[0].get("name") == "a1"
assert xml.find("inputs").findall("param")[1].get("name") == "b"
# Test is shortcut for macro type="xml"
with TestToolDirectory() as tool_dir:
tool_dir.write('''
''')
xml = tool_dir.load()
assert xml.find("inputs") is not None
with TestToolDirectory() as tool_dir:
tool_dir.write('''
tool_wrapper.py
#include source=$tool_params
-a 1 -b 2
''')
xml = tool_dir.load()
params_dict = template_macro_params(xml.getroot())
assert params_dict['tool_params'] == "-a 1 -b 2"
with TestToolDirectory() as tool_dir:
tool_dir.write('''
The citation.@CITATION@
''')
xml = tool_dir.load()
help_el = xml.find("help")
assert help_el.text == "The citation.", help_el.text
with TestToolDirectory() as tool_dir:
tool_dir.write('''
The value.
''')
xml = tool_dir.load()
tag_el = xml.find("another").find("tag")
value = tag_el.get('value')
assert value == "The value.", value
with TestToolDirectory() as tool_dir:
tool_dir.write('''
''')
xml = tool_dir.load()
tag_el = xml.find("another").find("tag")
value = tag_el.get('value')
assert value == "", value
# Test macros XML macros with $$ expansions in attributes
with TestToolDirectory() as tool_dir:
tool_dir.write('''
''')
xml = tool_dir.load()
input_els = xml.findall("inputs")
assert len(input_els) == 1
assert input_els[0].attrib["type"] == "the type is hello"
# Test macros XML macros with @ expansions in text
with TestToolDirectory() as tool_dir:
tool_dir.write('''
@FOO@
''')
xml = tool_dir.load()
input_els = xml.findall("inputs")
assert len(input_els) == 3
assert input_els[0].text == "hello"
assert input_els[1].text == "world"
assert input_els[2].text == "the_default"