Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/TurtleArt/taexportpython.py
diff options
context:
space:
mode:
authorMarion <marion.zepf@gmail.com>2013-07-18 08:36:32 (GMT)
committer Marion <marion.zepf@gmail.com>2013-07-18 08:36:32 (GMT)
commitf08f71ea8efb9eb7de31fd3a84ac3d550ce39974 (patch)
tree957f2eb925d25ba6aa301c0867ff3cf53cbd1287 /TurtleArt/taexportpython.py
parente64440bd50721d848653c24b6a69093bc10c31c4 (diff)
taexportpython: generate the exported file and fill it with some setup code
- taexportpython will eventually transform all the action stacks into python code and wrap them in separate functions
Diffstat (limited to 'TurtleArt/taexportpython.py')
-rw-r--r--TurtleArt/taexportpython.py115
1 files changed, 115 insertions, 0 deletions
diff --git a/TurtleArt/taexportpython.py b/TurtleArt/taexportpython.py
new file mode 100644
index 0000000..ac04512
--- /dev/null
+++ b/TurtleArt/taexportpython.py
@@ -0,0 +1,115 @@
+#Copyright (c) 2013 Marion Zepf
+
+#Permission is hereby granted, free of charge, to any person obtaining a copy
+#of this software and associated documentation files (the "Software"), to deal
+#in the Software without restriction, including without limitation the rights
+#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+#copies of the Software, and to permit persons to whom the Software is
+#furnished to do so, subject to the following conditions:
+
+#The above copyright notice and this permission notice shall be included in
+#all copies or substantial portions of the Software.
+
+#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+#THE SOFTWARE.
+
+""" Python export tool """
+
+import re
+from os import linesep
+
+
+
+_SETUP_CODE_START = """\
+#!/usr/bin/env python
+
+from window_setup import *
+
+
+tw = get_tw()
+
+BOX = {}
+ACTION = {}
+
+
+
+"""
+_SETUP_CODE_END = """\
+
+
+
+if __name__ == '__main__':
+ start()
+ gtk.main()
+
+
+"""
+_ACTION_STACK_START = """\
+def %s():
+ turtle = tw.turtles.get_active_turtle()
+ canvas = tw.canvas
+ logo = tw.lc
+
+"""
+_ACTION_STACK_END = """\
+ACTION["%s"] = %s
+"""
+# character that is illegal in a Python identifier
+PAT_IDENTIFIER_ILLEGAL_CHAR = re.compile("[^A-Za-z0-9_]")
+
+
+
+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
+ # TODO serialize the ASTs into python code
+ generated_code = "pass"
+
+ # wrap the setup code around everything
+ name_id = _make_identifier(name)
+ generated_code = _indent(generated_code, 1)
+ if generated_code.endswith(linesep):
+ newline = ""
+ else:
+ newline = linesep
+ snippets = [_SETUP_CODE_START,
+ _ACTION_STACK_START % (name_id),
+ generated_code,
+ newline,
+ _ACTION_STACK_END % (name, name_id),
+ _SETUP_CODE_END]
+ return "".join(snippets)
+
+def _make_identifier(name):
+ """ Turn name into a Python identifier name by replacing illegal
+ characters """
+ replaced = re.sub(PAT_IDENTIFIER_ILLEGAL_CHAR, "_", name)
+ # TODO find better strategy to avoid number at beginning
+ if re.match("[0-9]", replaced):
+ replaced = "_" + replaced
+ return replaced
+
+def _indent(code, num_levels=1):
+ """ Indent each line of code with num_levels * 4 spaces
+ code -- some python code as a (multi-line) string """
+ indentation = " " * (4 * num_levels)
+ line_list = code.split(linesep)
+ new_line_list = []
+ for line in line_list:
+ new_line_list.append(indentation + line)
+ return linesep.join(new_line_list)
+
+
+
+if __name__ == "__main__":
+ f = open("/bigrepos/sugar-labs/ta-python-export-dev/exported.py", "w")
+ f.write(action_stack_to_python([]))
+ f.close()
+
+