Delete utils/block_correcter.py
Browse files- utils/block_correcter.py +0 -143
utils/block_correcter.py
DELETED
|
@@ -1,143 +0,0 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import secrets
|
| 3 |
-
import string
|
| 4 |
-
from collections import defaultdict
|
| 5 |
-
|
| 6 |
-
def generate_secure_token(length=20):
|
| 7 |
-
charset = string.ascii_letters + string.digits + "!@#$%^&*()[]{}=+-_~"
|
| 8 |
-
return ''.join(secrets.choice(charset) for _ in range(length))
|
| 9 |
-
|
| 10 |
-
def process_scratch_blocks(all_generated_blocks, generated_output_json):
|
| 11 |
-
processed_blocks = {}
|
| 12 |
-
|
| 13 |
-
# Initialize dictionaries to store and reuse generated unique IDs
|
| 14 |
-
# This prevents creating multiple unique IDs for the same variable/broadcast across different blocks
|
| 15 |
-
variable_id_map = defaultdict(lambda: generate_secure_token(20))
|
| 16 |
-
broadcast_id_map = defaultdict(lambda: generate_secure_token(20))
|
| 17 |
-
|
| 18 |
-
for block_id, gen_block_data in generated_output_json.items():
|
| 19 |
-
processed_block = {}
|
| 20 |
-
all_gen_block_data = all_generated_blocks.get(block_id, {})
|
| 21 |
-
|
| 22 |
-
# Copy and update fields, inputs, next, parent, shadow, topLevel, mutation, and opcode
|
| 23 |
-
processed_block["opcode"] = all_gen_block_data.get("op_code", gen_block_data.get("op_code"))
|
| 24 |
-
processed_block["inputs"] = {}
|
| 25 |
-
processed_block["fields"] = {}
|
| 26 |
-
processed_block["shadow"] = all_gen_block_data.get("shadow", gen_block_data.get("shadow"))
|
| 27 |
-
processed_block["topLevel"] = all_gen_block_data.get("topLevel", gen_block_data.get("topLevel"))
|
| 28 |
-
processed_block["parent"] = all_gen_block_data.get("parent", gen_block_data.get("parent"))
|
| 29 |
-
processed_block["next"] = all_gen_block_data.get("next", gen_block_data.get("next"))
|
| 30 |
-
if "mutation" in all_gen_block_data:
|
| 31 |
-
processed_block["mutation"] = all_gen_block_data["mutation"]
|
| 32 |
-
|
| 33 |
-
# Process inputs
|
| 34 |
-
if "inputs" in all_gen_block_data:
|
| 35 |
-
for input_name, input_data in all_gen_block_data["inputs"].items():
|
| 36 |
-
if input_name in ["SUBSTACK", "CONDITION"]:
|
| 37 |
-
# These should always be type 2
|
| 38 |
-
if isinstance(input_data, list) and len(input_data) == 2:
|
| 39 |
-
processed_block["inputs"][input_name] = [2, input_data[1]]
|
| 40 |
-
elif isinstance(input_data, dict) and input_data.get("kind") == "block":
|
| 41 |
-
processed_block["inputs"][input_name] = [2, input_data.get("block")]
|
| 42 |
-
else: # Fallback for unexpected formats, try to use the original if possible
|
| 43 |
-
processed_block["inputs"][input_name] = gen_block_data["inputs"].get(input_name, [2, None])
|
| 44 |
-
|
| 45 |
-
elif isinstance(input_data, dict):
|
| 46 |
-
if input_data.get("kind") == "value":
|
| 47 |
-
# Case 1: Direct value input
|
| 48 |
-
processed_block["inputs"][input_name] = [
|
| 49 |
-
1,
|
| 50 |
-
[
|
| 51 |
-
4,
|
| 52 |
-
str(input_data.get("value", ""))
|
| 53 |
-
]
|
| 54 |
-
]
|
| 55 |
-
elif input_data.get("kind") == "block":
|
| 56 |
-
# Case 3: Nested block input
|
| 57 |
-
existing_shadow_value = ""
|
| 58 |
-
if input_name in gen_block_data.get("inputs", {}) and \
|
| 59 |
-
isinstance(gen_block_data["inputs"][input_name], list) and \
|
| 60 |
-
len(gen_block_data["inputs"][input_name]) > 2 and \
|
| 61 |
-
isinstance(gen_block_data["inputs"][input_name][2], list) and \
|
| 62 |
-
len(gen_block_data["inputs"][input_name][2]) > 1:
|
| 63 |
-
existing_shadow_value = gen_block_data["inputs"][input_name][2][1]
|
| 64 |
-
|
| 65 |
-
processed_block["inputs"][input_name] = [
|
| 66 |
-
3,
|
| 67 |
-
input_data.get("block", ""),
|
| 68 |
-
[
|
| 69 |
-
10, # Assuming 10 for number/string shadow
|
| 70 |
-
existing_shadow_value
|
| 71 |
-
]
|
| 72 |
-
]
|
| 73 |
-
elif input_data.get("kind") == "menu":
|
| 74 |
-
# Handle menu inputs like in event_broadcast
|
| 75 |
-
menu_option = input_data.get("option", "")
|
| 76 |
-
|
| 77 |
-
# Generate or retrieve a unique ID for the broadcast message
|
| 78 |
-
broadcast_id = broadcast_id_map[menu_option] # Use defaultdict for unique IDs
|
| 79 |
-
|
| 80 |
-
processed_block["inputs"][input_name] = [
|
| 81 |
-
1,
|
| 82 |
-
[
|
| 83 |
-
11, # This is typically the code for menu dropdowns
|
| 84 |
-
menu_option,
|
| 85 |
-
broadcast_id
|
| 86 |
-
]
|
| 87 |
-
]
|
| 88 |
-
elif isinstance(input_data, list):
|
| 89 |
-
# For cases like TOUCHINGOBJECTMENU, where input_data is a list [1, "block_id"]
|
| 90 |
-
processed_block["inputs"][input_name] = input_data
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
# Process fields
|
| 94 |
-
if "fields" in all_gen_block_data:
|
| 95 |
-
for field_name, field_value in all_gen_block_data["fields"].items():
|
| 96 |
-
if field_name == "VARIABLE" and isinstance(field_value, list) and len(field_value) > 0:
|
| 97 |
-
# Generate or retrieve a unique ID for the variable
|
| 98 |
-
variable_name = field_value[0]
|
| 99 |
-
unique_id = variable_id_map[variable_name] # Use defaultdict for unique IDs
|
| 100 |
-
|
| 101 |
-
processed_block["fields"][field_name] = [
|
| 102 |
-
variable_name,
|
| 103 |
-
unique_id
|
| 104 |
-
]
|
| 105 |
-
elif field_name == "STOP_OPTION":
|
| 106 |
-
processed_block["fields"][field_name] = [
|
| 107 |
-
field_value[0],
|
| 108 |
-
None
|
| 109 |
-
]
|
| 110 |
-
elif field_name == "TOUCHINGOBJECTMENU":
|
| 111 |
-
referenced_menu_block_id = all_gen_block_data["inputs"].get("TOUCHINGOBJECTMENU", [None, None])[1]
|
| 112 |
-
if referenced_menu_block_id and referenced_menu_block_id in all_generated_blocks:
|
| 113 |
-
menu_block = all_generated_blocks[referenced_menu_block_id]
|
| 114 |
-
menu_value = menu_block.get("fields", {}).get("TOUCHINGOBJECTMENU", ["", None])[0]
|
| 115 |
-
processed_block["fields"][field_name] = [menu_value, None]
|
| 116 |
-
else:
|
| 117 |
-
processed_block["fields"][field_name] = [field_value[0], None]
|
| 118 |
-
else:
|
| 119 |
-
processed_block["fields"][field_name] = field_value
|
| 120 |
-
|
| 121 |
-
# Remove unwanted keys from the processed block
|
| 122 |
-
keys_to_remove = ["functionality", "block_shape", "id", "block_name", "block_type"]
|
| 123 |
-
for key in keys_to_remove:
|
| 124 |
-
if key in processed_block:
|
| 125 |
-
del processed_block[key]
|
| 126 |
-
|
| 127 |
-
processed_blocks[block_id] = processed_block
|
| 128 |
-
return processed_blocks
|
| 129 |
-
|
| 130 |
-
# Path to your JSON file
|
| 131 |
-
if __name__ == "__main__":
|
| 132 |
-
all_generated_blocks_path = 'all_generated_blocks.json'
|
| 133 |
-
generated_output_json_path = 'generated_output_json.json'
|
| 134 |
-
|
| 135 |
-
# Open and load the JSON files into Python dictionaries
|
| 136 |
-
with open(all_generated_blocks_path, 'r') as f:
|
| 137 |
-
all_generated_blocks_data = json.load(f)
|
| 138 |
-
|
| 139 |
-
with open(generated_output_json_path, 'r') as f:
|
| 140 |
-
generated_output_json_data = json.load(f)
|
| 141 |
-
|
| 142 |
-
processed_blocks = process_scratch_blocks(all_generated_blocks_data, generated_output_json_data)
|
| 143 |
-
print(json.dumps(processed_blocks, indent=4))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|