mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
Allow parameterization of XML macros.
This comes in two modes - simple lists of required attributes and per-attribute parameters that may specify defaults.
The following toy example:
```
<tool>
<expand macro="inputs" bar="hello" />
<macros>
<xml name="inputs" dollar_tokens="bar">
<inputs type="the type is $BAR$" />
</xml>
</macros>
</tool>
```
will evalute to ``<tool><inputs type="the type is hello" /><tool>``. The XML macro definition tag can now take in two new fixed attributes dollar_tokens and at_tokens - which will be interpreted as a list of parameter names. Each parameter can be specified when expanding the macros as simple attributes specifying the value of the parameter. The "dollar" parameter with name bar would map to a token with name $BAR$. Likewise an "at" parameter with name foo would map to a token with name @FOO@.
Both parameter types can be specified on a per-parameter basis to also specify default values. So the following example:
```
<tool>
<expand macro="inputs" foo="hello" />
<expand macro="inputs" foo="world" />
<expand macro="inputs" />
<macros>
<xml name="inputs" at_token_foo="the_default">
<inputs>@FOO@</inputs>
</xml>
</macros>
</tool>
```
Would evalute to ``<tool><inputs>hello</inputs><inputs>world</inputs><inputs>the_default</inputs>``.
Updated unit tests explain the above much more succinctly - though some like English and words for reasons that escape me.
This commit is contained in:
@@ -3,6 +3,8 @@ from xml.etree import ElementTree, ElementInclude
|
||||
from copy import deepcopy
|
||||
import os
|
||||
|
||||
REQUIRED_PARAMETER = object()
|
||||
|
||||
|
||||
def load_tool(path):
|
||||
"""
|
||||
@@ -17,7 +19,7 @@ def load_tool(path):
|
||||
tokens = _macros_of_type(root, 'token', lambda el: el.text)
|
||||
|
||||
# Expand xml macros
|
||||
macro_dict = _macros_of_type(root, 'xml', lambda el: list(el))
|
||||
macro_dict = _macros_of_type(root, 'xml', lambda el: XmlMacroDef(el))
|
||||
_expand_macros([root], macro_dict, tokens)
|
||||
|
||||
return tree
|
||||
@@ -116,19 +118,24 @@ def _expand_macros(elements, macros, tokens):
|
||||
|
||||
def _expand_macro(element, expand_el, macros, tokens):
|
||||
macro_name = expand_el.get('macro')
|
||||
macro_def = deepcopy(macros[macro_name])
|
||||
macro_def = macros[macro_name]
|
||||
expanded_elements = deepcopy(macro_def.elements)
|
||||
|
||||
_expand_yield_statements(macro_def, expand_el)
|
||||
_expand_yield_statements(expanded_elements, expand_el)
|
||||
|
||||
# Recursively expand contained macros.
|
||||
_expand_macros(macro_def, macros, tokens)
|
||||
_expand_macros(expanded_elements, macros, tokens)
|
||||
macro_tokens = macro_def.macro_tokens(expand_el)
|
||||
if macro_tokens:
|
||||
print macro_tokens
|
||||
_expand_tokens(expanded_elements, macro_tokens)
|
||||
|
||||
# HACK for elementtree, newer implementations (etree/lxml) won't
|
||||
# require this parent_map data structure but elementtree does not
|
||||
# track parents or recongnize .find('..').
|
||||
# TODO fix this now that we're not using elementtree
|
||||
parent_map = dict((c, p) for p in element.getiterator() for c in p)
|
||||
_xml_replace(expand_el, macro_def, parent_map)
|
||||
_xml_replace(expand_el, expanded_elements, parent_map)
|
||||
|
||||
|
||||
def _expand_yield_statements(macro_def, expand_el):
|
||||
@@ -233,6 +240,34 @@ def _xml_replace(query, targets, parent_map):
|
||||
parent_el.remove(query)
|
||||
|
||||
|
||||
class XmlMacroDef(object):
|
||||
|
||||
def __init__(self, el):
|
||||
self.elements = list(el)
|
||||
parameters = {}
|
||||
for key, value in el.attrib.items():
|
||||
for char, char_descript in [("@", "at"), ("$", "dollar")]:
|
||||
if key == "%s_tokens" % char_descript:
|
||||
for at_token in value.split(","):
|
||||
parameter_name = at_token
|
||||
parameters[parameter_name] = (char, REQUIRED_PARAMETER)
|
||||
elif key.startswith("%s_token_" % char_descript):
|
||||
parameter_name = key[len("%s_token_" % char_descript):]
|
||||
parameters[parameter_name] = (char, value)
|
||||
self.parameters = parameters
|
||||
|
||||
def macro_tokens(self, expand_el):
|
||||
tokens = {}
|
||||
for key, (wrap_char, default_val) in self.parameters.items():
|
||||
token_value = expand_el.attrib.get(key, default_val)
|
||||
if token_value is REQUIRED_PARAMETER:
|
||||
message = "Failed to expand macro - missing required parameter [%s]."
|
||||
raise ValueError(message % key)
|
||||
token_name = "%s%s%s" % (wrap_char, key.upper(), wrap_char)
|
||||
tokens[token_name] = token_value
|
||||
return tokens
|
||||
|
||||
|
||||
def _parse_xml(fname):
|
||||
tree = ElementTree.parse(fname)
|
||||
root = tree.getroot()
|
||||
|
||||
@@ -189,3 +189,41 @@ def test_loader():
|
||||
tag_el = xml.find("another").find("tag")
|
||||
value = tag_el.get('value')
|
||||
assert value == "The value.", value
|
||||
|
||||
# Test macros XML macros with $ expansions in attributes
|
||||
with TestToolDirectory() as tool_dir:
|
||||
tool_dir.write('''
|
||||
<tool>
|
||||
<expand macro="inputs" bar="hello" />
|
||||
<macros>
|
||||
<xml name="inputs" dollar_tokens="bar">
|
||||
<inputs type="the type is $BAR$" />
|
||||
</xml>
|
||||
</macros>
|
||||
</tool>
|
||||
''')
|
||||
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('''
|
||||
<tool>
|
||||
<expand macro="inputs" foo="hello" />
|
||||
<expand macro="inputs" foo="world" />
|
||||
<expand macro="inputs" />
|
||||
<macros>
|
||||
<xml name="inputs" at_token_foo="the_default">
|
||||
<inputs>@FOO@</inputs>
|
||||
</xml>
|
||||
</macros>
|
||||
</tool>
|
||||
''')
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user