From 76b91d02e9893932c7795aea954f579d41e01ed4 Mon Sep 17 00:00:00 2001 From: Michael Jungo Date: Thu, 13 Jul 2023 11:11:22 +0200 Subject: [PATCH] fix(py): ignore JSON files with invalid categories When a category, such as "images" occurs in the JSON file, it was assumed that it was a dictionary, but that may not be the case, hence it crashed when trying to access a dict value. This now just ignores them. --- py/lavd/fs.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/py/lavd/fs.py b/py/lavd/fs.py index 618df16..6ef95ad 100644 --- a/py/lavd/fs.py +++ b/py/lavd/fs.py @@ -101,21 +101,24 @@ def insert_file( abs_path = Path(abs_path) if file_category == "json": json_data = load_json(abs_path) - if "scalars" in json_data: - data.set("scalars", name, step, category, json_data["scalars"]) - if "texts" in json_data: - text = json_data["texts"] - text_len = len(text.get("actual", "")) + len(text.get("expeted", "")) + scalars_dict = json_data.get("scalars") + if isinstance(scalars_dict, dict): + data.set("scalars", name, step, category, scalars_dict) + text_dict = json_data.get("texts") + if isinstance(text_dict, dict): + text_len = len(text_dict.get("actual", "")) + len( + text_dict.get("expeted", "") + ) data.set( "texts", name, step, category, - text, + text_dict, truncate=text_len > MAX_TEXT_LEN, ) - if "images" in json_data: - image_dict = json_data["images"] + image_dict = json_data.get("images") + if isinstance(image_dict, dict): image_source = image_dict.get("source") if image_source: image_path = abs_path.parent / image_source