Skip to content

Commit f87fcf0

Browse files
committed
Tidy up
1 parent e808e73 commit f87fcf0

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

cylc/flow/parsec/config.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import re
2020
import sys
2121
from textwrap import dedent
22-
from typing import TYPE_CHECKING, Callable, Iterable, List, Optional
22+
from typing import TYPE_CHECKING, Callable, Iterable, List, Optional, TextIO
2323

2424
from cylc.flow.context_node import ContextNode
2525
from cylc.flow.parsec.exceptions import (
@@ -183,32 +183,36 @@ def idump(self, items=None, sparse=False, prefix='',
183183
if null:
184184
mkeys = [[]]
185185
if json:
186-
self.jdump(mkeys, sparse, prefix, oneline, none_str, handle=handle)
186+
self.jdump(mkeys, sparse, oneline, none_str, handle=handle)
187187
else:
188188
self.mdump(mkeys, sparse, prefix, oneline, none_str, handle=handle)
189189

190190
def jdump(
191191
self,
192-
mkeys=None,
193-
sparse=False,
194-
prefix='',
195-
oneline=False,
196-
none_str='',
197-
handle=None
198-
):
192+
mkeys: Optional[Iterable] = None,
193+
sparse: bool = False,
194+
oneline: bool = False,
195+
none_str: Optional[str] = None,
196+
handle: Optional[TextIO] = None
197+
) -> None:
199198
"""Dump a config to JSON format.
199+
200+
Args:
201+
mkeys: Items to display.
202+
sparse: Only display user set items, not defaults.
203+
oneline: Output on a single line.
204+
none_str: Value to give instead of null.
205+
handle: Where to write the output.
200206
"""
201-
# When we call json.dumps we use indent to
202-
# control whether output is multi-line:
203-
indent = None
204-
if not oneline:
205-
indent = 4
207+
# Use json indent to control online output:
208+
indent = None if oneline else 4
206209

207-
for keys in mkeys:
210+
for keys in mkeys or []:
208211
if not keys:
209212
keys = []
210213
cfg = self.get(keys, sparse)
211-
cfg.repl_val(cfg, None, none_str)
214+
if none_str:
215+
cfg.repl_val(cfg, None, none_str)
212216
data = json.dumps(cfg, indent=indent)
213217

214218
print(data, file=handle or sys.stdout)

tests/functional/cylc-config/11-json-dump.t

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,26 @@
2020
# is from Standard library json.
2121
. "$(dirname "$0")/test_header"
2222
#-------------------------------------------------------------------------------
23-
set_test_number 6
23+
set_test_number 9
2424
#-------------------------------------------------------------------------------
2525

26+
# Test that option parser errors if incompat options given:
27+
cylc config --json --platforms 2> err
28+
named_grep_ok "${TEST_NAME_BASE}.CLI-one-incompat-item" \
29+
"--json incompatible with --platforms" \
30+
err
31+
32+
cylc config --json --platforms --platform-names 2> err
33+
named_grep_ok "${TEST_NAME_BASE}.CLI-two-incompat-items" \
34+
"--json incompatible with --platform-names or --platforms" \
35+
err
36+
37+
cylc config --json --platforms --platform-names --print-hierarchy 2> err
38+
named_grep_ok "${TEST_NAME_BASE}.CLI-three-incompat-items" \
39+
"--json incompatible with --print-hierarchy or --platform-names or --platforms" \
40+
err
41+
42+
2643
# Test the global.cylc
2744
TEST_NAME="${TEST_NAME_BASE}-global"
2845

0 commit comments

Comments
 (0)