From 955cbca83a84b0c1ea06f8c33dfec8dd0f850ed1 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 30 Oct 2013 17:24:11 +0000 Subject: comvert custom blocks to new prim style -- args is passed as a tuple --- diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 8a64f7b..b645e73 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -39,7 +39,7 @@ import traceback from tablock import (Block, Media, media_blocks_dictionary) from taconstants import (TAB_LAYER, DEFAULT_SCALE, ICON_SIZE) -from tajail import myfunc +from tajail import (myfunc, myfunc_import) from tapalette import (block_names, value_blocks) from tatype import (TATypeError, TYPES_NUMERIC) from tautils import (get_pixbuf_from_journal, data_from_file, get_stack_name, @@ -832,6 +832,16 @@ class LogoCode: while self.heap: self.heap.pop() + def prim_myblock(self, *args): + """ Run Python code imported from Journal """ + if self.bindex is not None and self.bindex in self.tw.myblock: + # try: + myfunc_import(self, self.tw.myblock[self.bindex], args) + ''' + except: + raise logoerror("#syntaxerror") + ''' + def prim_myfunction(self, f, *args): """ Programmable block (Call tajail.myfunc and convert any errors to logoerrors) """ diff --git a/plugins/turtle_blocks_extras/turtle_blocks_extras.py b/plugins/turtle_blocks_extras/turtle_blocks_extras.py index bc8d4e5..33142b5 100644 --- a/plugins/turtle_blocks_extras/turtle_blocks_extras.py +++ b/plugins/turtle_blocks_extras/turtle_blocks_extras.py @@ -37,7 +37,6 @@ from TurtleArt.taconstants import (DEFAULT_SCALE, CONSTANTS, from TurtleArt.tautils import (round_int, debug_output, get_path, data_to_string, find_group, image_to_base64, hat_on_top, listify, data_from_file) -from TurtleArt.tajail import myfunc_import from TurtleArt.taprimitive import (ArgSlot, ConstantArg, Primitive) from TurtleArt.tatype import (TYPE_BOOL, TYPE_BOX, TYPE_CHAR, TYPE_INT, TYPE_FLOAT, TYPE_OBJECT, TYPE_STRING, @@ -670,7 +669,6 @@ advanced multi-variable math equations, e.g., sin(x+y+z)')) self.tw.lc.def_prim('cartesian', 0, lambda self: self.tw.set_cartesian(True)) - primitive_dictionary['userdefined'] = self._prim_myblock palette.add_block('userdefined', style='basic-style-var-arg', label=' ', @@ -681,8 +679,8 @@ advanced multi-variable math equations, e.g., sin(x+y+z)')) help_string=_('runs code found in the tamyblock.py \ module found in the Journal')) self.tw.lc.def_prim('userdefined', 1, - lambda self, x: - primitive_dictionary['userdefined']([x])) + Primitive(self.tw.lc.prim_myblock, + arg_descs=[ArgSlot(TYPE_OBJECT)])) BLOCKS_WITH_SKIN.append('userdefined') PYTHON_SKIN.append('userdefined') @@ -698,8 +696,9 @@ module found in the Journal')) help_string=_('runs code found in the tamyblock.py \ module found in the Journal')) self.tw.lc.def_prim('userdefined2', 2, - lambda self, x, y: - primitive_dictionary['userdefined']([x, y])) + Primitive(self.tw.lc.prim_myblock, + arg_descs=[ArgSlot(TYPE_OBJECT), + ArgSlot(TYPE_OBJECT)])) BLOCKS_WITH_SKIN.append('userdefined2args') PYTHON_SKIN.append('userdefined2args') @@ -715,8 +714,10 @@ module found in the Journal')) help_string=_('runs code found in the tamyblock.py \ module found in the Journal')) self.tw.lc.def_prim('userdefined3', 3, - lambda self, x, y, z: - primitive_dictionary['userdefined']([x, y, z])) + Primitive(self.tw.lc.prim_myblock, + arg_descs=[ArgSlot(TYPE_OBJECT), + ArgSlot(TYPE_OBJECT), + ArgSlot(TYPE_OBJECT)])) BLOCKS_WITH_SKIN.append('userdefined3args') PYTHON_SKIN.append('userdefined3args') MEDIA_SHAPES.append('pythonsmall') diff --git a/pysamples/brain.py b/pysamples/brain.py index 33ee320..3510e03 100644 --- a/pysamples/brain.py +++ b/pysamples/brain.py @@ -21,7 +21,7 @@ # . -def myblock(tw, text): +def myblock(tw, args): ''' Dialog with AIML library: Usage: Load this code into a Python Block. Pass text as an argument and the robot's response will be pushed to the stack. Use a Pop Block to pop the response @@ -104,6 +104,7 @@ Close other activities and try once more.')) return kernel + text = args[0] if not hasattr(tw, 'aiml_kernel'): tw.aiml_kernel = brain_load(tw, get_default_voice()) response_text = brain_respond(tw.aiml_kernel, text) diff --git a/pysamples/csound.py b/pysamples/csound.py index a935674..ec1b014 100644 --- a/pysamples/csound.py +++ b/pysamples/csound.py @@ -17,16 +17,24 @@ def myblock(tw, sound): import os dirs = [os.path.join( - os.environ['HOME'], - 'Activities/TamTamMini.activity/common/Resources/Sounds/')] + os.environ['HOME'], + 'Activities/TamTamMini.activity/common/Resources/Sounds/'), + os.path.join( + os.environ['HOME'], + 'Activities/TamTamJam.activity/common/Resources/Sounds/'), + os.path.join( + os.environ['HOME'], + 'Activities/TamTamEdit.activity/common/Resources/Sounds/')] orchlines = [] scorelines = [] instrlist = [] def finddir(): + print dirs for d in dirs: if os.path.isdir(d): return d + return '.' def playSine(pitch=1000, amplitude=5000, duration=1, starttime=0, pitch_envelope='default', amplitude_envelope='default'): @@ -128,18 +136,17 @@ def myblock(tw, sound): csd.write("\n") csd.close() - if type(sound) == float: - playSine(pitch=float(sound)) - elif type(sound) == list: # Create a score by computing a sinewave. - if len(sound) == 1: + if len(sound) == 1: + if isinstance(sound[0], float) or isinstance(sound[0], int): playSine(pitch=float(sound[0])) - elif len(sound) == 2: + else: # Create a score from a prerecorded Wave file. + playWave(sound[0]) + else: + if len(sound) == 2: playSine(pitch=float(sound[0]), amplitude=float(sound[1])) else: playSine(pitch=float(sound[0]), amplitude=float(sound[1]), duration=float(sound[2])) - else: # Create a score from a prerecorded Wave file. - playWave(sound) if tw.running_sugar: path = os.path.join(get_path(tw.activity, 'instance'), 'tmp.csd') else: diff --git a/pysamples/dotted_line.py b/pysamples/dotted_line.py index c0d3008..098e0a4 100644 --- a/pysamples/dotted_line.py +++ b/pysamples/dotted_line.py @@ -124,11 +124,11 @@ # of the numeric argument block docked to the Python block. -def myblock(tw, line_length): +def myblock(tw, args): ''' Draw a dotted line of length line_length. ''' try: # make sure line_length is a number - line_length = float(line_length) + line_length = float(args[0]) except ValueError: return if tw.turtles.get_active_turtle().get_pen_state(): diff --git a/pysamples/forward_push.py b/pysamples/forward_push.py index ca527db..80fe818 100644 --- a/pysamples/forward_push.py +++ b/pysamples/forward_push.py @@ -10,7 +10,7 @@ # destination to the FILO. -def myblock(tw, name): +def myblock(tw, args): ''' ''' def _prim_forward_push(tw, line_length): @@ -42,7 +42,7 @@ def myblock(tw, name): # Create a new block prototype. palette.add_block('forwardpush', style='basic-style-1arg', - label=name, + label=args[0], default=100, prim_name='forwardpush', help_string=_('push destination rgb value to heap')) diff --git a/pysamples/grecord.py b/pysamples/grecord.py index a28b82c..486fdb7 100644 --- a/pysamples/grecord.py +++ b/pysamples/grecord.py @@ -10,7 +10,7 @@ # Sugar Journal. -def myblock(tw, arg): +def myblock(tw, args): ''' Record and playback a sound (Sugar only) ''' import os import gst @@ -204,12 +204,9 @@ def myblock(tw, arg): # Sometime we need to parse multiple arguments, e.g., save, savename save_name = '%s_%s' % (tw.activity.name, _('sound')) - if isinstance(arg, list): - cmd = arg[0].lower() - if len(arg) > 1: - save_name = str(arg[1]) - else: - cmd = arg.lower() + cmd = args[0].lower() + if len(args) > 1: + save_name = str(arg[1]) if cmd == 'start' or cmd == _('start').lower(): tw.grecord.start_recording_audio() diff --git a/pysamples/serial.py b/pysamples/serial.py index 84772ab..d15011c 100644 --- a/pysamples/serial.py +++ b/pysamples/serial.py @@ -9,13 +9,13 @@ # (3) use a Pop Block to retrieve any strings input from serial device. -def myblock(tw, x): # x is the string to transmit +def myblock(tw, args): # x is the string to transmit import serial # you may need to install this library # serial device on USB, 9600 baud ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) - ser.write(str(x)) # send string x + ser.write(str(args[0])) # send string x st = ser.read(1000) # read up to 1000 bytes tw.lc.heap.append(st) # append to heap ser.close() diff --git a/pysamples/sinewave.py b/pysamples/sinewave.py index 4f14c4c..a060f34 100644 --- a/pysamples/sinewave.py +++ b/pysamples/sinewave.py @@ -8,8 +8,9 @@ # the speaker at the specified frequency. -def myblock(tw, frequency): +def myblock(tw, args): ''' Plays a sound at frequency frequency ''' import os + frequency = args[0] os.system('speaker-test -t sine -l 1 -f %d' % (int(frequency))) diff --git a/pysamples/speak.py b/pysamples/speak.py index 30762a9..13215e8 100644 --- a/pysamples/speak.py +++ b/pysamples/speak.py @@ -28,7 +28,7 @@ def myblock(tw, arg): import os pitch = None - if type(arg) == type([]): + if len(arg) > 1: text = arg[0] if len(arg) > 1: pitch = int(arg[1]) @@ -37,7 +37,7 @@ def myblock(tw, arg): elif pitch < 0: pitch = 0 else: - text = arg + text = arg[0] # Turtle Art numbers are passed as float, # but they may be integer values. diff --git a/pysamples/uturn.py b/pysamples/uturn.py index edd2bcd..f6ff2e0 100644 --- a/pysamples/uturn.py +++ b/pysamples/uturn.py @@ -8,7 +8,7 @@ # can use the u-turn block as you would any other block. -def myblock(tw, arg): +def myblock(tw, args): ''' Add a uturn block to the 'turtle' palette ''' # def_prim takes 3 arguments: the primitive name, the number of -- cgit v0.9.1