From 976e2517ae538bc78d11623f4429b9364d0ced0c Mon Sep 17 00:00:00 2001 From: Rafael Folco Date: Mon, 9 Oct 2023 17:23:09 -0300 Subject: [PATCH] Do not print warnings on stdout by default Print warnings and non-critical errors on stderr only if '--verbose' is used. Ignore warnings and non-error messages if not critical, e.g. a block that was requested that does not exist, or an index from an endpoint array. --- util/blockbreaker.py | 61 ++++++++++++++++++++------------- util/tests/test-blockbreaker.py | 2 +- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/util/blockbreaker.py b/util/blockbreaker.py index 93d5a43f..059b5ec2 100755 --- a/util/blockbreaker.py +++ b/util/blockbreaker.py @@ -38,6 +38,10 @@ def process_options(): default = None, type = str) + parser.add_argument("--verbose", + action = "store_true", + help = "Increase output verbosity") + args = parser.parse_args() return args @@ -46,6 +50,8 @@ def dump_json(obj, key, idx, format = 'readable'): # Parseable format has no indentation indentation = None sep = ':' + err_msg = None + warn_msg = None if format == 'readable': indentation = 4 sep += ' ' @@ -59,54 +65,59 @@ def dump_json(obj, key, idx, format = 'readable'): json_str = json.dumps(blk, indent = indentation, separators = (',', sep), sort_keys = False) return json_str - except IndexError as err: - err_msg = f"{ err }: Index { idx } does not exist in the config block { key }." - except KeyError as err: - err_msg = f"{ err }: Config block { key } does not exist." + except IndexError as warn: + warn_msg = f"{ warn }: Index { idx } does not exist in the config block { key }." + except KeyError as warn: + warn_msg = f"{ warn }: Config block { key } does not exist." except Exception as err: err_msg = f"{ err }: An unexpected error occurred while processing JSON { obj }." - - print(err_msg) - return None + + if args.verbose and warn_msg is not None: + print(warn_msg, file=sys.stderr) + if err_msg is not None: + print(err_msg, file=sys.stderr) + return "" def json_blk_to_file(endpoint_setting, json_filename): """Generate json file from endpoints setting block""" + err_msg = None try: with open(json_filename, 'w', encoding='utf-8') as f: f.write(endpoint_setting) f.close() + return True except Exception as err: - print("Unexpected error writing JSON file %s: %s" % (json_filename, err)) - return None - return True + err_msg = f"Unexpected error writing JSON file { json_filename }:{ err }" + if err_msg is not None: + print(err_msg, file=sys.stderr) + return None def load_json_file(json_file): """Load JSON file and return a json object""" + err_msg = None try: input_fp = open(json_file, 'r') input_json = json.load(input_fp) input_fp.close() + return input_json except FileNotFoundError as err: - print("Could not find JSON file %s: %s" % (json_file, err)) - return None + err_msg = f"Could not find JSON file { json_file }:{ err }" except IOError as err: - print("Could not open/read JSON file %s: %s" % (json_file, err)) - return None + err_msg = "Could not open/read JSON file { json_file }:{ err }" except Exception as err: - print("Unexpected error opening JSON file %s: %s" % (json_file, err)) - return None + err_msg = f"Unexpected error opening JSON file { json_file }:{ err }" except JSONDecodeError as err: - print("Decoding JSON file %s has failed: %s" % (json_file, err)) - return None + err_msg = f"Decoding JSON file %s has failed: { json_file }:{ err }" except TypeError as err: - print("JSON object type error: %s" % (err)) - return None - return input_json + err_msg = f"JSON object type error: { err }" + if err_msg is not None: + print(err_msg, file=sys.stderr) + return None def json_to_stream(json_obj, cfg, idx): """Parse key:value from a JSON object/block and transform into a stream""" - stream = "" err_msg = None + stream = "" if json_obj is None: return None if cfg == 'endpoint': @@ -133,7 +144,7 @@ def json_to_stream(json_obj, cfg, idx): err_msg=f"{ err }: Failed to parse config { cfg }, index { idx }." if err_msg is not None: - print(f"ERROR: { err_msg }") + print(err_msg, file=sys.stderr) return None for key in json_blk: @@ -198,6 +209,7 @@ def json_to_stream(json_obj, cfg, idx): def validate_schema(input_json, schema_file = None): """Validate json with schema file""" + err_msg = None # schema_file defaults to general schema.json for the full run-file schema_path = "%s/JSON/" % (os.path.dirname(os.path.abspath(__file__))) schema_default = "schema.json" @@ -213,7 +225,8 @@ def validate_schema(input_json, schema_file = None): return False validate(instance = input_json, schema = schema_obj) except Exception as err: - print("JSON schema validation error: %s" % (err)) + err_msg = f"JSON schema validation error: { err }" + print(err_msg, file=sys.stderr) return False return True diff --git a/util/tests/test-blockbreaker.py b/util/tests/test-blockbreaker.py index eefb5fbf..3697ae1d 100755 --- a/util/tests/test-blockbreaker.py +++ b/util/tests/test-blockbreaker.py @@ -64,7 +64,7 @@ def test_dump_json_mvparams(self, load_json_file): def test_json_to_stream_endpoints_invalid_idx(self, load_json_file, capsys): input_json = blockbreaker.json_to_stream(load_json_file, "endpoints", 1) out, err = capsys.readouterr() - assert 'Invalid index' in out + assert out == "" assert input_json is None """Test validate_schema using default schema for null schema_file arg"""