# -*- 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 re from time import clock import gobject from operator import isNumberType from random import uniform import audioop from math import sqrt import subprocess from UserDict import UserDict try: from sugar.datastore import datastore except: pass from constants import * from tacanvas import * from tagplay import play_audio, play_movie_from_file, stop_media from tajail import myfunc, myfunc_import 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 movie_media_type(suffix): if suffix.replace('.','') in ['ogv','vob','mp4','wmv','mov', 'mpeg']: return True return False def audio_media_type(suffix): if suffix.replace('.','') in ['ogg', 'oga', 'm4a']: return True return False 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]) if blk.primitive is not None: code.append(blk.primitive) elif len(blk.values)>0: 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 nothing as a value" % (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 def intern(self, str): if str in self.oblist: return self.oblist[str] sym = symbol(str) self.oblist[str] = sym return sym 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 def setup_cmd(self, str): self.tw.active_turtle.hide() self.procstop=False list = self.readline(str) self.step = self.start_eval(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 def evline(self, list): oldiline = self.iline self.iline = list[:] self.arglist = None while self.iline: if self.tw.step_time > 0: self.tw.active_turtle.show() endtime = millis()+self.an_int(self.tw.step_time)*100 while millis()