Skip to content

Commit 6e4242d

Browse files
committed
Add --format=json option to cylc broadcast command
Deprecate `--raw` in favor of `--format=raw`
1 parent c827b48 commit 6e4242d

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

changes.d/6892.feat.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `--format=json` option to `cylc broadcast` for use with the `--display` option. Deprecated the `--raw` option in favour of `--format=raw`.

cylc/flow/scripts/broadcast.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@
111111
from optparse import Values
112112

113113

114+
RAW_DEPR_MSG = (
115+
"DEPRECATED: the --raw option will be removed at Cylc 8.7; "
116+
"use --format=raw instead."
117+
)
118+
114119
REC_ITEM = re.compile(r'^\[([^\]]*)\](.*)$')
115120

116121
MUTATION = '''
@@ -312,12 +317,33 @@ def get_option_parser() -> COP:
312317
help="Use unicode box characters with -d, -k.",
313318
action="store_true", default=False, dest="unicode")
314319

320+
# BACK COMPAT: --raw
321+
# From: < 8.5.1
322+
# To: 8.5.1
323+
# Remove at: 8.7.0
315324
parser.add_option(
316325
"-r", "--raw",
317-
help="With -d/--display or -k/--display-task, write out "
318-
"the broadcast config structure in raw Python form.",
326+
help=(
327+
"With -d/--display or -k/--display-task, write out "
328+
"the broadcast config structure in raw Python form. "
329+
f"{RAW_DEPR_MSG}"
330+
),
319331
action="store_true", default=False, dest="raw")
320332

333+
parser.add_option(
334+
'--format',
335+
help=(
336+
"With -d/--display or -k/--display-task, write out "
337+
"the broadcast config structure in one of the following formats: "
338+
"tree, json, or raw (like json but as a Python dictionary). "
339+
r"Default: %default."
340+
),
341+
action='store',
342+
dest='format',
343+
choices=('tree', 'json', 'raw'),
344+
default='tree',
345+
)
346+
321347
return parser
322348

323349

@@ -356,6 +382,9 @@ async def run(options: 'Values', workflow_id):
356382

357383
}
358384

385+
if options.raw:
386+
ret['stderr'].append(cparse(f"<yellow>{RAW_DEPR_MSG}</yellow>"))
387+
359388
if options.show or options.showtask:
360389
if options.showtask:
361390
try:
@@ -369,7 +398,12 @@ async def run(options: 'Values', workflow_id):
369398
for wflow in result['workflows']:
370399
settings = wflow['broadcasts']
371400
padding = get_padding(settings) * ' '
372-
if options.raw:
401+
if options.format == 'json':
402+
import json
403+
ret['stdout'].append(
404+
json.dumps(settings, indent=2, sort_keys=True)
405+
)
406+
elif options.raw or options.format == 'raw':
373407
ret['stdout'].append(str(settings))
374408
else:
375409
ret['stdout'].extend(
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bash
2+
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
3+
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
# Test the --format option.
19+
20+
. "$(dirname "$0")/test_header"
21+
set_test_number 6
22+
23+
init_workflow "${TEST_NAME_BASE}" << '__EOF__'
24+
[scheduler]
25+
[[events]]
26+
stall timeout = PT0S
27+
abort on stall timeout = True
28+
[scheduling]
29+
[[graph]]
30+
R1 = foo
31+
[runtime]
32+
[[foo]]
33+
pre-script = """
34+
cylc broadcast "$CYLC_WORKFLOW_ID" \
35+
-p "$CYLC_TASK_CYCLE_POINT" -n "$CYLC_TASK_NAME" \
36+
--set '[environment]horse=dorothy' \
37+
--set 'post-script=echo "$horse"'
38+
"""
39+
script = """
40+
cylc broadcast "$CYLC_WORKFLOW_ID" --display --format json > out.json
41+
cylc broadcast "$CYLC_WORKFLOW_ID" --display --format raw > raw1.stdout
42+
# Test deprecated option:
43+
cylc broadcast "$CYLC_WORKFLOW_ID" --display --raw 1> raw2.stdout 2> raw2.stderr
44+
"""
45+
__EOF__
46+
47+
run_ok "${TEST_NAME_BASE}-validate" cylc validate "${WORKFLOW_NAME}"
48+
workflow_run_ok "${TEST_NAME_BASE}-run" cylc play "${WORKFLOW_NAME}" --no-detach
49+
50+
FOO_WORK_DIR="${WORKFLOW_RUN_DIR}/work/1/foo"
51+
52+
TEST_NAME="${TEST_NAME_BASE}-cmp-json"
53+
cmp_json "$TEST_NAME" "${FOO_WORK_DIR}/out.json" << '__EOF__'
54+
{
55+
"1": {
56+
"foo": {
57+
"environment": {
58+
"horse": "dorothy"
59+
},
60+
"post-script": "echo \"$horse\""
61+
}
62+
}
63+
}
64+
__EOF__
65+
66+
cmp_ok "${FOO_WORK_DIR}/raw1.stdout" << '__EOF__'
67+
{'1': {'foo': {'environment': {'horse': 'dorothy'}, 'post-script': 'echo "$horse"'}}}
68+
__EOF__
69+
70+
cmp_ok "${FOO_WORK_DIR}/raw2.stdout" "${FOO_WORK_DIR}/raw1.stdout"
71+
72+
cmp_ok "${FOO_WORK_DIR}/raw2.stderr" << __EOF__
73+
DEPRECATED: the --raw option will be removed at Cylc 8.7; use --format=raw instead.
74+
__EOF__
75+
76+
purge

0 commit comments

Comments
 (0)