Skip to content

Commit fe3c32a

Browse files
committed
Refactor Tool._populate for reuse of hook checks.
1 parent ce083ca commit fe3c32a

3 files changed

Lines changed: 68 additions & 56 deletions

File tree

lib/galaxy/tools/__init__.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,17 +1972,21 @@ def _populate(
19721972
simple_errors=False,
19731973
input_format=input_format,
19741974
)
1975-
# If the tool provides a `validate_input` hook, call it.
1976-
validate_input = self.get_hook("validate_input")
1977-
if validate_input:
1978-
# hooks are so terrible ... this is specifically for https://github.com/galaxyproject/tools-devteam/blob/main/tool_collections/gops/basecoverage/operation_filter.py
1979-
legacy_non_dce_params = {
1980-
k: v.hda if isinstance(v, model.DatasetCollectionElement) and v.hda else v
1981-
for k, v in params.items()
1982-
}
1983-
validate_input(request_context, errors, legacy_non_dce_params, self.inputs)
1975+
self._handle_validate_input_hook(request_context, params, errors)
19841976
return params, errors
19851977

1978+
def _handle_validate_input_hook(
1979+
request_context, params: ToolStateJobInstancePopulatedT, errors: ParameterValidationErrorsT
1980+
):
1981+
# If the tool provides a `validate_input` hook, call it.
1982+
validate_input = self.get_hook("validate_input")
1983+
if validate_input:
1984+
# hooks are so terrible ... this is specifically for https://github.com/galaxyproject/tools-devteam/blob/main/tool_collections/gops/basecoverage/operation_filter.py
1985+
legacy_non_dce_params = {
1986+
k: v.hda if isinstance(v, model.DatasetCollectionElement) and v.hda else v for k, v in params.items()
1987+
}
1988+
validate_input(request_context, errors, legacy_non_dce_params, self.inputs)
1989+
19861990
def completed_jobs(
19871991
self, trans, use_cached_job: bool, all_params: List[ToolStateJobInstancePopulatedT]
19881992
) -> Dict[int, Optional[model.Job]]:

