From acb61a37f56038c51f88e7cf7029a7295c307eb5 Mon Sep 17 00:00:00 2001 From: Marion Date: Wed, 24 Jul 2013 20:58:13 +0000 Subject: HiddenBlock inherits from Block, which has new utility methods for value blocks --- diff --git a/TurtleArt/tablock.py b/TurtleArt/tablock.py index 8d8ed4a..e878a46 100644 --- a/TurtleArt/tablock.py +++ b/TurtleArt/tablock.py @@ -24,7 +24,8 @@ import cairo from taconstants import (EXPANDABLE, EXPANDABLE_ARGS, OLD_NAMES, CONSTANTS, STANDARD_STROKE_WIDTH, BLOCK_SCALE, BOX_COLORS, - GRADIENT_COLOR, EXPANDABLE_FLOW, COLORDICT) + GRADIENT_COLOR, EXPANDABLE_FLOW, COLORDICT, + PREFIX_DICTIONARY) from tapalette import (palette_blocks, block_colors, expandable_blocks, content_blocks, block_names, block_primitives, block_styles, special_block_colors) @@ -34,6 +35,9 @@ import sprites from tautils import (debug_output, error_output) +media_blocks_dictionary = {} # new media blocks get added here + + class Blocks: """ A class for the list of blocks and everything they share in common """ @@ -277,6 +281,37 @@ class Block: return False return True + def is_value_block(self): + """ Return True iff this block is a value block (numeric, string, + media, etc.) """ + return self.primitive is None and self.values + + def get_value(self): + """ Return the value stored in this value block """ + # TODO what error to raise if this is not a value block? + if self.name == 'number': + try: + return float(self.values[0]) + except ValueError: + return float(ord(self.values[0][0])) + elif (self.name == 'string' or + self.name == 'title'): # deprecated block + if isinstance(self.values[0], (float, int)): + if int(self.values[0]) == self.values[0]: + self.values[0] = int(self.values[0]) + return '#s' + str(self.values[0]) + else: + return '#s' + self.values[0] + elif self.name in PREFIX_DICTIONARY: + if self.values[0] is not None: + return PREFIX_DICTIONARY[self.name] + str(self.values[0]) + else: + return PREFIX_DICTIONARY[self.name] + 'None' + elif self.name in media_blocks_dictionary: + return '#smedia_' + self.name.upper() + else: + return '%nothing%' + def highlight(self): """ We may want to highlight a block... """ if self.spr is not None and self.status is not 'collapsed': diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 7aac4ce..c6991bd 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -33,7 +33,8 @@ try: except ImportError: GRID_CELL_SIZE = 55 -from taconstants import (TAB_LAYER, DEFAULT_SCALE, PREFIX_DICTIONARY) +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) @@ -46,7 +47,6 @@ except ImportError: from gettext import gettext as _ -media_blocks_dictionary = {} # new media blocks get added here primitive_dictionary = {} # new block primitives get added here @@ -79,7 +79,7 @@ class logoerror(Exception): return str(self.value) -class HiddenBlock: +class HiddenBlock(Block): def __init__(self, name, value=None): self.name = name @@ -92,6 +92,7 @@ class HiddenBlock: self.connections = [] self.docks = [] + # Utility functions @@ -286,30 +287,12 @@ class LogoCode: self.tw.block_list.list.index(blk))) else: code.append(blk.primitive) # Hidden block - elif len(blk.values) > 0: # Extract the value from content blocks. - if blk.name == 'number': - try: - code.append(float(blk.values[0])) - except ValueError: - code.append(float(ord(blk.values[0][0]))) - elif blk.name == 'string' or \ - blk.name == 'title': # deprecated block - if isinstance(blk.values[0], (float, int)): - if int(blk.values[0]) == blk.values[0]: - blk.values[0] = int(blk.values[0]) - code.append('#s' + str(blk.values[0])) - else: - code.append('#s' + blk.values[0]) - elif blk.name in PREFIX_DICTIONARY: - if blk.values[0] is not None: - code.append(PREFIX_DICTIONARY[blk.name] + - str(blk.values[0])) - else: - code.append(PREFIX_DICTIONARY[blk.name] + 'None') - elif blk.name in media_blocks_dictionary: - code.append('#smedia_' + blk.name.upper()) - else: + elif blk.is_value_block(): # Extract the value from content blocks. + value = blk.get_value() + if value == '%nothing%': return ['%nothing%'] + else: + code.append(value) else: return ['%nothing%'] if blk.connections is not None and len(blk.connections) > 0: -- cgit v0.9.1