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-08-15 19:46:04 (GMT)
committer Marion <marion.zepf@gmail.com>2013-08-15 19:46:04 (GMT)
commit6835d03a9a307ef1624c8e97d826c42f9e2437d2 (patch)
tree46953c0b187d40541258fc857627ba45fdf2d5a1
parent991b4f4697eeb14f1a0c55fd714863cf4bbf90de (diff)
use utility function for extracting values out of ASTs
- Fix a bug that was accidentally introduced in one of the previous commits.
-rw-r--r--TurtleArt/taprimitive.py60
1 files changed, 18 insertions, 42 deletions
diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py
index e05460b..1fbeac9 100644
--- a/TurtleArt/taprimitive.py
+++ b/TurtleArt/taprimitive.py
@@ -588,25 +588,15 @@ class Primitive(object):
Otherwise, convert both to a number. (Colors are converted to an
integer before they are converted to a string.) """
convert_to_ast = False
+ (value1_ast, value2_ast) = (None, None)
- def _get_value_and_ast(val):
- if isinstance(val, ast.AST):
- val_ast = val
- val = None
- if isinstance(val_ast, ast.Num):
- val = val_ast.n
- elif isinstance(val_ast, ast.Str):
- val = val_ast.s
- elif isinstance(val_ast, ast.List):
- val = val_ast.elts
- elif (isinstance(val_ast, ast.Name) and
- val_ast.id.startswith("CONSTANTS[")):
- val = eval(val_ast.id)
- return (val, val_ast, True)
- else:
- return (val, None, False)
- (value1, value1_ast, convert_to_ast) = _get_value_and_ast(value1)
- (value2, value2_ast, convert_to_ast) = _get_value_and_ast(value2)
+ if isinstance(value1, ast.AST):
+ convert_to_ast = True
+ value1_ast = value1
+ value1 = ast_to_value(value1_ast)
+ if isinstance(value2, ast.AST):
+ value2_ast = value2
+ value2 = ast_to_value(value2_ast)
def _to_string(val, val_ast):
""" Return strings as they are, convert Colors to an integer and
@@ -687,16 +677,9 @@ class Primitive(object):
if isinstance(value, ast.AST):
convert_to_ast = True
value_ast = value
- value = None
- if isinstance(value_ast, ast.Str):
- value = value_ast.s
- elif isinstance(value_ast, ast.List):
- value = value_ast.elts
- elif (isinstance(value_ast, ast.Name) and
- value_ast.id.startswith("CONSTANTS[")):
- value = eval(value_ast.id)
- if isinstance(decimal_point, ast.Str):
- decimal_point = decimal_point.s
+ value = ast_to_value(value_ast)
+ if isinstance(decimal_point, ast.AST):
+ decimal_point = ast_to_value(decimal_point)
# 2./3. string
if isinstance(value, basestring):
@@ -816,11 +799,9 @@ class Primitive(object):
if isinstance(value, ast.AST):
convert_to_ast = True
value_ast = value
- value = None
- if isinstance(value_ast, ast.Str):
- value = value_ast.s
- if isinstance(decimal_point, ast.Str):
- decimal_point = decimal_point.s
+ value = ast_to_value(value_ast)
+ if isinstance(decimal_point, ast.AST):
+ decimal_point = ast_to_value(decimal_point)
if isinstance(value, basestring):
# 1. string containing a number
@@ -873,13 +854,6 @@ class Primitive(object):
-class PrimitiveCall(Primitive):
- """ Primitive that is called when it is a constant argument to another
- Primitive. """
- pass
-
-
-
def is_instancemethod(method):
# TODO how to access the type `instancemethod` directly?
return type(method).__name__ == "instancemethod"
@@ -923,7 +897,10 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim):
return ast.List(elts=ast_list, ctx=ast.Load)
elif isinstance(value, Color):
if str(value) in CONSTANTS:
- return ast.Name(id='CONSTANTS[%s]' % repr(value), ctx=ast.Load)
+ # repr(str(value)) is necessary; it first converts the Color to a
+ # string and then adds appropriate quotes around that string
+ return ast.Name(id='CONSTANTS[%s]' % repr(str(value)),
+ ctx=ast.Load)
else:
# call to the Color constructor with this object's values,
# e.g., Color('red', 0, 50, 100)
@@ -932,7 +909,6 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim):
else:
raise ValueError("unknown type of raw value: " + repr(type(value)))
-# TODO replace every fiddling with a call to this function
def ast_to_value(ast_object):
""" Retrieve the value out of a value AST. Supported AST types:
Num, Str, Name, List, Tuple, Set