| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
"""Utility for writing Cylc Flow configuration files. |
| 17 |
|
| 18 |
These utilities are not intended for direct use by tests |
| 19 |
(hence the underscore function names). |
| 20 |
Use the fixtures provided in the conftest instead. |
| 21 |
|
| 22 |
""" |
| 23 |
|
| 24 |
from textwrap import dedent |
| 25 |
from typing import List |
| 26 |
|
| 27 |
|
| 28 |
def _write_header(name: str, level: int) -> str: |
| 29 |
"""Write a cylc section definition.""" |
| 30 |
indent = ' ' * (level - 1) |
| 31 |
return f'{indent}{"[" * level}{name}{"]" * level}' |
| 32 |
|
| 33 |
|
| 34 |
def _write_setting(key: str, value: str, level: int) -> List[str]: |
| 35 |
"""Write a cylc setting definition.""" |
| 36 |
indent = ' ' * (level - 1) |
| 37 |
value = str(value) |
| 38 |
if '\n' in value: |
| 39 |
value = dedent(value).strip() |
| 40 |
ret = [f'{indent}{key} = """'] |
| 41 |
if 'script' in key: |
| 42 |
ret.extend(value.splitlines()) |
| 43 |
else: |
| 44 |
ret.extend([ |
| 45 |
f'{indent} {line}' |
| 46 |
for line in value.splitlines() |
| 47 |
]) |
| 48 |
ret += [f'{indent}"""'] |
| 49 |
else: |
| 50 |
ret = [f'{" " * (level - 1)}{key} = {value}'] |
| 51 |
return ret |
| 52 |
|
| 53 |
|
| 54 |
def _write_section(name: str, section: dict, level: int) -> List[str]: |
| 55 |
"""Write an entire cylc section including headings and settings.""" |
| 56 |
ret = [] |
| 57 |
ret.append(_write_header(name, level)) |
| 58 |
ret.extend(_write_conf(section, level)) |
| 59 |
return ret |
| 60 |
|
| 61 |
|
| 62 |
def _write_conf(conf: dict, level: int) -> List[str]: |
| 63 |
ret = [] |
| 64 |
for key, value in conf.items(): |
| 65 |
# write out settings first |
| 66 |
if key.lower() == '#!jinja2': |
| 67 |
ret.append('#!jinja2') |
| 68 |
elif not isinstance(value, dict): |
| 69 |
ret.extend( |
| 70 |
_write_setting(key, value, level + 1) |
| 71 |
) |
| 72 |
for key, value in conf.items(): |
| 73 |
# then sections after |
| 74 |
if isinstance(value, dict): |
| 75 |
ret.extend( |
| 76 |
_write_section(key, value, level + 1) |
| 77 |
) |
| 78 |
return ret |
| 79 |
|
| 80 |
|
| 81 |
def flow_config_str(conf: dict) -> str: |
| 82 |
"""Convert a configuration dictionary into cylc/parsec format. |
| 83 |
|
| 84 |
Args: |
| 85 |
conf (dict): |
| 86 |
A [nested] dictionary of configurations. |
| 87 |
|
| 88 |
Returns: |
| 89 |
str - Multiline string in cylc/parsec format. |
| 90 |
|
| 91 |
""" |
| 92 |
return '\n'.join(_write_conf(conf, 0)) + '\n' |