-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-factored to put the rose-changes in a separate file
- Loading branch information
Showing
6 changed files
with
183 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# THIS FILE IS PART OF THE CYLC SUITE ENGINE. | ||
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
"""Cylc support for reading and interpreting ``rose-suite.conf`` workflow | ||
configuration files. | ||
""" | ||
|
||
import os | ||
import shlex | ||
|
||
from pathlib import Path | ||
|
||
# from cylc.flow import LOG | ||
|
||
|
||
def get_rose_vars(dir_=None, opts=None): | ||
"""Load Jinja2 Vars from rose-suite.conf in dir_ | ||
Args: | ||
dir_(string or Pathlib.path object): | ||
Search for a ``rose-suite.conf`` file in this location. | ||
opts: | ||
Some sort of options object or string - To be used to allow CLI | ||
specification of optional configuaration. | ||
Returns: | ||
If no ``rose-suite.conf`` file found then None. | ||
Else return a dictionary of evaluated key value pairs from | ||
the ``rose-suite.conf[jinja2:suite.rc]`` section. | ||
TODO: | ||
- Once the CLI for the ``rose suite-run`` replacement command is | ||
ready plumb in the the equivelent of | ||
``rose suite-run --opt-conf-key=""``. | ||
- Condsider allowing ``[jinja2:flow.conf]`` as an alias for | ||
consistency with cylc. | ||
""" | ||
# Return None if dir_ does not exist | ||
if dir_ is None: | ||
return None | ||
|
||
# Return None if rose-suite.conf do not exist. | ||
if isinstance(dir_, str): | ||
dir_ = Path(dir_) | ||
top_level_file = dir_ / 'rose-suite.conf' | ||
if not top_level_file.is_file(): | ||
return None | ||
|
||
from metomi.rose.config_tree import ConfigTreeLoader | ||
|
||
opt_conf_keys = [] | ||
# get optional config key set as environment variable: | ||
opt_conf_keys_env = os.getenv("ROSE_SUITE_OPT_CONF_KEYS") | ||
if opt_conf_keys_env: | ||
opt_conf_keys += shlex.split(opt_conf_keys_env) | ||
# ... or as command line options | ||
if 'opt_conf_keys' in dir(opts) and opts.opt_conf_keys: | ||
opt_conf_keys += opts.opt_conf_keys | ||
|
||
# Optional definitions | ||
redefinitions = [] | ||
if 'defines' in dir(opts) and opts.defines: | ||
redefinitions = opts.defines | ||
|
||
# Load the actual config tree | ||
config_tree = ConfigTreeLoader().load( | ||
str(dir_), | ||
'rose-suite.conf', | ||
opt_keys=opt_conf_keys, | ||
defines=redefinitions | ||
) | ||
|
||
# Get the jinja2 section of the config | ||
config = config_tree.node.value['jinja2:suite.rc'] | ||
# Walk through the jinja2 section getting key=value pairs. | ||
config = dict([(item[0][1], item[1].value) for item in config.walk()]) | ||
return config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# THIS FILE IS PART OF THE CYLC SUITE ENGINE. | ||
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import os | ||
|
||
from cylc.flow.parsec.rose_utils import get_rose_vars | ||
|
||
|
||
def test_get_rosedirs(tmp_path): | ||
"""Function returns dict of [jinja2:suite.rc] items | ||
Also check that function does _not_ return [env] section items. | ||
Creates: | ||
. | ||
`--tmp_path | ||
|-- rose-suite.conf | ||
`-- opt | ||
|-- rose-suite-gravy.conf | ||
`-- rose-suite-chips.conf | ||
Scenarios tested: | ||
- Read in a basic rose-suite.conf file. Ensure we don't return env, | ||
just jinja2. | ||
- Get optional config name from an environment variable. | ||
- Get optional config name from command line option. | ||
- Get optional config name from an explicit over-ride string. | ||
""" | ||
with open(tmp_path / 'rose-suite.conf', 'w+') as testfh: | ||
# The [env] section is there to make sure I don't load it with | ||
# the jinja2 method. | ||
testfh.write( | ||
"[env]\n" | ||
"TIMS_ENV_VAR=Jelly\n" | ||
"[jinja2:suite.rc]\n" | ||
"TIMS_JINJA2_ENV=64\n" | ||
"Another_Jinja2_var=IceCream\n" | ||
) | ||
|
||
opt_dir = tmp_path / 'opt' | ||
opt_dir.mkdir() | ||
with open(opt_dir / 'rose-suite-gravy.conf', 'w+') as testfh: | ||
testfh.write( | ||
"[jinja2:suite.rc]\n" | ||
"TIMS_JINJA2_ENV=42\n" | ||
"Another_Jinja2_var=Peas\n" | ||
) | ||
|
||
with open(opt_dir / 'rose-suite-chips.conf', 'w+') as testfh: | ||
testfh.write( | ||
"[jinja2:suite.rc]\n" | ||
"TIMS_JINJA2_ENV=99\n" | ||
"Another_Jinja2_var=Chips\n" | ||
) | ||
|
||
assert get_rose_vars(tmp_path) == { | ||
'Another_Jinja2_var': 'IceCream', | ||
'TIMS_JINJA2_ENV': '64' | ||
} | ||
|
||
os.environ['ROSE_SUITE_OPT_CONF_KEYS'] = "gravy" | ||
assert get_rose_vars(tmp_path) == { | ||
'Another_Jinja2_var': 'Peas', | ||
'TIMS_JINJA2_ENV': '42' | ||
} | ||
|
||
from types import SimpleNamespace | ||
options = SimpleNamespace() | ||
options.opt_conf_keys = ["chips"] | ||
assert get_rose_vars(tmp_path, options) == { | ||
'Another_Jinja2_var': 'Chips', | ||
'TIMS_JINJA2_ENV': '99' | ||
} | ||
|
||
options.defines = ["[jinja2:suite.rc]TIMS_JINJA2_ENV=Curry_Sauce"] | ||
assert get_rose_vars(tmp_path, options) == { | ||
'Another_Jinja2_var': 'Chips', | ||
'TIMS_JINJA2_ENV': 'Curry_Sauce' | ||
} |