From f9f9f399428ba4384949c615964ae3cc5a9dad94 Mon Sep 17 00:00:00 2001 From: Tim Pillinger <26465611+wxtim@users.noreply.github.com> Date: Tue, 27 Aug 2024 10:06:54 +0100 Subject: [PATCH] Response to review - Ensure --json help message is useful. - Make the null value reporting only happen to actual null values. --- cylc/flow/parsec/OrderedDict.py | 13 +++++++++++++ cylc/flow/parsec/config.py | 4 +--- cylc/flow/scripts/config.py | 3 +-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cylc/flow/parsec/OrderedDict.py b/cylc/flow/parsec/OrderedDict.py index ed5b85c20f4..8e727719b40 100644 --- a/cylc/flow/parsec/OrderedDict.py +++ b/cylc/flow/parsec/OrderedDict.py @@ -100,6 +100,19 @@ def prepend(self, key, value): self[key] = value self.move_to_end(key, last=False) + @staticmethod + def repl_val(target, replace, replacement): + """Replace dictionary values with a string. + + Designed to be used recursively. + """ + for key, val in target.items(): + if isinstance(val, dict): + OrderedDictWithDefaults.repl_val( + val, replace, replacement) + elif val == replace: + target[key] = replacement + class DictTree: """An object providing a single point of access to a tree of dicts. diff --git a/cylc/flow/parsec/config.py b/cylc/flow/parsec/config.py index 049867fd686..1e1d1ad86ce 100644 --- a/cylc/flow/parsec/config.py +++ b/cylc/flow/parsec/config.py @@ -208,11 +208,9 @@ def jdump( if not keys: keys = [] cfg = self.get(keys, sparse) + cfg.repl_val(cfg, None, none_str) data = json.dumps(cfg, indent=indent) - # We can replace null values with our none_str: - data = data.replace('null', f'"{none_str}"') - print(data, file=handle or sys.stdout) def mdump(self, mkeys=None, sparse=False, prefix='', diff --git a/cylc/flow/scripts/config.py b/cylc/flow/scripts/config.py index 67b14037daf..d5ec417301b 100755 --- a/cylc/flow/scripts/config.py +++ b/cylc/flow/scripts/config.py @@ -113,8 +113,7 @@ def get_option_parser() -> COP: parser.add_option( '--json', help=( - 'Print metadata from a Cylc configuration.' - ), + 'Returns config as JSON rather than Cylc Config format.'), default=False, action='store_true', dest='json'