Skip to content

Commit

Permalink
allowed multiparameter parents
Browse files Browse the repository at this point in the history
  • Loading branch information
wxtim committed May 15, 2023
1 parent 5b03e57 commit 95ad6d9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 15 deletions.
80 changes: 68 additions & 12 deletions cylc/flow/param_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,64 @@ def _expand_name(self, results, tmpl, params, spec_vals=None):
spec_vals[params[0][0]] = param_val
self._expand_name(results, tmpl, params[1:], spec_vals)

@staticmethod
def _parse_parent_string(parent):
"""Takes a parent string and returns a list of parameters and a
template string.
Examples:
>>> this = NameExpander._parse_parent_string
# Parent doesn't contain a parameter:
>>> this('foo')
([], 'foo')
# Parent contains a simple single parameter:
>>> this('<foo>')
(['foo'], '{foo}')
# Parent contains 2 parameters in 1 <>:
>>> this('something<foo, bar>other')
(['foo', 'bar'], 'something{foo}{bar}other')
# Parent contains 2 parameters in 2 <>:
>>> this('something<foo>middlebit<bar>other')
(['foo', 'bar'], 'something{foo}middlebit{bar}other')
# Parent contains 2 parameters, once with an = sign in it.
>>> this('something<foo=42>middlebit<bar>other')
(['foo=42', 'bar'], 'something{foo}middlebit{bar}other')
# Parent contains 2 parameters in 2 <>:
>>> this('something<foo,bar=99>other')
(['foo', 'bar=99'], 'something{foo}{bar}other')
"""
params = re.findall(r'<.*?>', parent)
p_list = []
tmpl = parent
for param in params:
param_template = param[1:-1]

if ',' in param_template:
# parameter syntax `<foo, bar>`
sub_params = [
i.strip('<> ') for i in param_template.split(',')]
for sub_param in sub_params:
p_list.append(sub_param)
if '=' in sub_param:
sub_params.remove(sub_param)
sub_params.append(sub_param.split('=')[0])
replacement = '{' + '}{'.join(sub_params) + '}'
else:
# parameter syntax: `<foo><bar>`
p_list.append(param_template)
if '=' in param_template:
param_template = param_template.split('=')[0]
replacement = '{' + param_template + '}'
tmpl = tmpl.replace(param, replacement)

return p_list, tmpl

def expand_parent_params(self, parent, param_values, origin):
"""Replace parameters with specific values in inherited parent names.
Expand All @@ -214,11 +272,13 @@ def expand_parent_params(self, parent, param_values, origin):
then it must be a legal value for that parameter.
"""
head, p_list_str, tail = REC_P_ALL.match(parent).groups()
if not p_list_str:
return (None, head)
p_list, tmpl = self._parse_parent_string(parent)

if not p_list:
return (None, parent)

used = {}
for item in (i.strip() for i in p_list_str.split(',')):
for item in p_list:
if '-' in item or '+' in item:
raise ParamExpandError(
"parameter offsets illegal here: '%s'" % origin)
Expand All @@ -244,14 +304,10 @@ def expand_parent_params(self, parent, param_values, origin):
raise ParamExpandError(
"parameter '%s' undefined in '%s'" % (
item, origin))
if head:
tmpl = head
else:
tmpl = ''
for pname in used:
tmpl += self.param_tmpl_cfg[pname]
if tail:
tmpl += tail

# For each parameter substitute the param_tmpl_cfg.
tmpl = tmpl.format(**self.param_tmpl_cfg)
# Insert parameter values into template.
return (used, tmpl % used)


Expand Down
5 changes: 2 additions & 3 deletions tests/unit/test_param_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,11 @@ def get(self):
id='two-valid-param'
).get(),
myParam(
expect=({'bar': 1, 'baz': 42}, 'foo_bar1_baz42'),
raw_str='foo<bar><baz>',
expect=({'bar': 1, 'baz': 42}, 'foo_bar1qux_baz42'),
raw_str='foo<bar>qux<baz>',
parameter_values={'bar': 1, 'baz': 42},
templates={'bar': '_bar%(bar)s', 'baz': '_baz%(baz)s'},
id='two-valid-param-sep-brackets',
xfail=True,
).get(),
myParam(
raw_str='foo<bar-1>baz',
Expand Down

0 comments on commit 95ad6d9

Please sign in to comment.