template
The uw mode for handling Jinja2 templates.
uw template --help
usage: uw template [-h] [--version] ACTION ...
Handle templates
Optional arguments:
-h, --help
Show help and exit
--version
Show version info and exit
Positional arguments:
ACTION
render
Render a template
translate
Translate atparse to Jinja2
render
uw template render --help
usage: uw template render [-h] [--version] [--input-file PATH]
[--output-file PATH] [--values-file PATH]
[--values-format {ini,nml,sh,yaml}] [--cycle CYCLE]
[--leadtime LEADTIME] [--env]
[--search-path PATH[:PATH:...]] [--values-needed]
[--dry-run] [--quiet] [--verbose]
[KEY=VALUE ...]
Render a template
Optional arguments:
-h, --help
Show help and exit
--version
Show version info and exit
--input-file PATH, -i PATH
Path to input file (default: read from stdin)
--output-file PATH, -o PATH
Path to output file (default: write to stdout)
--values-file PATH
Path to file providing override or interpolation values
--values-format {ini,nml,sh,yaml}
Values format
--cycle CYCLE
The cycle in ISO8601 format (e.g. yyyy-mm-ddThh)
--leadtime LEADTIME
The leadtime as hours[:minutes[:seconds]]
--env
Use environment variables
--search-path PATH[:PATH:...]
Colon-separated paths to search for extra templates
--values-needed
Report values needed to render template, then exit
--dry-run
Only log info, making no changes
--quiet, -q
Print no logging messages
--verbose, -v
Print all logging messages
KEY=VALUE
A key=value pair to override/supplement config
Examples
The examples in this section use a template file template with contents:
{{ greeting }}, {{ recipient }}!
and a YAML file called values.yaml with contents:
greeting: Hello
recipient: World
To show the values needed to render the template:
uw template render --input-file template --values-needed[2025-01-02T03:04:05] INFO Value(s) needed to render this template are: [2025-01-02T03:04:05] INFO greeting [2025-01-02T03:04:05] INFO recipient
To render the template to
stdout:uw template render --input-file template --values-file values.yamlHello, World!
Shell redirection may also be used to stream output to a file, another process, etc.
To render the template to a file via command-line argument:
rm -f rendered uw template render --input-file template --values-file values.yaml --output-file rendered cat renderedHello, World!
With the
--dry-runflag specified, nothing is written tostdout(or to a file if--output-fileis specified), but a report of what would have been written is logged tostderr:uw template render --input-file template --values-file values.yaml --dry-run[2025-01-02T03:04:05] INFO Hello, World!
To read the template from
stdinand render tostdout:cat template | uw template render --values-file values.yamlHello, World!
If the values file has an unrecognized (or no) extension,
uwwill not know how to parse its contents:uw template render --input-file template --values-file values.txtHello, World!
The format must be explicitly specified (here,
values.txtis identical tovalues.yaml):uw template render --input-file template --values-file values.txt --values-format yamlHello, World!
To request verbose log output:
uw template render --input-file template --values-file values.yaml --verbose[2025-01-02T03:04:05] DEBUG Command: uw template render --input-file template --values-file values.yaml --verbose [2025-01-02T03:04:05] DEBUG Internal arguments when rendering template: [2025-01-02T03:04:05] DEBUG --------------------------------------------------------------------- [2025-01-02T03:04:05] DEBUG values_src: values.yaml [2025-01-02T03:04:05] DEBUG values_format: yaml [2025-01-02T03:04:05] DEBUG input_file: template [2025-01-02T03:04:05] DEBUG output_file: None [2025-01-02T03:04:05] DEBUG cycle: None [2025-01-02T03:04:05] DEBUG leadtime: None [2025-01-02T03:04:05] DEBUG overrides: {} [2025-01-02T03:04:05] DEBUG env: False [2025-01-02T03:04:05] DEBUG searchpath: None [2025-01-02T03:04:05] DEBUG values_needed: False [2025-01-02T03:04:05] DEBUG dry_run: False [2025-01-02T03:04:05] DEBUG --------------------------------------------------------------------- [2025-01-02T03:04:05] DEBUG Read initial template values from values.yaml Hello, World!Note that
uwlogs tostderr. Use shell redirection as needed.The optional
--cycleand--leadtimearguments may be used to inject Pythondatetimeandtimedeltaobjects, respectively, into the template for use in Jinja2 expressions. For example, the template{{ (cycle + leadtime).strftime("%Y%m%d%H%M") }}can be rendered with
uw template render --input-file template-cycle-leadtime --cycle 2025-11-12T06:00 --leadtime 6202511121200
The following examples use the YAML file
greeting.yamlwith contents:greeting: Hello
It is an error to render a template without providing all needed values.
uw template render --input-file template --values-file greeting.yaml[2025-01-02T03:04:05] ERROR Value(s) required to render template not provided: [2025-01-02T03:04:05] ERROR recipient [2025-01-02T03:04:05] ERROR Template could not be rendered
Values may also be supplemented by
key=valuecommand-line arguments:uw template render --input-file template --values-file greeting.yaml recipient=ReaderHello, Reader!
The optional
--envswitch allows environment variables to be used to supply values:recipient=You uw template render --input-file template --values-file greeting.yaml --envHello, You!
Values from
key=valuearguments override values from file, and environment variables override both:recipient=Sunshine uw template render --input-file template --values-file greeting.yaml recipient=Reader greeting="Good day" --envGood day, Sunshine!
Note that, in the previous two examples, the var=val syntax preceding the uw command is shell syntax for exporting environment variable var only for the duration of the command that follows. It should not be confused with the two key=value pairs later on the command line, which are arguments to uw.)
Jinja2 supports references to additional templates via, for example, import expressions, and
uwprovides support as follows:By default, the directory containing the primary template file is used as the search path for additional templates.
The optional
--search-pathflag overrides the default search path with any number of explicitly specified, colon-separated paths.
For example, given file
template-with-macros{% import "macros" as m -%} {{ m.double(11) }}
and file
macros(in the same directory astemplate-with-macros){% macro double(n) -%} {{ n * 2 }} {%- endmacro %}
the template is rendered as
uw template render --input-file template-with-macros22
The
--search-pathoption can also be specified with a colon-separated set of directories to be searched for templates.NB: Reading the primary template from
stdinrequires use of--search-path, as there is no implicit directory related to the input. For example, given the existence of directorymacros-dir:cat template-with-macros | uw template render --search-path macros-dir22
Non-YAML-formatted files may also be used as value sources. For example,
values.shwith contentsgreeting="Hello" recipient="World"
can be used to render
template:uw template render --input-file template --values-file values.shHello, World!
translate
uw template translate --help
usage: uw template translate [-h] [--version] [--input-file PATH]
[--output-file PATH] [--dry-run] [--quiet]
[--verbose]
Translate atparse to Jinja2
Optional arguments:
-h, --help
Show help and exit
--version
Show version info and exit
--input-file PATH, -i PATH
Path to input file (default: read from stdin)
--output-file PATH, -o PATH
Path to output file (default: write to stdout)
--dry-run
Only log info, making no changes
--quiet, -q
Print no logging messages
--verbose, -v
Print all logging messages
Examples
The examples in this section use atparse-formatted template file atparse.txt with contents:
@[greeting], @[recipient]!
To convert an atparse-formatted template file to Jinja2 format:
uw template translate --input-file atparse.txt{{ greeting }}, {{ recipient }}!Shell redirection may also be used to stream output to a file, another process, etc.
To convert the template to a file via command-line argument:
rm -f jinja2.txt uw template translate --input-file atparse.txt --output-file jinja2.txt cat jinja2.txt{{ greeting }}, {{ recipient }}!With the
--dry-runflag specified, nothing is written tostdout(or to a file if--output-fileis specified), but a report of what would have been written is logged tostderr:uw template translate --input-file atparse.txt --dry-run[2025-01-02T03:04:05] INFO {{ greeting }}, {{ recipient }}!