# -*- coding: utf-8 -*- #Copyright (c) 2007-8, Playful Invention Company. #Copyright (c) 2008-10, Walter Bender #Copyright (c) 2008-10, Raúl Gutiérrez Segalés #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 gtk import gobject from time import clock from math import sqrt from random import uniform from operator import isNumberType import audioop import subprocess from UserDict import UserDict try: from sugar.datastore import datastore except: pass from taconstants import * from tagplay import play_audio, play_movie_from_file, stop_media from tajail import myfunc, myfunc_import from tautils import get_pixbuf_from_journal, movie_media_type,\ audio_media_type, round_int from gettext import gettext as _ procstop = False class noKeyError(UserDict): __missing__=lambda x,y: 0 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) """ Utility functions """ def careful_divide(x,y): try: if y==0: return 0 return x/y except: return 0 def taequal(x,y): try: return float(x)==float(y) except: if type(x) == str or type(x) == unicode: xx = ord(x[0]) else: xx = x if type(y) == str or type(y) == unicode: yy = ord(y[0]) else: yy = y return xx==yy def taless(x, y): try: return float(x)4: code.append(dock[4]) # There could be a '(' or '['. if blk.primitive is not None: code.append(blk.primitive) elif len(blk.values)>0: # Extract the value from content blocks. if blk.name=='number': try: code.append(float(blk.values[0])) except ValueError: code.append(float(ord(blk.values[0][0]))) elif blk.name=='string' or blk.name=='title': if type(blk.values[0]) == float or type(blk.values[0]) == int: if int(blk.values[0]) == blk.values[0]: blk.values[0] = int(blk.values[0]) code.append('#s'+str(blk.values[0])) else: code.append('#s'+blk.values[0]) elif blk.name=='journal': if blk.values[0] is not None: code.append('#smedia_'+str(blk.values[0])) else: code.append('#smedia_None') elif blk.name=='description': if blk.values[0] is not None: code.append('#sdescr_'+str(blk.values[0])) else: code.append('#sdescr_None') elif blk.name=='audio': if blk.values[0] is not None: code.append('#saudio_'+str(blk.values[0])) else: code.append('#saudio_None') else: print "%s had no primitive." % (blk.name) return ['%nothing%'] else: print "%s had no value." % (blk.name) 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(self.blocks_to_code(b)) elif blk.docks[i][0] not in ['flow', 'unavailable']: code.append('%nothing%') return code """ Execute the psuedocode. """ def setup_cmd(self, str): self.tw.active_turtle.hide() # Hide the turtle while we are running. self.procstop = False list = self.readline(str) self.step = self.start_eval(list) """ Convert the pseudocode into a list of commands. """ def readline(self, 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(self.readline(line)) elif token == ']': return res else: res.append(self.intern(token)) return res """ Add the object to the object list. """ def intern(self, str): if str in self.oblist: return self.oblist[str] sym = symbol(str) self.oblist[str] = sym return sym """ Step through the list. """ def start_eval(self, list): self.icall(self.evline, list) yield True if self.tw.running_sugar: self.tw.activity.stop_button.set_icon("stopitoff") yield False """ Add a function to the program stack. """ def icall(self, fcn, *args): self.istack.append(self.step) self.step = fcn(*(args)) """ Evaluate a line of code from the list. """ def evline(self, list): oldiline = self.iline self.iline = list[:] self.arglist = None while self.iline: if self.tw.step_time > 0: # show the turtle during idle time self.tw.active_turtle.show() endtime = millis()+self.an_int(self.tw.step_time)*100 while millis()