lib/galaxy/tools/parameters/__init__.py

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -389,53 +389,6 @@ def populate_state(
389389
):
390390
"""
391391
Populates nested state dict from incoming parameter values.
392-
>>> from galaxy.util import XML
393-
>>> from galaxy.util.bunch import Bunch
394-
>>> from galaxy.tools.parameters.basic import TextToolParameter, BooleanToolParameter
395-
>>> from galaxy.tools.parameters.grouping import Repeat
396-
>>> trans = Bunch(workflow_building_mode=False)
397-
>>> a = TextToolParameter(None, XML('<param name="a"/>'))
398-
>>> b = Repeat('b')
399-
>>> b.min = 0
400-
>>> b.max = 1
401-
>>> c = TextToolParameter(None, XML('<param name="c"/>'))
402-
>>> d = Repeat('d')
403-
>>> d.min = 0
404-
>>> d.max = 1
405-
>>> e = TextToolParameter(None, XML('<param name="e"/>'))
406-
>>> f = Conditional('f')
407-
>>> g = BooleanToolParameter(None, XML('<param name="g"/>'))
408-
>>> h = TextToolParameter(None, XML('<param name="h"/>'))
409-
>>> i = TextToolParameter(None, XML('<param name="i"/>'))
410-
>>> b.inputs = dict([('c', c), ('d', d)])
411-
>>> d.inputs = dict([('e', e), ('f', f)])
412-
>>> f.test_param = g
413-
>>> f.cases = [Bunch(value='true', inputs= { 'h': h }), Bunch(value='false', inputs= { 'i': i })]
414-
>>> inputs = dict([('a',a),('b',b)])
415-
>>> flat = dict([('a', 1), ('b_0|c', 2), ('b_0|d_0|e', 3), ('b_0|d_0|f|h', 4), ('b_0|d_0|f|g', True)])
416-
>>> state = {}
417-
>>> populate_state(trans, inputs, flat, state, check=False)
418-
>>> print(state['a'])
419-
1
420-
>>> print(state['b'][0]['c'])
421-
2
422-
>>> print(state['b'][0]['d'][0]['e'])
423-
3
424-
>>> print(state['b'][0]['d'][0]['f']['h'])
425-
4
426-
>>> # now test with input_format='21.01'
427-
>>> nested = {'a': 1, 'b': [{'c': 2, 'd': [{'e': 3, 'f': {'h': 4, 'g': True}}]}]}
428-
>>> state_new = {}
429-
>>> populate_state(trans, inputs, nested, state_new, check=False, input_format='21.01')
430-
>>> print(state_new['a'])
431-
1
432-
>>> print(state_new['b'][0]['c'])
433-
2
434-
>>> print(state_new['b'][0]['d'][0]['e'])
435-
3
436-
>>> print(state_new['b'][0]['d'][0]['f']['h'])
437-
4
438-
439392
"""
440393
if errors is None:
441394
errors = {}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from galaxy.util import XML
2+
from galaxy.util.bunch import Bunch
3+
from galaxy.app_unittest_utils.galaxy_mock import MockTrans
4+
from galaxy.app_unittest_utils.tools_support import UsesTools
5+
from galaxy.tools.parameters import populate_state
6+
from galaxy.tools.parameters.basic import (
7+
BooleanToolParameter,
8+
TextToolParameter,
9+
)
10+
from galaxy.tools.parameters.grouping import (
11+
Conditional,
12+
Repeat,
13+
)
14+
from galaxy.tool_util.unittest_utils import functional_test_tool_path
15+
from galaxy.util.unittest import TestCase
16+
17+
18+
trans = Bunch(workflow_building_mode=False)
19+
20+
21+
def test_populate_state():
22+
23+
a = TextToolParameter(None, XML('<param name="a"/>'))
24+
b = Repeat('b')
25+
b.min = 0
26+
b.max = 1
27+
c = TextToolParameter(None, XML('<param name="c"/>'))
28+
d = Repeat('d')
29+
d.min = 0
30+
d.max = 1
31+
e = TextToolParameter(None, XML('<param name="e"/>'))
32+
f = Conditional('f')
33+
g = BooleanToolParameter(None, XML('<param name="g"/>'))
34+
h = TextToolParameter(None, XML('<param name="h"/>'))
35+
i = TextToolParameter(None, XML('<param name="i"/>'))
36+
b.inputs = dict([('c', c), ('d', d)])
37+
d.inputs = dict([('e', e), ('f', f)])
38+
f.test_param = g
39+
f.cases = [Bunch(value='true', inputs= { 'h': h }), Bunch(value='false', inputs= { 'i': i })]
40+
inputs = dict([('a',a),('b',b)])
41+
flat = dict([('a', 1), ('b_0|c', 2), ('b_0|d_0|e', 3), ('b_0|d_0|f|h', 4), ('b_0|d_0|f|g', True)])
42+
state = {}
43+
populate_state(trans, inputs, flat, state, check=False)
44+
assert state['a'] == 1
45+
assert state['b'][0]['c'] == 2
46+
assert state['b'][0]['d'][0]['e'] == 3
47+
assert state['b'][0]['d'][0]['f']['h'] == 4
48+
# now test with input_format='21.01'
49+
nested = {'a': 1, 'b': [{'c': 2, 'd': [{'e': 3, 'f': {'h': 4, 'g': True}}]}]}
50+
state_new = {}
51+
populate_state(trans, inputs, nested, state_new, check=False, input_format='21.01')
52+
assert state_new['a'] == 1
53+
assert state_new['b'][0]['c'] == 2
54+
assert state_new['b'][0]['d'][0]['e'] == 3
55+
assert state_new['b'][0]['d'][0]['f']['h'] == 4

0 commit comments

Comments
 (0)