From c2bf17edd507099954aa6d2d8d7f257d381336c6 Mon Sep 17 00:00:00 2001 From: Tim Pillinger <26465611+wxtim@users.noreply.github.com> Date: Tue, 12 Sep 2023 14:37:22 +0100 Subject: [PATCH 1/5] alt --- cylc/rose/stem.py | 7 +++---- cylc/rose/utilities.py | 35 +++++++++++++++++++++++++++++----- tests/unit/test_config_node.py | 15 +++++++++++++++ 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/cylc/rose/stem.py b/cylc/rose/stem.py index c391701f..fd156f02 100644 --- a/cylc/rose/stem.py +++ b/cylc/rose/stem.py @@ -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 @@ -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) diff --git a/cylc/rose/utilities.py b/cylc/rose/utilities.py index ed90511d..b9d2ad14 100644 --- a/cylc/rose/utilities.py +++ b/cylc/rose/utilities.py @@ -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 @@ -192,13 +192,38 @@ 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 _identify_templating_section(defined_sections) + + +def _identify_templating_section( + sections: Union[str, List, None] = None, + with_brackets: Optional[bool] = False +) -> str: + """Return a full template section string. + + Args: + sections: Can be + * A list of sections + * A section name + * None + """ + templating = None + if sections: + if isinstance(sections, set): + section = sections.pop() if sections else [] + else: + section = sections + + 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 diff --git a/tests/unit/test_config_node.py b/tests/unit/test_config_node.py index 86bf8232..fc8d891c 100644 --- a/tests/unit/test_config_node.py +++ b/tests/unit/test_config_node.py @@ -32,6 +32,7 @@ deprecation_warnings, dump_rose_log, identify_templating_section, + _identify_templating_section, MultipleTemplatingEnginesError ) @@ -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 _identify_templating_section(*input_) == expect + + + @pytest.fixture def node_with_ROSE_ORIG_HOST(): def _inner(comment=''): From 418db8202648a19050f01951a765fe410da60dfd Mon Sep 17 00:00:00 2001 From: Tim Pillinger <26465611+wxtim@users.noreply.github.com> Date: Thu, 21 Sep 2023 10:14:04 +0100 Subject: [PATCH 2/5] Update cylc/rose/utilities.py Co-authored-by: Oliver Sanders --- cylc/rose/utilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cylc/rose/utilities.py b/cylc/rose/utilities.py index b9d2ad14..0fa718e5 100644 --- a/cylc/rose/utilities.py +++ b/cylc/rose/utilities.py @@ -193,7 +193,7 @@ def identify_templating_section(config_node): f"You defined:\n\t{'; '.join(defined_sections)}" ) else: - return _identify_templating_section(defined_sections) + return _identify_templating_section(list(defined_sections)[0:1]) def _identify_templating_section( From 1654eea36cad41ccfdc455aec8a24a9b0b39675f Mon Sep 17 00:00:00 2001 From: Tim Pillinger <26465611+wxtim@users.noreply.github.com> Date: Thu, 21 Sep 2023 10:14:13 +0100 Subject: [PATCH 3/5] Update cylc/rose/utilities.py Co-authored-by: Oliver Sanders --- cylc/rose/utilities.py | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/cylc/rose/utilities.py b/cylc/rose/utilities.py index 0fa718e5..dbf168c8 100644 --- a/cylc/rose/utilities.py +++ b/cylc/rose/utilities.py @@ -197,24 +197,11 @@ def identify_templating_section(config_node): def _identify_templating_section( - sections: Union[str, List, None] = None, + sections: Optional[str, None] = None, with_brackets: Optional[bool] = False ) -> str: - """Return a full template section string. - - Args: - sections: Can be - * A list of sections - * A section name - * None - """ + """Return a full template section string.""" templating = None - if sections: - if isinstance(sections, set): - section = sections.pop() if sections else [] - else: - section = sections - if section and 'jinja2' in section: templating = 'jinja2:suite.rc' elif section and 'empy' in section: From baf202ec28b7db0fac9eaecbeff97b8f06003d4f Mon Sep 17 00:00:00 2001 From: Tim Pillinger <26465611+wxtim@users.noreply.github.com> Date: Thu, 21 Sep 2023 10:15:50 +0100 Subject: [PATCH 4/5] Update cylc/rose/utilities.py --- cylc/rose/utilities.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cylc/rose/utilities.py b/cylc/rose/utilities.py index dbf168c8..919d798c 100644 --- a/cylc/rose/utilities.py +++ b/cylc/rose/utilities.py @@ -193,10 +193,10 @@ def identify_templating_section(config_node): f"You defined:\n\t{'; '.join(defined_sections)}" ) else: - return _identify_templating_section(list(defined_sections)[0:1]) + return id_templating_section(list(defined_sections)[0:1]) -def _identify_templating_section( +def id_templating_section( sections: Optional[str, None] = None, with_brackets: Optional[bool] = False ) -> str: From a7b1e284b108aecd793416440082f102e5477ce7 Mon Sep 17 00:00:00 2001 From: Tim Pillinger <26465611+wxtim@users.noreply.github.com> Date: Thu, 21 Sep 2023 10:16:35 +0100 Subject: [PATCH 5/5] Update tests/unit/test_config_node.py --- tests/unit/test_config_node.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_config_node.py b/tests/unit/test_config_node.py index fc8d891c..10cbf2eb 100644 --- a/tests/unit/test_config_node.py +++ b/tests/unit/test_config_node.py @@ -262,8 +262,8 @@ def test_identify_templating_section(node_, expect, raises): (['jinja2', True], '[jinja2:suite.rc]'), ) ) -def test__identify_templating_section(input_, expect): - assert _identify_templating_section(*input_) == expect +def test_identify_templating_section(input_, expect): + assert id_templating_section(*input_) == expect