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:52:40 (GMT)
committer Marion <marion.zepf@gmail.com>2013-09-12 23:52:40 (GMT)
commit3d725845c08fe0897ade3bcd6b86c654ab8d5228 (patch)
treeac69a7e6f7f92dc77769d0c7cfa0b10276c64440
parent4c064d5de8e5d5214e5e71498eeb6ed9df373824 (diff)
make Media objects exportable
-rw-r--r--TurtleArt/taprimitive.py16
-rw-r--r--TurtleArt/tatype.py13
-rw-r--r--pyexported/window_setup.py2
3 files changed, 25 insertions, 6 deletions
diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py
index b2b2d09..ec30e6c 100644
--- a/TurtleArt/taprimitive.py
+++ b/TurtleArt/taprimitive.py
@@ -25,6 +25,7 @@ from random import uniform
#from ast_pprint import * # only used for debugging, safe to comment out
+from tablock import Media
from tacanvas import TurtleGraphics
from taconstants import (Color, CONSTANTS)
from talogo import (LogoCode, logoerror, NegativeRootError)
@@ -1041,20 +1042,25 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim):
bool, basestring, list
If the value is already an AST, return it unchanged.
If the value is a non-exportable Primitive, return None. """
- # TODO media
+ # already an AST
if isinstance(value, ast.AST):
return value
+ # Primitive
elif isinstance(value, Primitive):
if value.export_me:
return value.get_ast(*args_for_prim, **kwargs_for_prim)
else:
return None
+ # boolean
elif isinstance(value, bool):
return ast.Name(id=str(value), ctx=ast.Load)
+ # number
elif isinstance(value, (int, float)):
return ast.Num(n=value)
+ # string
elif isinstance(value, basestring):
return ast.Str(value)
+ # list (recursively transform to an AST)
elif isinstance(value, list):
ast_list = []
for item in value:
@@ -1062,16 +1068,22 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim):
if item_ast is not None:
ast_list.append(item_ast)
return ast.List(elts=ast_list, ctx=ast.Load)
+ # color
elif isinstance(value, Color):
# call to the Color constructor with this object's values,
# e.g., Color('red', 0, 50, 100)
return get_call_ast('Color', [value.name, value.color,
value.shade, value.gray],
return_type=TYPE_COLOR)
+ # media
+ elif isinstance(value, Media):
+ args = [value_to_ast(value.type), value_to_ast(value.value)]
+ return get_call_ast('Media', args, return_type=TYPE_MEDIA)
+ # unknown
else:
raise ValueError("unknown type of raw value: " + repr(type(value)))
-def ast_to_value(ast_object):
+def ast_to_value(ast_object): # TODO make obsolete and remove
""" Retrieve the value out of a value AST. Supported AST types:
Num, Str, Name, List, Tuple, Set
If no value can be extracted, return None. """
diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py
index 3a14fbf..1402014 100644
--- a/TurtleArt/tatype.py
+++ b/TurtleArt/tatype.py
@@ -23,6 +23,7 @@
import ast
from gettext import gettext as _
+from tablock import Media
from taconstants import (Color, CONSTANTS)
from tautils import debug_output
@@ -74,11 +75,11 @@ TYPE_CHAR = Type('TYPE_CHAR', 1)
TYPE_COLOR = Type('TYPE_COLOR', 2)
TYPE_FLOAT = Type('TYPE_FLOAT', 3)
TYPE_INT = Type('TYPE_INT', 4)
+TYPE_MEDIA = Type('TYPE_MEDIA', 10)
TYPE_NUMBER = Type('TYPE_NUMBER', 6) # shortcut to avoid a TypeDisjunction
# between TYPE_FLOAT and TYPE_INT
TYPE_NUMERIC_STRING = Type('TYPE_NUMERIC_STRING', 7)
TYPE_STRING = Type('TYPE_STRING', 9)
-# TODO add list types
BOX_AST = ast.Name(id='BOX', ctx=ast.Load)
@@ -104,6 +105,8 @@ def get_type(x):
return (TYPE_NUMERIC_STRING, False)
elif isinstance(x, Color):
return (TYPE_COLOR, False)
+ elif isinstance(x, Media):
+ return (TYPE_MEDIA, False)
elif hasattr(x, "return_type"):
return (x.return_type, False)
@@ -133,6 +136,10 @@ def get_type(x):
return (TYPE_CHAR, True)
elif x.func.id in ('repr', 'str', 'unicode'):
return (TYPE_STRING, True)
+ elif x.func.id == 'Color':
+ return (TYPE_COLOR, True)
+ elif x.func.id == 'Media':
+ return (TYPE_MEDIA, True)
# unary operands never change the type of their argument
elif isinstance(x, ast.UnaryOp):
if issubclass(x.op, ast.Not):
@@ -146,9 +153,9 @@ def get_type(x):
# other binary operators
elif isinstance(x, ast.BinOp):
type_left = get_type(x.left)[0]
- if type_left == TYPE_STRING:
- return (TYPE_STRING, True)
type_right = get_type(x.right)[0]
+ if type_left == TYPE_STRING or type_right == TYPE_STRING:
+ return (TYPE_STRING, True)
if type_left == type_right == TYPE_INT:
return (TYPE_INT, True)
else:
diff --git a/pyexported/window_setup.py b/pyexported/window_setup.py
index 9586f2c..62947dd 100644
--- a/pyexported/window_setup.py
+++ b/pyexported/window_setup.py
@@ -39,7 +39,7 @@ from TurtleArt.taconstants import (HORIZONTAL_PALETTE, VERTICAL_PALETTE, BLOCK_S
EXPANDABLE_FLOW, SUFFIX)
from TurtleArt.talogo import (LogoCode, primitive_dictionary, logoerror)
from TurtleArt.tacanvas import TurtleGraphics
-from TurtleArt.tablock import (Blocks, Block)
+from TurtleArt.tablock import (Blocks, Block, Media)
from TurtleArt.taturtle import (Turtles, Turtle)
from TurtleArt.tautils import (magnitude, get_load_name, get_save_name, data_from_file,
data_to_file, round_int, get_id, get_pixbuf_from_journal,