Skip to content

Commit e6326c4

Browse files
authored
Add config compose mode/action (#804)
1 parent 51ef909 commit e6326c4

30 files changed

+642
-203
lines changed

docs/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ When the Linux diff tool just doesn't work for comparing unordered namelists wit
3737

3838
| :any:`CLI documentation with examples<cli_config_compare_examples>`
3939
40+
Compose Action
41+
""""""""""""""
42+
43+
To compose configs is to start with a base config and to update its values, structurally, from the contents of one or more other configs of the same type (YAML, Fortran namelist, etc.). Composition supports building up complex experiment configurations by, for example, combining default values appropriate to the overall application with values needed on a particular machine, and then with specific user requirements, each stored in their own config files. Such a hierarchical approach to configuration management provides flexibility and avoids repetition of common values across files.
44+
45+
| :any:`CLI documentation with examples<cli_config_compose_examples>`
46+
4047
Realize Action
4148
""""""""""""""
4249

docs/sections/user_guide/cli/tools/config.rst

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,75 @@ The examples that follow use identical namelist files ``a.nml`` and ``b.nml`` wi
8787
.. literalinclude:: config/compare-format-mismatch.out
8888
:language: text
8989

90+
.. _cli_config_compose_examples:
91+
92+
``compose``
93+
-----------
94+
95+
The ``compose`` action builds up a final config by repeatedly updating a base config with the contents of other configs of the same format.
96+
97+
.. literalinclude:: config/compose-help.cmd
98+
:language: text
99+
:emphasize-lines: 1
100+
.. literalinclude:: config/compose-help.out
101+
:language: text
102+
103+
Examples
104+
^^^^^^^^
105+
106+
* Consider three YAML configs:
107+
108+
.. literalinclude:: config/compose-base.yaml
109+
:caption: compose-base.yaml
110+
:language: yaml
111+
.. literalinclude:: config/compose-update-1.yaml
112+
:caption: compose-update-1.yaml
113+
:language: yaml
114+
.. literalinclude:: config/compose-update-2.yaml
115+
:caption: compose-update-2.yaml
116+
:language: yaml
117+
118+
Compose the three together, writing to ``stdout``:
119+
120+
.. literalinclude:: config/compose-basic.cmd
121+
:language: text
122+
:emphasize-lines: 1
123+
.. literalinclude:: config/compose-basic.out
124+
:language: yaml
125+
126+
Values provided by update configs override or augment values provided in the base config, while unaffected values survive to the final config. Priority of values increases from left to right.
127+
128+
Additionally:
129+
130+
* Sets of configs in the ``ini``, ``nml``, and ``sh`` formats can be similarly composed.
131+
* The ``--input-format`` and ``--output-format`` options can be used to specify the format of the input and output configs, respectively, for cases when ``uwtools`` cannot deduce the format of configs from their filename extensions. When the formats are neither explicitly specified or deduced, ``yaml`` is assumed.
132+
* The ``--output-file`` / ``-o`` option can be added to write the final config to a file instead of to ``stdout``.
133+
134+
* The optional ``--realize`` switch can be used to render as many Jinja2 template expressions as possible in the final config, using the config itself as a source of values. For example:
135+
136+
.. literalinclude:: config/compose-template.yaml
137+
:caption: compose-template.yaml
138+
:language: yaml
139+
.. literalinclude:: config/compose-values.yaml
140+
:caption: compose-values.yaml
141+
:language: yaml
142+
143+
Without the ``--realize`` switch:
144+
145+
.. literalinclude:: config/compose-realize-no.cmd
146+
:language: text
147+
:emphasize-lines: 1
148+
.. literalinclude:: config/compose-realize-no.out
149+
:language: yaml
150+
151+
And with ``--realize``:
152+
153+
.. literalinclude:: config/compose-realize-yes.cmd
154+
:language: text
155+
:emphasize-lines: 1
156+
.. literalinclude:: config/compose-realize-yes.out
157+
:language: yaml
158+
90159
.. _cli_config_realize_examples:
91160

92161
``realize``
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
constants:
2+
pi: 3.142
3+
color: blue
4+
flower: violet
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
uw config compose compose-base.yaml compose-update-1.yaml compose-update-2.yaml
2+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
constants:
2+
pi: 3.142
3+
e: 2.718
4+
color: blue
5+
flower: rose
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uw config compose --help
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
usage: uw config compose [-h] [--version] [--realize] [--output-file PATH]
2+
[--input-format {ini,nml,sh,yaml}]
3+
[--output-format {ini,nml,sh,yaml}] [--quiet]
4+
[--verbose]
5+
CONFIG [CONFIG ...]
6+
7+
Compose configs
8+
9+
positional arguments:
10+
CONFIG
11+
12+
Optional arguments:
13+
-h, --help
14+
Show help and exit
15+
--version
16+
Show version info and exit
17+
--realize
18+
Render template expressions where possible
19+
--output-file PATH, -o PATH
20+
Path to output file (default: write to stdout)
21+
--input-format {ini,nml,sh,yaml}
22+
Input format (default: yaml)
23+
--output-format {ini,nml,sh,yaml}
24+
Output format (default: yaml)
25+
--quiet, -q
26+
Print no logging messages
27+
--verbose, -v
28+
Print all logging messages
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uw config compose compose-template.yaml compose-values.yaml
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
radius: !float '{{ 2.0 * pi * r }}'
2+
pi: 3.142
3+
r: 1.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uw config compose compose-template.yaml compose-values.yaml --realize

0 commit comments

Comments
 (0)