src.util.json_utils module

Utility functions for reading and manipulating json files

src.util.json_utils.get_config_file_type(file_path: str) str[source]

Verify that configuration file is json or yaml

src.util.json_utils.strip_comments(str_, delimiter=None)[source]

Remove comments from str_. Comments are taken to start with an arbitrary delimiter and run to the end of the line.

src.util.json_utils.parse_json(str_)[source]

Parse JSONC (JSON with //-comments) string str_ into a Python object. Comments are discarded. Wraps standard library json.loads().

Syntax errors in the input (JSONDecodeError) are passed through from the Python standard library parser. We correct the line numbers mentioned in the errors to refer to the original file (i.e., with comments.)

src.util.json_utils.read_json(file_path, log=<Logger>)[source]

Reads a struct from a JSONC file at file_path.

Raises:

MDTFFileNotFoundError – If file not found at file_path.

Returns:

dict – data contained in the file, as parsed by parse_json().

Execution exits with error code 1 on all other exceptions.

src.util.json_utils.find_json(file_path, exit_if_missing=True, log=<Logger>)[source]

Reads a JSONC file

Parameters:
  • file_path (str) – Filename to search for.

  • exit_if_missing (bool) – Optional, default True. Exit with error code 1 if file_name not found.

  • log – log file

src.util.json_utils.write_json(struct, file_path, sort_keys=False, log=<Logger>)[source]

Serializes struct to a JSON file at file_path.

Parameters:
  • struct (dict) – Object to serialize.

  • file_path (str) – path of the JSON file to write.

  • sort_keys (bool) – parameter indicating whether to sort keys to pass to json.dumps

  • log (logging.getlogger) – log object

src.util.json_utils.pretty_print_json(struct, sort_keys=False)[source]

Serialize struct to a pseudo-YAML string for human-readable debugging purposes only. Output is not valid JSON (or YAML).