Skip to content

Commit

Permalink
Add CYLC_ variables to Jinja2 globals.
Browse files Browse the repository at this point in the history
  • Loading branch information
hjoliver committed Jun 19, 2023
1 parent 231f0ea commit 546cc73
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
8 changes: 7 additions & 1 deletion cylc/flow/parsec/jinja2support.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,17 @@ def jinja2environment(dir_=None):
envnsp[fname] = getattr(module, fname)

# Import WORKFLOW HOST USER ENVIRONMENT into template:
# (usage e.g.: {{environ['HOME']}}).
# (Usage e.g.: {{environ['HOME']}}).
env.globals['environ'] = os.environ

env.globals['raise'] = raise_helper
env.globals['assert'] = assert_helper

# And all `CYLC_` variables (this will include CYLC_WORKFLOW environment
# variables exported by `cylc` commands before config file parsing.
for key, val in os.environ.items():
if key.startswith('CYLC_'):
env.globals[key] = val
return env


Expand Down
41 changes: 25 additions & 16 deletions cylc/flow/scripts/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@

"""cylc view [OPTIONS] ARGS
Print a processed workflow configuration.
Print workflow configurations as processed before full parsing by Cylc. This
includes Jinja2 or Empy template processing, and inlining of include-files.
Some explanatory markup may also be requested.
Warning: this command will fail if Jinja2 `CYLC_` variables are referenced
without default values, because they are only defined during full parsing.
E.g., `{{CYLC_WORKFLOW_NAME | default("not defined")}}`.
See also `cylc config`, which displays the fully parsed configuration.
Note:
This is different to `cylc config` which displays the parsed
configuration (as Cylc would see it).
"""

import asyncio
Expand Down Expand Up @@ -116,20 +121,24 @@ async def _main(options: 'Values', workflow_id: str) -> None:
)

# read in the flow.cylc file
viewcfg = {
'mark': options.mark,
'single': options.single,
'label': options.label,
'empy': options.empy or options.process,
'jinja2': options.jinja2 or options.process,
'contin': options.cat or options.process,
'inline': (options.inline or options.jinja2 or options.empy
or options.process),
}
for line in read_and_proc(
flow_file,
load_template_vars(options.templatevars, options.templatevars_file),
viewcfg=viewcfg,
load_template_vars(
options.templatevars,
options.templatevars_file
),
viewcfg={
'mark': options.mark,
'single': options.single,
'label': options.label,
'empy': options.empy or options.process,
'jinja2': options.jinja2 or options.process,
'contin': options.cat or options.process,
'inline': (
options.jinja2 or options.empy or
options.inline or options.process
),
},
opts=options,
):
print(line)

0 comments on commit 546cc73

Please sign in to comment.