MCPcopy Create free account
hub / github.com/TimMcCool/scratchattach / from_json

Method from_json

scratchattach/editor/backpack_json.py:62–97  ·  view source on GitHub ↗

Load a block in the **backpack** JSON format :param data: A dictionary (not list) :return: A new block/prim object

(data: dict)

Source from the content-addressed store, hash-verified

60
61 @staticmethod
62 def from_json(data: dict) -> prim.Prim | block.Block:
63 """
64 Load a block in the **backpack** JSON format
65 :param data: A dictionary (not list)
66 :return: A new block/prim object
67 """
68 _opcode = data["opcode"]
69
70 _x, _y = data.get("x"), data.get("y")
71 if prim.is_prim_opcode(_opcode):
72 # This is actually a prim
73 prim_value, prim_name, prim_id = parse_prim_fields(data["fields"])
74 return prim.Prim(prim.PrimTypes.find(_opcode, "opcode"),
75 prim_value, prim_name, prim_id)
76
77 _next_id = data.get("next")
78 _parent_id = data.get("parent")
79
80 _shadow = data.get("shadow", False)
81 _top_level = data.get("topLevel", _parent_id is None)
82
83 _inputs = {}
84 for _input_code, _input_data in data.get("inputs", {}).items():
85 _inputs[_input_code] = BpInput.from_json(_input_data)
86
87 _fields = {}
88 for _field_code, _field_data in data.get("fields", {}).items():
89 _fields[_field_code] = BpField.from_json(_field_data)
90
91 if "mutation" in data:
92 _mutation = mutation.Mutation.from_json(data["mutation"])
93 else:
94 _mutation = None
95
96 return block.Block(_opcode, _shadow, _top_level, _mutation, _fields, _inputs, _x, _y, _next_id=_next_id,
97 _parent_id=_parent_id)
98
99
100def load_script(_script_data: list[dict]) -> sprite.Sprite:

Callers

nothing calls this directly

Calls 4

parse_prim_fieldsFunction · 0.85
getMethod · 0.45
findMethod · 0.45
from_jsonMethod · 0.45

Tested by

no test coverage detected