Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/TurtleArt/taprimitive.py
diff options
context:
space:
mode:
Diffstat (limited to 'TurtleArt/taprimitive.py')
-rw-r--r--TurtleArt/taprimitive.py16
1 files changed, 14 insertions, 2 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. """