Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support expanding <param1><param2> in inherited family names #5537

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions cylc/flow/param_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def expand(template, params, results, values=None):

from contextlib import suppress
import re
from typing import List, Tuple

from cylc.flow.exceptions import ParamExpandError
from cylc.flow.task_id import TaskID
Expand All @@ -70,9 +71,6 @@ def expand(template, params, results, values=None):
REC_P_ALL = re.compile(r"(%s)?(?:<(.*?)>)?(.+)?" % TaskID.NAME_RE)
# To extract all parameter lists e.g. 'm,n,o' (from '<m,n,o>').
REC_P_GROUP = re.compile(r"<(.*?)>")
# As REC_P_ALL, but retaining <> so that we can tell which bits of re.split
# are templates, and which are plain text:
REC_P_MATCH = re.compile(r'(<[^>]*>)')
# To extract parameter name and optional offset or value e.g. 'm-1'.
REC_P_OFFS = re.compile(
r'(\w+)\s*([\-+]\s*\d+|=\s*%s)?' % TaskID.NAME_SUFFIX_RE)
Expand Down Expand Up @@ -205,7 +203,7 @@ def _expand_name(self, results, tmpl, params, spec_vals=None):
self._expand_name(results, tmpl, params[1:], spec_vals)

@staticmethod
def _parse_task_name_string(parent):
def _parse_task_name_string(task_str: str) -> Tuple[List[str], str]:
"""Takes a parent string and returns a list of parameters and a
template string.

Expand Down Expand Up @@ -240,34 +238,32 @@ def _parse_task_name_string(parent):
>>> this('FAM<i = cat ,j=3>')
(['i = cat', 'j=3'], 'FAM{i}{j}')
"""
tmpl_list = REC_P_MATCH.split(parent)
param_list = []
for template in tmpl_list:
group = REC_P_GROUP.findall(template)
if group:
param = group[0]
if ',' in param:
# parameter syntax `<foo, bar>`
replacement = ''
for sub_param in param.split(','):
sub_param = sub_param.strip()
param_list.append(sub_param)
if '=' in sub_param:
sub_param = sub_param.split('=')[0].strip()
replacement += '{' + sub_param + '}'

def _parse_replacement(match: re.Match) -> str:
wxtim marked this conversation as resolved.
Show resolved Hide resolved
nonlocal param_list
param = match.group(1)
if ',' in param:
# parameter syntax `<foo, bar>`
replacement = ''
for sub_param in param.split(','):
sub_param = sub_param.strip()
param_list.append(sub_param)
if '=' in sub_param:
sub_param = sub_param.split('=')[0].strip()
replacement += '{' + sub_param + '}'
else:
# parameter syntax: `<foo><bar>`
param_list.append(param)
if '=' in param:
replacement = '{' + param.split('=')[0] + '}'
else:
# parameter syntax: `<foo><bar>`
param_list.append(param)
if '=' in param:
replacement = '{' + param.split('=')[0] + '}'
else:
replacement = '{' + param + '}'
replacement = '{' + param + '}'
return replacement

# Replace param in template list with template.
if f'<{param}>' in tmpl_list:
tmpl_list[tmpl_list.index(f'<{param}>')] = replacement
replacement = REC_P_GROUP.sub(_parse_replacement, task_str)

return param_list, ''.join(tmpl_list)
return param_list, replacement

def expand_parent_params(self, parent, param_values, origin):
"""Replace parameters with specific values in inherited parent names.
Expand Down