diff options
author | Marion <marion.zepf@gmail.com> | 2013-07-18 09:16:01 (GMT) |
---|---|---|
committer | Marion <marion.zepf@gmail.com> | 2013-07-18 09:16:01 (GMT) |
commit | ccf1695b3ebb4c13ac4756e1f454d72b34df9aff (patch) | |
tree | dca6e7a0b3f630e4c9beae009c3d69c7bfc87107 | |
parent | f08f71ea8efb9eb7de31fd3a84ac3d550ce39974 (diff) |
add menu item to GNOME version for exporting python code
-rw-r--r-- | TurtleArt/taexportpython.py | 15 | ||||
-rw-r--r-- | TurtleArt/tautils.py | 2 | ||||
-rwxr-xr-x | turtleblocks.py | 20 |
3 files changed, 36 insertions, 1 deletions
diff --git a/TurtleArt/taexportpython.py b/TurtleArt/taexportpython.py index ac04512..85889bb 100644 --- a/TurtleArt/taexportpython.py +++ b/TurtleArt/taexportpython.py @@ -23,6 +23,8 @@ import re from os import linesep +from tautils import find_top_block + _SETUP_CODE_START = """\ @@ -64,6 +66,19 @@ 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: + # TODO name of action stack? + top = find_top_block(stack) + if stack != top: + continue + pythoncode += action_stack_to_python(stack) + pythoncode += linesep + return pythoncode + 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") """ diff --git a/TurtleArt/tautils.py b/TurtleArt/tautils.py index 07b72d9..244be6d 100644 --- a/TurtleArt/tautils.py +++ b/TurtleArt/tautils.py @@ -311,7 +311,7 @@ def get_save_name(filefilter, load_save_folder, save_file_name): gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK)) dialog.set_default_response(gtk.RESPONSE_OK) - if filefilter in ['.png', '.svg', '.lg']: + if filefilter in ['.png', '.svg', '.lg', '.py']: suffix = filefilter else: suffix = SUFFIX[1] diff --git a/turtleblocks.py b/turtleblocks.py index 5d5808f..02e1aae 100755 --- a/turtleblocks.py +++ b/turtleblocks.py @@ -55,6 +55,7 @@ from TurtleArt.taconstants import (OVERLAY_LAYER, DEFAULT_TURTLE_COLORS, from TurtleArt.tautils import (data_from_string, get_save_name) from TurtleArt.tawindow import TurtleArtWindow from TurtleArt.taexportlogo import save_logo +from TurtleArt.taexportpython import save_python from util.menubuilder import MenuBuilder @@ -377,6 +378,8 @@ return %s(self)" % (p, P, P) self._do_save_picture_cb) MenuBuilder.make_menu_item(menu, _('Save as Logo'), self._do_save_logo_cb) + MenuBuilder.make_menu_item(menu, _('Save as Python'), + self._do_save_python_cb) MenuBuilder.make_menu_item(menu, _('Quit'), self._quit_ta) activity_menu = MenuBuilder.make_sub_menu(menu, _('File')) @@ -527,6 +530,23 @@ Would you like to save before quitting?')) f.write(logocode) f.close() + def _do_save_python_cb(self, widget): + ''' Callback for saving the project as Python code. ''' + # TODO implement + pythoncode = save_python(self.tw) + if not pythoncode: + return + save_type = '.py' + # TODO use name of TA project if it has been saved already + (filename, self.tw.load_save_folder) = get_save_name( + save_type, self.tw.load_save_folder, 'myproject') + if isinstance(filename, unicode): + filename = filename.encode('ascii', 'replace') + if filename is not None: + f = file(filename, 'w') + f.write(pythoncode) + f.close() + def _do_resize_cb(self, widget, factor): ''' Callback to resize blocks. ''' if factor == -1: |