Allow expression evaluation to fail, so default value can be set in expression

This commit is contained in:
mvdbeek
2024-02-12 15:48:01 +01:00
parent 648b29bf7e
commit 2eca43e175
2 changed files with 30 additions and 7 deletions
+10 -6
View File
@@ -788,16 +788,20 @@ class DynamicOptions:
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
except Exception as e:
log.warning("Fetching from url '%s' failed: %s", url, str(e))
return []
data = response.json()
data = None
if self.from_url_postprocess:
data = do_eval(
self.from_url_postprocess,
data,
)
try:
data = do_eval(
self.from_url_postprocess,
data,
)
except Exception as eval_error:
log.warning("Failed to evaluate postprocess_expression: %s", str(eval_error))
data = []
# We only support the very specific ["name", "value", "selected"] format for now.
return [to_triple(d) for d in data]
+20 -1
View File
@@ -1,7 +1,8 @@
<tool id="select_from_url" name="select_from_url" version="0.1.0" profile="23.1">
<command><![CDATA[
echo '$url_param_value' > '$param_value' &&
echo '$url_param_value_postprocessed' > '$param_value_postprocessed'
echo '$url_param_value_postprocessed' > '$param_value_postprocessed' &&
echo '$invalid_url_param_value_postprocessed' > '$invalid_param_value_postprocessed'
]]></command>
<inputs>
<param name="url_param_value" type="select">
@@ -21,15 +22,28 @@ echo '$url_param_value_postprocessed' > '$param_value_postprocessed'
]]></postprocess_expression>
</options>
</param>
<param name="invalid_url_param_value_postprocessed" type="select">
<options from_url="https://usegalaxy.or/api/genomes/dm6">
<postprocess_expression type="ecma5.1"><![CDATA[${
if (inputs) {
return Object.values(inputs.chrom_info).map((v) => [v.chrom, v.len])
} else {
return [["The fallback value", "default"]]
}
}]]></postprocess_expression>
</options>
</param>
</inputs>
<outputs>
<data format="txt" label="url param value" name="param_value"></data>
<data format="txt" label="url param value postprocessed" name="param_value_postprocessed"></data>
<data format="txt" label="invalid url param value postprocessed" name="invalid_param_value_postprocessed"></data>
</outputs>
<tests>
<test>
<param name="url_param_value" value="dm6" />
<param name="url_param_value_postprocessed" value="chr2L" />
<param name="invalid_url_param_value_postprocessed" value="default" />
<output name="param_value">
<assert_contents>
<has_text text="dm6"></has_text>
@@ -40,6 +54,11 @@ echo '$url_param_value_postprocessed' > '$param_value_postprocessed'
<has_text text="23513712"></has_text>
</assert_contents>
</output>
<output name="invalid_param_value_postprocessed">
<assert_contents>
<has_text text="default"></has_text>
</assert_contents>
</output>
</test>
</tests>
</tool>