Spaces:
Running
Running
Update utils/block_relation_builder.py
Browse files
utils/block_relation_builder.py
CHANGED
@@ -2514,9 +2514,36 @@ def generate_plan(generated_input, opcode_keys, pseudo_code):
|
|
2514 |
#--------------------------------------------------[Security key id generation for the better understanding of keys]---------------------------------------------
|
2515 |
#################################################################################################################################################################
|
2516 |
|
2517 |
-
def generate_secure_token(length=20):
|
2518 |
-
|
2519 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2520 |
|
2521 |
#################################################################################################################################################################
|
2522 |
#--------------------------------------------------[Processed the two Skelton as input and generate refined skelton json]----------------------------------------
|
@@ -2527,8 +2554,8 @@ def process_scratch_blocks(all_generated_blocks, generated_output_json):
|
|
2527 |
|
2528 |
# Initialize dictionaries to store and reuse generated unique IDs
|
2529 |
# This prevents creating multiple unique IDs for the same variable/broadcast across different blocks
|
2530 |
-
variable_id_map = defaultdict(lambda: generate_secure_token(
|
2531 |
-
broadcast_id_map = defaultdict(lambda: generate_secure_token(
|
2532 |
|
2533 |
# Define the mapping for input field names to their required integer types for shadows
|
2534 |
input_type_mapping = {
|
@@ -2590,7 +2617,7 @@ def process_scratch_blocks(all_generated_blocks, generated_output_json):
|
|
2590 |
processed_block["inputs"][input_name] = input_data
|
2591 |
else:
|
2592 |
# Fallback for unexpected formats, try to use the original if possible
|
2593 |
-
processed_block["inputs"][input_name] = gen_block_data["inputs"].get(input_name, [1, [11, "message1", generate_secure_token(
|
2594 |
|
2595 |
elif isinstance(input_data, dict):
|
2596 |
if input_data.get("kind") == "value":
|
|
|
2514 |
#--------------------------------------------------[Security key id generation for the better understanding of keys]---------------------------------------------
|
2515 |
#################################################################################################################################################################
|
2516 |
|
2517 |
+
# def generate_secure_token(length=20):
|
2518 |
+
# charset = string.ascii_letters + string.digits + "!@#$%^&*()[]{}=+-_~"
|
2519 |
+
# return ''.join(secrets.choice(charset) for _ in range(length))
|
2520 |
+
# conservative, safe alphabet derived from Scratch-style IDs
|
2521 |
+
_DEFAULT_SCRATCH_ALPHABET = string.ascii_letters + string.digits + "_-"
|
2522 |
+
_DEFAULT_LENGTH = 20
|
2523 |
+
|
2524 |
+
# small in-memory cache to avoid duplicates in a single run
|
2525 |
+
_generated_ids_cache = set()
|
2526 |
+
|
2527 |
+
def generate_secure_token() -> str:
|
2528 |
+
"""
|
2529 |
+
Return a single Scratch-style ID string (20 chars long) using a safe alphabet.
|
2530 |
+
No input required.
|
2531 |
+
|
2532 |
+
Notes:
|
2533 |
+
- This does NOT check for collisions inside an existing project.json.
|
2534 |
+
If you need to guarantee uniqueness with an existing file, use the
|
2535 |
+
project-aware generator shared earlier.
|
2536 |
+
- Collisions are astronomically unlikely for random 20-char tokens, but
|
2537 |
+
the function still avoids duplicates within the same Python process.
|
2538 |
+
"""
|
2539 |
+
alphabet = _DEFAULT_SCRATCH_ALPHABET
|
2540 |
+
length = _DEFAULT_LENGTH
|
2541 |
+
|
2542 |
+
while True:
|
2543 |
+
token = "".join(secrets.choice(alphabet) for _ in range(length))
|
2544 |
+
if token not in _generated_ids_cache:
|
2545 |
+
_generated_ids_cache.add(token)
|
2546 |
+
return token
|
2547 |
|
2548 |
#################################################################################################################################################################
|
2549 |
#--------------------------------------------------[Processed the two Skelton as input and generate refined skelton json]----------------------------------------
|
|
|
2554 |
|
2555 |
# Initialize dictionaries to store and reuse generated unique IDs
|
2556 |
# This prevents creating multiple unique IDs for the same variable/broadcast across different blocks
|
2557 |
+
variable_id_map = defaultdict(lambda: generate_secure_token())
|
2558 |
+
broadcast_id_map = defaultdict(lambda: generate_secure_token())
|
2559 |
|
2560 |
# Define the mapping for input field names to their required integer types for shadows
|
2561 |
input_type_mapping = {
|
|
|
2617 |
processed_block["inputs"][input_name] = input_data
|
2618 |
else:
|
2619 |
# Fallback for unexpected formats, try to use the original if possible
|
2620 |
+
processed_block["inputs"][input_name] = gen_block_data["inputs"].get(input_name, [1, [11, "message1", generate_secure_token()]])
|
2621 |
|
2622 |
elif isinstance(input_data, dict):
|
2623 |
if input_data.get("kind") == "value":
|