Debian Codegraph @

cylc/flow/scripts/view.py

raw
1 #!/usr/bin/env python3
2
3 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
4 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 """cylc view [OPTIONS] ARGS
20
21 Print a processed workflow configuration.
22
23 Print workflow configurations as processed before full parsing by Cylc. This
24 includes Jinja2 template processing, and inlining of include-files.
25 Some explanatory markup may also be requested.
26
27 Warning:
28 This command will fail if `CYLC_` template variables are referenced
29 without default values, because they are only defined for full parsing.
30 E.g. (Jinja2): `{{CYLC_WORKFLOW_ID | default("not defined")}}`.
31
32 See also `cylc config`, which displays the fully parsed configuration.
33
34 """
35
36 import asyncio
37 from pathlib import Path
38 from typing import TYPE_CHECKING
39
40 from cylc.flow.id_cli import parse_id_async
41 from cylc.flow.option_parsers import (
42 AGAINST_SOURCE_OPTION,
43 WORKFLOW_ID_OR_PATH_ARG_DOC,
44 CylcOptionParser as COP,
45 )
46 from cylc.flow.pathutil import get_workflow_run_dir
47 from cylc.flow.parsec.fileparse import read_and_proc
48 from cylc.flow.templatevars import get_template_vars
49 from cylc.flow.terminal import cli_function
50
51 if TYPE_CHECKING:
52 from optparse import Values
53
54
55 def get_option_parser():
56 parser = COP(
57 __doc__,
58 jset=True,
59 argdoc=[WORKFLOW_ID_OR_PATH_ARG_DOC],
60 )
61
62 parser.add_option(
63 "--inline", "-i", help="Inline include-files.", action="store_true",
64 default=False, dest="inline")
65
66 parser.add_option(
67 "--jinja2", "-j",
68 help="View after Jinja2 template processing "
69 "(implies '-i/--inline' as well).",
70 action="store_true", default=False, dest="jinja2")
71
72 parser.add_option(
73 "-p", "--process",
74 help="View after all processing (Jinja2, inlining, "
75 "line-continuation joining).",
76 action="store_true", default=False, dest="process")
77
78 parser.add_option(
79 "--mark", "-m",
80 help="(With '-i') Mark inclusions in the left margin.",
81 action="store_true", default=False, dest="mark")
82
83 parser.add_option(
84 "--label", "-l",
85 help="(With '-i') Label file inclusions with the file name. Line "
86 "numbers will not correspond to those reported by the parser.",
87 action="store_true", default=False, dest="label")
88
89 parser.add_option(
90 "--single",
91 help="(With '-i') Inline only the first instances of any "
92 "multiply-included files. Line numbers will not correspond to "
93 "those reported by the parser.",
94 action="store_true", default=False, dest="single")
95
96 parser.add_option(
97 "--cat", "-c",
98 help="Concatenate continuation lines (line numbers will "
99 "not correspond to those reported by the parser).",
100 action="store_true", default=False, dest="cat")
101
102 parser.add_option(
103 *AGAINST_SOURCE_OPTION.args, **AGAINST_SOURCE_OPTION.kwargs)
104
105 parser.add_cylc_rose_options()
106
107 return parser
108
109
110 @cli_function(get_option_parser)
111 def main(parser: COP, options: 'Values', workflow_id: str) -> None:
112 asyncio.run(_main(options, workflow_id))
113
114
115 async def _main(options: 'Values', workflow_id: str) -> None:
116 workflow_id, _, flow_file = await parse_id_async(
117 workflow_id,
118 src=True,
119 constraint='workflows',
120 )
121
122 # Save the location of the existing workflow run dir in the
123 # against source option:
124 if options.against_source:
125 options.against_source = Path(get_workflow_run_dir(workflow_id))
126
127 # read in the flow.cylc file
128 for line in read_and_proc(
129 flow_file,
130 get_template_vars(options),
131 viewcfg={
132 'mark': options.mark,
133 'single': options.single,
134 'label': options.label,
135 'jinja2': options.jinja2 or options.process,
136 'contin': options.cat or options.process,
137 'inline': options.jinja2 or options.inline or options.process,
138 },
139 opts=options,
140 ):
141 print(line)