diff --git a/tools/run.py b/tools/run.py index 3dcbb3e..8dbd8d1 100755 --- a/tools/run.py +++ b/tools/run.py @@ -16,6 +16,41 @@ script = sys.argv[1] base_path = Path(f"scripts/{script}") +# --------------------------------------------------- +# Load script metadata +# --------------------------------------------------- +model_path = base_path / "model.json" +meta_path = base_path / "meta.json" + +if not model_path.exists(): + raise RuntimeError(f"Missing model.json in {base_path}") + +with open(model_path, "r", encoding="utf-8") as f: + model = json.load(f) + +meta = {} +if meta_path.exists(): + with open(meta_path, "r", encoding="utf-8") as f: + meta = json.load(f) + +output_format = meta.get("output_format") + + +def first_variable_name(model_key, fallback_name): + variables = model.get(model_key) or [] + if not variables: + return fallback_name + + name = variables[0].get("name") + if not name: + raise RuntimeError(f"First {model_key} entry in model.json is missing a name") + + return name + + +input_name = first_variable_name("inputVariables", "data_in") +output_name = first_variable_name("outputVariables", "data_out") + # --------------------------------------------------- # Load raw input (format-agnostic) # --------------------------------------------------- @@ -28,21 +63,9 @@ test_input = f.read() payload = { - "data_in": test_input + input_name: test_input } -# --------------------------------------------------- -# Load meta data (optional, for format hints) -# --------------------------------------------------- -meta_path = base_path / "meta.json" - -meta = {} -if meta_path.exists(): - with open(meta_path, "r", encoding="utf-8") as f: - meta = json.load(f) - -output_format = meta.get("output_format") - # --------------------------------------------------- # Headers # --------------------------------------------------- @@ -76,10 +99,10 @@ # --------------------------------------------------- # Validate output # --------------------------------------------------- -if "data_out" not in data: - raise RuntimeError(f"No data_out in response: {data}") +if output_name not in data: + raise RuntimeError(f"No {output_name} in response: {data}") -output = data["data_out"] +output = data[output_name] if output is None or (isinstance(output, str) and not output.strip()): raise RuntimeError("Script returned empty output") @@ -118,4 +141,4 @@ else: print(str(output)[:500]) -print(f"\n>>> Saved as tests/output.{output_format if output_format != 'Base64' else 'bin'}") \ No newline at end of file +print(f"\n>>> Saved as tests/output.{output_format if output_format != 'Base64' else 'bin'}")