diff options
author | Marion <marion.zepf@gmail.com> | 2013-07-18 09:24:47 (GMT) |
---|---|---|
committer | Marion <marion.zepf@gmail.com> | 2013-07-18 09:24:47 (GMT) |
commit | 9cf37b454cf84f8352ab8f1217638b395f460630 (patch) | |
tree | 8bb938e2d336f481aaa32b20e88b8a2da0d644a1 /TurtleArt/taexportpython.py | |
parent | ccf1695b3ebb4c13ac4756e1f454d72b34df9aff (diff) |
taexportpython: separate general setup code from code for each action stack
Diffstat (limited to 'TurtleArt/taexportpython.py')
-rw-r--r-- | TurtleArt/taexportpython.py | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/TurtleArt/taexportpython.py b/TurtleArt/taexportpython.py index 85889bb..60ad0ba 100644 --- a/TurtleArt/taexportpython.py +++ b/TurtleArt/taexportpython.py @@ -69,17 +69,20 @@ PAT_IDENTIFIER_ILLEGAL_CHAR = re.compile("[^A-Za-z0-9_]") def save_python(tw): """ Find all the action stacks and turn each into python code """ stacks_of_blocks = tw.just_blocks() - pythoncode = "" - for stack in stacks_of_blocks: + snippets = [_SETUP_CODE_START] + for block in stacks_of_blocks: # TODO name of action stack? - top = find_top_block(stack) - if stack != top: + # TODO there must be a more efficient way than finding the top from each block + top = find_top_block(block) + if block != top: continue - pythoncode += action_stack_to_python(stack) - pythoncode += linesep - return pythoncode + pythoncode = _action_stack_to_python(block) + snippets.append(pythoncode) + snippets.append(linesep) + snippets.append(_SETUP_CODE_END) + return "".join(snippets) -def action_stack_to_python(blklist, name="start"): +def _action_stack_to_python(blklist, name="start"): """ Turn a stack of blocks into python code name -- the name of the action stack (defaults to "start") """ # TODO loop over blklist and get the AST for every block @@ -93,12 +96,10 @@ def action_stack_to_python(blklist, name="start"): newline = "" else: newline = linesep - snippets = [_SETUP_CODE_START, - _ACTION_STACK_START % (name_id), + snippets = [_ACTION_STACK_START % (name_id), generated_code, newline, - _ACTION_STACK_END % (name, name_id), - _SETUP_CODE_END] + _ACTION_STACK_END % (name, name_id)] return "".join(snippets) def _make_identifier(name): @@ -124,7 +125,7 @@ def _indent(code, num_levels=1): if __name__ == "__main__": f = open("/bigrepos/sugar-labs/ta-python-export-dev/exported.py", "w") - f.write(action_stack_to_python([])) + f.write(_action_stack_to_python([])) f.close() |