Skip to content

Commit

Permalink
Merge pull request #1 from wxtim/fix.template_section_name_for_define…
Browse files Browse the repository at this point in the history
…s_with_refactor

alt
  • Loading branch information
wxtim authored Sep 21, 2023
2 parents 8c121a7 + a7b1e28 commit 37ca72b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
7 changes: 3 additions & 4 deletions cylc/rose/stem.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
)

from cylc.rose.entry_points import get_rose_vars
from cylc.rose.utilities import _identify_templating_section

import metomi.rose.config
from metomi.rose.fs_util import FileSystemUtil
Expand Down Expand Up @@ -452,10 +453,8 @@ def process(self):
if i == 0:
template_type = get_rose_vars(
Path(url) / "rose-stem")["templating_detected"]
if template_type in ['jinja2', 'empy']:
self.template_section = f'[{template_type}:suite.rc]'
else:
self.template_section = f'[{template_type}]'
self.template_section = _identify_templating_section(
template_type, with_brackets=True)

# Versions of variables with hostname prepended for working copies
url_host = self._prepend_localhost(url)
Expand Down
22 changes: 17 additions & 5 deletions cylc/rose/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from pathlib import Path
import re
import shlex
from typing import TYPE_CHECKING, Union
from typing import TYPE_CHECKING, List, Optional, Union

from cylc.flow.hostuserutil import get_host
from cylc.flow import LOG
Expand Down Expand Up @@ -192,13 +192,25 @@ def identify_templating_section(config_node):
"You should not define more than one templating section. "
f"You defined:\n\t{'; '.join(defined_sections)}"
)
elif 'jinja2:suite.rc' in defined_sections:
templating = 'jinja2:suite.rc'
elif 'empy:suite.rc' in defined_sections:
templating = 'empy:suite.rc'
else:
return id_templating_section(list(defined_sections)[0:1])


def id_templating_section(
sections: Optional[str, None] = None,
with_brackets: Optional[bool] = False
) -> str:
"""Return a full template section string."""
templating = None
if section and 'jinja2' in section:
templating = 'jinja2:suite.rc'
elif section and 'empy' in section:
templating = 'empy:suite.rc'

if not templating:
templating = 'template variables'

templating = f'[{templating}]' if with_brackets else templating
return templating


Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_config_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
deprecation_warnings,
dump_rose_log,
identify_templating_section,
_identify_templating_section,
MultipleTemplatingEnginesError
)

Expand Down Expand Up @@ -252,6 +253,20 @@ def test_identify_templating_section(node_, expect, raises):
identify_templating_section(node)


@pytest.mark.parametrize(
'input_, expect',
(
([None], 'template variables'),
(['jinja2'], 'jinja2:suite.rc'),
([None, True], '[template variables]'),
(['jinja2', True], '[jinja2:suite.rc]'),
)
)
def test_identify_templating_section(input_, expect):
assert id_templating_section(*input_) == expect



@pytest.fixture
def node_with_ROSE_ORIG_HOST():
def _inner(comment=''):
Expand Down

0 comments on commit 37ca72b

Please sign in to comment.