diff options
author | Marion <marion.zepf@gmail.com> | 2013-08-16 09:49:20 (GMT) |
---|---|---|
committer | Marion <marion.zepf@gmail.com> | 2013-08-16 09:49:20 (GMT) |
commit | 7d97ef90f223f4f3980f355dc3e9d39b8b8ee7c5 (patch) | |
tree | ad6fc6fc3c30309ac641c4cb2a8ce4ee4d032609 | |
parent | a4456ea4b6b346bca7ea632a19daba46849dadfc (diff) |
use a utility function to get the name of a stack from the blocks
-rw-r--r-- | TurtleArt/talogo.py | 23 | ||||
-rw-r--r-- | TurtleArt/tautils.py | 20 |
2 files changed, 32 insertions, 11 deletions
diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index b96a2b9..fb78bf7 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -39,7 +39,8 @@ from tablock import (Block, media_blocks_dictionary) from taconstants import (TAB_LAYER, DEFAULT_SCALE) from tapalette import (block_names, value_blocks) from tautils import (get_pixbuf_from_journal, convert, data_from_file, - text_media_type, round_int, debug_output, find_group) + text_media_type, round_int, debug_output, find_group, + get_stack_name) try: from util.RtfParser import RtfTextOnly @@ -248,17 +249,17 @@ class LogoCode: code = self._blocks_to_code(b) self.stacks['stack2'] = self._readline(code) elif b.name == 'hat': - if b.connections is not None and len(b.connections) > 1 and \ - b.connections[1] is not None: + stack_name = get_stack_name(b) + if (b.connections is not None and len(b.connections) > 1 and + stack_name): code = self._blocks_to_code(b) - try: - x = b.connections[1].values[0] - except IndexError: - self.tw.showlabel('#nostack') - self.tw.showblocks() - self.tw.running_blocks = False - return None - self.stacks[self._get_stack_key(x)] = self._readline(code) + stack_key = self._get_stack_key(stack_name) + self.stacks[stack_key] = self._readline(code) + else: + self.tw.showlabel('#nostack') + self.tw.showblocks() + self.tw.running_blocks = False + return None code = self._blocks_to_code(blk) diff --git a/TurtleArt/tautils.py b/TurtleArt/tautils.py index 244be6d..a8ef384 100644 --- a/TurtleArt/tautils.py +++ b/TurtleArt/tautils.py @@ -808,6 +808,26 @@ def find_blk_below(blk, namelist): return None +def get_stack_name(blk): + ''' Return the name of the action stack that the given block belongs to. + If the top block of this stack is not a stack-defining block, return + None. ''' + top_block = find_top_block(blk) + if top_block.name == 'start': + return 'start' + elif top_block.name == 'hat1': + return 'stack1' + elif top_block.name == 'hat2': + return 'stack2' + elif top_block.name == 'hat': + try: + return top_block.connections[1].values[0] + except (IndexError, AttributeError): + return None + else: + return None + + def get_hardware(): ''' Determine whether we are using XO 1.0, 1.5, ... or 'unknown' hardware ''' |