"""JSON file I/O helpers for command-line scripts.""" import json from pathlib import Path from python_helpers.console import fail def load_json(path, missing_hint=None): """Load and parse a JSON file, failing with a consistent message if it is missing. Args: path: path to the JSON file. missing_hint: optional extra line of guidance to print if the file is missing. Returns: The parsed JSON content. """ if not Path(path).exists(): message = f"{path} not found!" if missing_hint: message += f"\n{missing_hint}" fail(message) with open(path, "r") as f: return json.load(f) def save_json(path, data, indent=2): """Save data to a file as JSON. Args: path: path to write to. data: JSON-serializable data to write. indent: indentation level to format the JSON with. """ with open(path, "w") as f: json.dump(data, f, indent=indent)