diff options
Diffstat (limited to 'TurtleArt/taexportpython.py')
-rw-r--r-- | TurtleArt/taexportpython.py | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/TurtleArt/taexportpython.py b/TurtleArt/taexportpython.py index 76087ad..49bde06 100644 --- a/TurtleArt/taexportpython.py +++ b/TurtleArt/taexportpython.py @@ -33,7 +33,7 @@ from talogo import LogoCode from taprimitive import (ast_yield_true, Primitive, PyExportError, value_to_ast) from tautils import (debug_output, find_group, find_top_block, get_stack_name) - +from tawindow import global_objects _SETUP_CODE_START = """\ @@ -66,10 +66,11 @@ def %s(): """ _START_STACK_START_ADD = """\ tw.start_plugins() + global_objects = tw.get_global_objects() """ _ACTION_STACK_PREAMBLE = """\ - turtle = tw.turtles.get_active_turtle() turtles = tw.turtles + turtle = turtles.get_active_turtle() canvas = tw.canvas logo = tw.lc @@ -83,7 +84,7 @@ 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 """ + """ Find all the action stacks and turn each into Python code """ all_blocks = tw.just_blocks() blocks_covered = set() tops_of_stacks = [] @@ -98,22 +99,23 @@ def save_python(tw): for block in tops_of_stacks: stack_name = get_stack_name(block) if stack_name: - pythoncode = _action_stack_to_python(block, tw.lc, name=stack_name) + pythoncode = _action_stack_to_python(block, tw, name=stack_name) snippets.append(pythoncode) snippets.append(linesep) snippets.append(_SETUP_CODE_END) return "".join(snippets) -def _action_stack_to_python(block, lc, name="start"): - """ Turn a stack of blocks into python code +def _action_stack_to_python(block, tw, name="start"): + """ Turn a stack of blocks into Python code name -- the name of the action stack (defaults to "start") """ + if isinstance(name, int): name = float(name) if not isinstance(name, basestring): name = str(name) # traverse the block stack and get the AST for every block - ast_list = _walk_action_stack(block, lc) + ast_list = _walk_action_stack(block, tw.lc) if not isinstance(ast_list[-1], ast.Yield): ast_list.append(ast_yield_true()) action_stack_ast = ast.Module(body=ast_list) @@ -126,6 +128,11 @@ def _action_stack_to_python(block, lc, name="start"): name_id = _make_identifier(name) if name == 'start': pre_preamble = _START_STACK_START_ADD + # TODO: only add the objects we are using + for k in global_objects.keys(): + if k not in ['window', 'canvas', 'logo', 'turtles']: + pre_preamble += " %s = global_objects['%s']\n" % ( + k.lower(), k) else: pre_preamble = '' generated_code = _indent(generated_code, 1) @@ -255,5 +262,3 @@ def _indent(code, num_levels=1): for line in line_list: new_line_list.append(indentation + line) return linesep.join(new_line_list) - - |