Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
wxtim committed Aug 29, 2024
1 parent e808e73 commit f87fcf0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
36 changes: 20 additions & 16 deletions cylc/flow/parsec/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 19,7 @@
import re
import sys
from textwrap import dedent
from typing import TYPE_CHECKING, Callable, Iterable, List, Optional
from typing import TYPE_CHECKING, Callable, Iterable, List, Optional, TextIO

from cylc.flow.context_node import ContextNode
from cylc.flow.parsec.exceptions import (
Expand Down Expand Up @@ -183,32 183,36 @@ def idump(self, items=None, sparse=False, prefix='',
if null:
mkeys = [[]]
if json:
self.jdump(mkeys, sparse, prefix, oneline, none_str, handle=handle)
self.jdump(mkeys, sparse, oneline, none_str, handle=handle)
else:
self.mdump(mkeys, sparse, prefix, oneline, none_str, handle=handle)

def jdump(
self,
mkeys=None,
sparse=False,
prefix='',
oneline=False,
none_str='',
handle=None
):
mkeys: Optional[Iterable] = None,
sparse: bool = False,
oneline: bool = False,
none_str: Optional[str] = None,
handle: Optional[TextIO] = None
) -> None:
"""Dump a config to JSON format.
Args:
mkeys: Items to display.
sparse: Only display user set items, not defaults.
oneline: Output on a single line.
none_str: Value to give instead of null.
handle: Where to write the output.
"""
# When we call json.dumps we use indent to
# control whether output is multi-line:
indent = None
if not oneline:
indent = 4
# Use json indent to control online output:
indent = None if oneline else 4

for keys in mkeys:
for keys in mkeys or []:
if not keys:
keys = []
cfg = self.get(keys, sparse)
cfg.repl_val(cfg, None, none_str)
if none_str:
cfg.repl_val(cfg, None, none_str)
data = json.dumps(cfg, indent=indent)

print(data, file=handle or sys.stdout)
Expand Down
19 changes: 18 additions & 1 deletion tests/functional/cylc-config/11-json-dump.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 20,26 @@
# is from Standard library json.
. "$(dirname "$0")/test_header"
#-------------------------------------------------------------------------------
set_test_number 6
set_test_number 9
#-------------------------------------------------------------------------------

# Test that option parser errors if incompat options given:
cylc config --json --platforms 2> err
named_grep_ok "${TEST_NAME_BASE}.CLI-one-incompat-item" \
"--json incompatible with --platforms" \
err

cylc config --json --platforms --platform-names 2> err
named_grep_ok "${TEST_NAME_BASE}.CLI-two-incompat-items" \
"--json incompatible with --platform-names or --platforms" \
err

cylc config --json --platforms --platform-names --print-hierarchy 2> err
named_grep_ok "${TEST_NAME_BASE}.CLI-three-incompat-items" \
"--json incompatible with --print-hierarchy or --platform-names or --platforms" \
err


# Test the global.cylc
TEST_NAME="${TEST_NAME_BASE}-global"

Expand Down

0 comments on commit f87fcf0

Please sign in to comment.