#Copyright (c) 2007-8, Playful Invention Company. #Copyright (c) 2008-9, Walter Bender #Permission is hereby granted, free of charge, to any person obtaining a copy #of this software and associated documentation files (the "Software"), to deal #in the Software without restriction, including without limitation the rights #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #copies of the Software, and to permit persons to whom the Software is #furnished to do so, subject to the following conditions: #The above copyright notice and this permission notice shall be included in #all copies or substantial portions of the Software. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #THE SOFTWARE. import re from time import * import gobject from operator import isNumberType import random import audioop from math import * import subprocess from UserDict import UserDict try: from sugar.datastore import datastore except: pass class noKeyError(UserDict): __missing__=lambda x,y: 0 class taLogo: pass from taturtle import * from tagplay import * from tajail import * from constants import * from gettext import gettext as _ procstop = False class symbol: def __init__(self, name): self.name = name self.nargs = None self.fcn = None def __str__(self): return self.name def __repr__(self): return '#'+self.name class logoerror(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) def run_blocks(lc, blk, blocks, run_flag): print "run blocks: %s" % (blk.name) for b in blocks: if b is None: print " None" else: print " %s" % b.name # user-defined stacks for x in lc.stacks.keys(): lc.stacks[x] = None # two built-in stacks lc.stacks['stack1'] = None lc.stacks['stack2'] = None for b in blocks: if b.name == 'hat1': lc.stacks['stack1'] = readline(lc,blocks_to_code(lc, b)) if b.name=='hat2': lc.stacks['stack2'] = readline(lc,blocks_to_code(lc, b)) if b.name == 'hat': if (b.connections[1] is not None): text = b.connections[1].spr.labels[0] lc.stacks['stack3'+text] = readline(lc,blocks_to_code(lc, b)) code = blocks_to_code(lc, blk) if run_flag is True: print "code: %s" % (code) setup_cmd(lc, code) else: return code def blocks_to_code(lc, blk): if blk is None: return ['%nothing%'] spr = blk.spr code = [] dock = blk.docks[0] if len(dock)>4: code.append(dock[4]) if blk.primitive is not None: code.append(blk.primitive) else: if blk.name=='number': try: code.append(float(spr.labels[0])) except: code.append(float(ord(spr.labels[0][0]))) elif blk.name=='string' or blk.name=='title': if type(spr.labels[0]) == float or type(spr.labels[0]) == int: if int(spr.labels[0]) == spr.labels[0]: spr.labels[0] = int(spr.labels[0]) code.append('#s'+str(spr.labels[0])) else: code.append('#s'+spr.labels[0]) elif blk.name=='journal': if spr.ds_id is not None: # TODO: put ds_id in blk code.append('#smedia_'+str(spr.ds_id)) else: code.append('#smedia_None') elif blk.name=='descriptionoff' or \ blk.name=='descriptionon': if spr.ds_id is not None: code.append('#sdescr_'+str(spr.ds_id)) else: code.append('#sdescr_None') elif blk.name=='audiooff' or blk.name=='audio': if spr.ds_id is not None: code.append('#saudio_'+str(spr.ds_id)) else: code.append('#saudio_None') else: return ['%nothing%'] for i in range(1,len(blk.connections)): b = blk.connections[i] dock = blk.docks[i] if len(dock)>4: for c in dock[4]: code.append(c) if b is not None: code.extend(blocks_to_code(lc, b)) elif blk.docks[i][0] not in ['flow', 'unavailable']: print "appending nothing" code.append('%nothing%') return code def intern(lc, str): if str in lc.oblist: return lc.oblist[str] sym = symbol(str) lc.oblist[str] = sym return sym def parseline(str): split = re.split(r"\s|([\[\]()])", str) return [x for x in split if x and x != ""] def readline(lc, line): res = [] while line: token = line.pop(0) if isNumberType(token): res.append(token) elif token.isdigit(): res.append(float(token)) elif token[0]=='-' and token[1:].isdigit(): res.append(-float(token[1:])) elif token[0] == '"': res.append(token[1:]) elif token[0:2] == "#s": res.append(token[2:]) elif token == '[': res.append(readline(lc,line)) elif token == ']': return res else: res.append(intern(lc, token)) return res def setup_cmd(lc, str): lc.tw.turtle.spr.set_layer(HIDE_LAYER) lc.procstop=False list = readline(lc, str) lc.step = start_eval(lc, list) def start_eval(lc, list): icall(lc, evline, list); yield True # turn off stop icon when execution is finished if hasattr(lc.tw,"activity"): lc.tw.activity.stop_button.set_icon("stopitoff") yield False def evline(lc, list): oldiline = lc.iline lc.iline = list[:] lc.arglist = None while lc.iline: if lc.tw.step_time > 0: lc.tw.turtle.spr.set_layer(TURTLE_LAYER) endtime = millis()+an_int(lc,lc.tw.step_time)*100 while millis()