Add an utility function to extract all blocks out of a stack
This commit is contained in:
parent
3b3f50a72f
commit
770dfa70ad
2 changed files with 25 additions and 0 deletions
python/libscratchproject
|
@ -1 +1,3 @@
|
|||
import utils
|
||||
|
||||
from project import Project
|
||||
|
|
23
python/libscratchproject/utils.py
Normal file
23
python/libscratchproject/utils.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
|
||||
|
||||
def _real_extract_blocks_from_stack(stack):
|
||||
for block in stack:
|
||||
if isinstance(block, list):
|
||||
if isinstance(block[0], str): # Got a primitive
|
||||
yield block
|
||||
for item in _real_extract_blocks_from_stack(block[1:]):
|
||||
yield item
|
||||
else: # Inside a sequence
|
||||
for item in _real_extract_blocks_from_stack(block):
|
||||
yield item
|
||||
|
||||
|
||||
def extract_blocks_from_script(script):
|
||||
'''
|
||||
Returns a flattened list of blocks used in the stack.
|
||||
Includes all arguments passed to the block.
|
||||
'''
|
||||
blocks = []
|
||||
blocknames = []
|
||||
blocks = list(_real_extract_blocks_from_stack(stack[2]))
|
||||
return blocks
|
Reference in a new issue