Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarion <marion.zepf@gmail.com>2013-09-12 23:26:05 (GMT)
committer Marion <marion.zepf@gmail.com>2013-09-12 23:26:05 (GMT)
commit4c064d5de8e5d5214e5e71498eeb6ed9df373824 (patch)
tree8bc66f88ccd0a4d9a37504b97a1d5bcae07028ee
parent40b688685dfce753276f86f3a09ece9081c0bf7c (diff)
use class for media objects instead of prefixed strings
- Applies to all media (image), audio, video, Journal description, and camera objects.
-rw-r--r--TurtleArt/tablock.py44
-rw-r--r--TurtleArt/taconstants.py6
-rw-r--r--TurtleArt/talogo.py129
-rw-r--r--TurtleArt/tawindow.py27
-rw-r--r--plugins/turtle_blocks_extras/turtle_blocks_extras.py10
5 files changed, 124 insertions, 92 deletions
diff --git a/TurtleArt/tablock.py b/TurtleArt/tablock.py
index a3f91c2..f258029 100644
--- a/TurtleArt/tablock.py
+++ b/TurtleArt/tablock.py
@@ -25,7 +25,7 @@ import cairo
from taconstants import (EXPANDABLE, EXPANDABLE_ARGS, OLD_NAMES, CONSTANTS,
STANDARD_STROKE_WIDTH, BLOCK_SCALE, BOX_COLORS,
GRADIENT_COLOR, EXPANDABLE_FLOW, Color,
- PREFIX_DICTIONARY)
+ MEDIA_BLOCK2TYPE)
from tapalette import (palette_blocks, block_colors, expandable_blocks,
content_blocks, block_names, block_primitives,
block_styles, special_block_colors)
@@ -37,6 +37,33 @@ from tautils import (debug_output, error_output)
media_blocks_dictionary = {} # new media blocks get added here
+class Media(object):
+ """ Media objects can be images, audio files, videos, Journal
+ descriptions, or camera snapshots. """
+
+ ALL_TYPES = ('media', 'audio', 'video', 'descr', 'camera', 'camera1')
+
+ def __init__(self, type_, value=None):
+ """
+ type_ --- a string that indicates the kind of media:
+ media --- image
+ audio --- audio file
+ video --- video
+ descr --- Journal description
+ camera, camera1 --- camera snapshot
+ value --- a file path or a reference to a Sugar datastore object """
+ if type_ not in Media.ALL_TYPES:
+ raise ValueError("Media.type must be one of " +
+ repr(Media.ALL_TYPES))
+ self.type = type_
+ self.value = value
+
+ def __str__(self):
+ return '%s_%s' % (self.type, str(self.value))
+
+ def __repr__(self):
+ return 'Media(type=%s, value=%s)' % (repr(self.type), repr(self.value))
+
class Blocks:
@@ -301,7 +328,6 @@ class Block:
if not self.is_value_block():
return None
- result = ''
if self.name == 'number':
try:
return float(self.values[0])
@@ -311,23 +337,21 @@ class Block:
self.name == 'title'): # deprecated block
if add_type_prefix:
result = '#s'
+ else:
+ result = ''
if isinstance(self.values[0], (float, int)):
if int(self.values[0]) == self.values[0]:
self.values[0] = int(self.values[0])
result += str(self.values[0])
else:
result += self.values[0]
- elif self.name in PREFIX_DICTIONARY:
- if add_type_prefix:
- result = PREFIX_DICTIONARY[self.name]
- result += str(self.values[0])
+ return result
+ elif self.name in MEDIA_BLOCK2TYPE:
+ return Media(MEDIA_BLOCK2TYPE[self.name], self.values[0])
elif self.name in media_blocks_dictionary:
- if add_type_prefix:
- result = '#smedia_'
- result += self.name.upper()
+ return Media('media', self.name.upper())
else:
return None
- return result
def highlight(self):
""" We may want to highlight a block... """
diff --git a/TurtleArt/taconstants.py b/TurtleArt/taconstants.py
index cd1ece2..9666573 100644
--- a/TurtleArt/taconstants.py
+++ b/TurtleArt/taconstants.py
@@ -197,10 +197,10 @@ OLD_DOCK = ['and', 'or', 'plus', 'minus', 'division', 'product', 'remainder']
CONTENT_ARGS = ['show', 'showaligned', 'push', 'storein', 'storeinbox1',
'storeinbox2']
-PREFIX_DICTIONARY = {}
+MEDIA_BLOCK2TYPE = {} # map media blocks to media types
-# These blocks get a special skin
-BLOCKS_WITH_SKIN = []
+
+BLOCKS_WITH_SKIN = [] # These blocks get a special skin
PYTHON_SKIN = []
diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py
index d3410a6..fff1d4b 100644
--- a/TurtleArt/talogo.py
+++ b/TurtleArt/talogo.py
@@ -36,7 +36,7 @@ except ImportError:
import traceback
-from tablock import (Block, media_blocks_dictionary)
+from tablock import (Block, Media, media_blocks_dictionary)
from taconstants import (TAB_LAYER, DEFAULT_SCALE)
from tapalette import (block_names, value_blocks)
from tatype import TATypeError
@@ -346,7 +346,9 @@ class LogoCode:
bindex = None
if isinstance(token, tuple):
(token, bindex) = token
- if isNumberType(token):
+ if isinstance(token, Media):
+ res.append(token)
+ elif isNumberType(token):
res.append(token)
elif token.isdigit():
res.append(float(token))
@@ -892,74 +894,71 @@ class LogoCode:
for blk in drag_group:
blk.spr.move_relative((dx, 0))
- def show(self, string, center=False):
+ def show(self, obj, center=False):
""" Show is the general-purpose media-rendering block. """
- if type(string) == str or type(string) == unicode:
- if string in ['media_', 'descr_', 'audio_', 'video_',
- 'media_None', 'descr_None', 'audio_None',
- 'video_None']:
- pass
- elif string[0:6] in ['media_', 'descr_', 'audio_', 'video_']:
- self.filepath = None
- self.pixbuf = None # Camera writes directly to pixbuf
- self.dsobject = None
- if string[6:].lower() in media_blocks_dictionary:
- media_blocks_dictionary[string[6:].lower()]()
- elif os_path_exists(string[6:]): # is it a path?
- self.filepath = string[6:]
- elif self.tw.running_sugar: # is it a datastore object?
- from sugar.datastore import datastore
- try:
- self.dsobject = datastore.get(string[6:])
- except:
- debug_output("Couldn't find dsobject %s" %
- (string[6:]), self.tw.running_sugar)
- if self.dsobject is not None:
- self.filepath = self.dsobject.file_path
- if self.pixbuf is not None:
- self.insert_image(center=center, pixbuf=True)
- elif self.filepath is None:
- if self.dsobject is not None:
- self.tw.showlabel(
- 'nojournal',
- self.dsobject.metadata['title'])
- else:
- self.tw.showlabel('nojournal', string[6:])
- debug_output("Couldn't open %s" % (string[6:]),
- self.tw.running_sugar)
- elif string[0:6] == 'media_':
- self.insert_image(center=center)
- elif string[0:6] == 'descr_':
- mimetype = None
- if self.dsobject is not None and \
- 'mime_type' in self.dsobject.metadata:
- mimetype = self.dsobject.metadata['mime_type']
- description = None
- if self.dsobject is not None and \
- 'description' in self.dsobject.metadata:
- description = self.dsobject.metadata[
- 'description']
- self.insert_desc(mimetype, description)
- elif string[0:6] == 'audio_':
- self.play_sound()
- elif string[0:6] == 'video_':
- self.play_video()
+ # media
+ if isinstance(obj, Media) and obj.value:
+ self.filepath = None
+ self.pixbuf = None # Camera writes directly to pixbuf
+ self.dsobject = None
+
+ # camera snapshot
+ if obj.value.lower() in media_blocks_dictionary:
+ media_blocks_dictionary[obj.value.lower()]()
+ # file path
+ elif os_path_exists(obj.value):
+ self.filepath = obj.value
+ # datastore object
+ elif self.tw.running_sugar:
+ from sugar.datastore import datastore
+ try:
+ self.dsobject = datastore.get(obj.value)
+ except:
+ debug_output("Couldn't find dsobject %s" %
+ (obj.value), self.tw.running_sugar)
if self.dsobject is not None:
- self.dsobject.destroy()
- else: # assume it is text to display
- x, y = self.x2tx(), self.y2ty()
- if center:
- y -= self.tw.canvas.textsize
- self.tw.turtles.get_active_turtle().draw_text(string, x, y,
- int(self.tw.canvas.textsize *
- self.scale / 100.),
- self.tw.canvas.width - x)
- elif type(string) == float or type(string) == int:
- string = round_int(string)
+ self.filepath = self.dsobject.file_path
+
+ if self.pixbuf is not None:
+ self.insert_image(center=center, pixbuf=True)
+ elif self.filepath is None:
+ if self.dsobject is not None:
+ self.tw.showlabel(
+ 'nojournal',
+ self.dsobject.metadata['title'])
+ else:
+ self.tw.showlabel('nojournal', obj.value)
+ debug_output("Couldn't open %s" % (obj.value),
+ self.tw.running_sugar)
+ elif obj.type == 'media':
+ self.insert_image(center=center)
+ elif obj.type == 'descr':
+ mimetype = None
+ if self.dsobject is not None and \
+ 'mime_type' in self.dsobject.metadata:
+ mimetype = self.dsobject.metadata['mime_type']
+ description = None
+ if self.dsobject is not None and \
+ 'description' in self.dsobject.metadata:
+ description = self.dsobject.metadata[
+ 'description']
+ self.insert_desc(mimetype, description)
+ elif obj.type == 'audio':
+ self.play_sound()
+ elif obj.type == 'video':
+ self.play_video()
+
+ if self.dsobject is not None:
+ self.dsobject.destroy()
+
+ # text or number
+ elif isinstance(obj, (basestring, float, int)):
+ if isinstance(obj, (float, int)):
+ obj = round_int(obj)
x, y = self.x2tx(), self.y2ty()
if center:
y -= self.tw.canvas.textsize
- self.tw.turtles.get_active_turtle().draw_text(string, x, y,
+ self.tw.turtles.get_active_turtle().draw_text(obj, x, y,
int(self.tw.canvas.textsize *
self.scale / 100.),
self.tw.canvas.width - x)
diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py
index f386b68..2949e49 100644
--- a/TurtleArt/tawindow.py
+++ b/TurtleArt/tawindow.py
@@ -67,7 +67,7 @@ from tapalette import (palette_names, palette_blocks, expandable_blocks,
palette_init_on_start)
from talogo import (LogoCode, primitive_dictionary, logoerror)
from tacanvas import TurtleGraphics
-from tablock import (Blocks, Block, media_blocks_dictionary)
+from tablock import (Blocks, Block, Media, media_blocks_dictionary)
from taturtle import (Turtles, Turtle)
from tautils import (magnitude, get_load_name, get_save_name, data_from_file,
data_to_file, round_int, get_id, get_pixbuf_from_journal,
@@ -4320,8 +4320,11 @@ before making changes to your Turtle Blocks program'))
""" Print object n to the bar at the bottom of the screen """
if flag and (self.hide or self.step_time == 0):
return
+
+ # list
if isinstance(n, list):
self.showlabel('print', n)
+ # color
elif isinstance(n, Color):
if n.color is None:
self.showlabel('print', '%s %d, %s %d' %
@@ -4332,27 +4335,33 @@ before making changes to your Turtle Blocks program'))
(_('color'), n.color,
_('shade'), n.shade,
_('gray'), n.gray))
- elif isinstance(n, basestring):
- if n[0:6] == 'media_' and \
- n[6:].lower not in media_blocks_dictionary:
+ # media
+ elif isinstance(n, Media):
+ if (n.type == 'media' and
+ n.value.lower() not in media_blocks_dictionary):
try:
if self.running_sugar:
from sugar.datastore import datastore
try:
- dsobject = datastore.get(n[6:])
+ dsobject = datastore.get(n.value)
except:
- debug_output("Couldn't open %s" % (n[6:]),
+ debug_output("Couldn't open %s" % (n.value),
self.running_sugar)
self.showlabel('print', dsobject.metadata['title'])
dsobject.destroy()
else:
- self.showlabel('print', n[6:])
+ self.showlabel('print', n.value)
except IOError:
- self.showlabel('print', n)
+ self.showlabel('print', str(n))
else:
- self.showlabel('print', n)
+ self.showlabel('print', str(n))
+ # string
+ elif isinstance(n, basestring):
+ self.showlabel('print', n)
+ # integer
elif isinstance(n, int):
self.showlabel('print', n)
+ # other number
else:
self.showlabel(
'print',
diff --git a/plugins/turtle_blocks_extras/turtle_blocks_extras.py b/plugins/turtle_blocks_extras/turtle_blocks_extras.py
index 22bae62..d59a5bb 100644
--- a/plugins/turtle_blocks_extras/turtle_blocks_extras.py
+++ b/plugins/turtle_blocks_extras/turtle_blocks_extras.py
@@ -32,7 +32,7 @@ from TurtleArt.talogo import (primitive_dictionary, logoerror,
media_blocks_dictionary)
from TurtleArt.taconstants import (DEFAULT_SCALE, ICON_SIZE, CONSTANTS,
MEDIA_SHAPES, SKIN_PATHS, BLOCKS_WITH_SKIN,
- PYTHON_SKIN, PREFIX_DICTIONARY, VOICES,
+ PYTHON_SKIN, MEDIA_BLOCK2TYPE, VOICES,
MACROS, Color)
from TurtleArt.tautils import (round_int, debug_output, get_path,
data_to_string, find_group, image_to_base64,
@@ -154,7 +154,7 @@ boolean operators from Numbers palette'))
default='None',
special_name=_('journal'),
help_string=_('Sugar Journal media object'))
- PREFIX_DICTIONARY['journal'] = '#smedia_'
+ MEDIA_BLOCK2TYPE['journal'] = 'media'
BLOCKS_WITH_SKIN.append('journal')
MEDIA_SHAPES.append('journalsmall')
MEDIA_SHAPES.append('journaloff')
@@ -167,7 +167,7 @@ boolean operators from Numbers palette'))
default='None',
help_string=_('Sugar Journal audio object'))
BLOCKS_WITH_SKIN.append('audio')
- PREFIX_DICTIONARY['audio'] = '#saudio_'
+ MEDIA_BLOCK2TYPE['audio'] = 'audio'
MEDIA_SHAPES.append('audiosmall')
MEDIA_SHAPES.append('audiooff')
MEDIA_SHAPES.append('audioon')
@@ -179,7 +179,7 @@ boolean operators from Numbers palette'))
default='None',
help_string=_('Sugar Journal video object'))
BLOCKS_WITH_SKIN.append('video')
- PREFIX_DICTIONARY['video'] = '#svideo_'
+ MEDIA_BLOCK2TYPE['video'] = 'video'
MEDIA_SHAPES.append('videosmall')
MEDIA_SHAPES.append('videooff')
MEDIA_SHAPES.append('videoon')
@@ -191,7 +191,7 @@ boolean operators from Numbers palette'))
default='None',
help_string=_('Sugar Journal description field'))
BLOCKS_WITH_SKIN.append('description')
- PREFIX_DICTIONARY['description'] = '#sdescr_'
+ MEDIA_BLOCK2TYPE['description'] = 'descr'
MEDIA_SHAPES.append('descriptionsmall')
MEDIA_SHAPES.append('descriptionoff')
MEDIA_SHAPES.append('descriptionon')