diff options
author | Walter Bender <walter@sugarlabs.org> | 2013-11-06 14:09:31 (GMT) |
---|---|---|
committer | Walter Bender <walter@sugarlabs.org> | 2013-11-06 14:09:31 (GMT) |
commit | f75b0922d559bcdb6d547354ea772d765f7f33d3 (patch) | |
tree | cf8d48cb73b6e97e4a2ee619bbbb5c940e007a7f /TurtleArt/taexportpython.py | |
parent | 578071e7d0c282be79349198e3855444bc49b4a6 (diff) |
workaround for set_turtle bug
Diffstat (limited to 'TurtleArt/taexportpython.py')
-rw-r--r-- | TurtleArt/taexportpython.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/TurtleArt/taexportpython.py b/TurtleArt/taexportpython.py index 1ed1ac5..c3c8e84 100644 --- a/TurtleArt/taexportpython.py +++ b/TurtleArt/taexportpython.py @@ -123,6 +123,13 @@ def _action_stack_to_python(block, tw, name="start"): # serialize the ASTs into python code generated_code = codegen.to_source(action_stack_ast) + # FIXME: there must be a better way to do this using the AST + # patch code to include missing get_active_turtle() calls + if generated_code.count('turtles.set_turtle') > 0: + generated_code = _insert_extra_code( + generated_code, 'turtles.set_turtle', + 'turtle = turtles.get_active_turtle()\n') + # wrap the action stack setup code around everything name_id = _make_identifier(name) if name == 'start': @@ -145,6 +152,27 @@ def _action_stack_to_python(block, tw, name="start"): return "".join(snippets) +def _insert_extra_code(generated_code, target, insert): + """ Add extra code when needed """ + tmp_code = '' + while target in generated_code: + i = generated_code.index(target) + if i == 0: + indent = 0 + else: + indent = 0 + while generated_code[i - 1 - indent] == ' ': + indent += 1 + while generated_code[i] != '\n': + i += 1 + i += 1 # include the newline + tmp_code += generated_code[0:i] + for j in range(indent): + tmp_code += ' ' + tmp_code += insert + generated_code = generated_code[i:] + return tmp_code + generated_code + def _walk_action_stack(top_block, lc, convert_me=True): """ Turn a stack of blocks into a list of ASTs convert_me -- convert values and Primitives to ASTs or return them |