#!/bin/env python import verse as v import versoo import time import sys class stdout_deluxe: def __init__(self): self.orig_stdout = None self.text = "" def enable(self): if not self.orig_stdout: sys.stdout = self self.orig_stdout = sys.__stdout__ def disable(self): if self.orig_stdout: sys.stdout = self.orig_stdout self.orig_stdout = None def write(self, text): self.text += text def get_text(self): text = self.text self.text = "" return text def run_script(node_id, buffer_id): if not node_id in session.nodes: print "--- No such node" return False node = session.nodes[node_id] if not isinstance(node, versoo.Text): print "--- Not a Text node" return False if not node.get_language() == "python/versoo": print "--- Can only execute language python/versoo, not", print node.get_language() return False if not buffer_id in node.buffers: print "--- No such buffer" return False buffer = node.buffers[buffer_id] code = buffer.get_text() # get output buffer global outputnode outbuffername = buffer.get_name() + ".out" if not outbuffername in outputnode.buffers: outputnode.buffer_create(outbuffername) outbuffer = outputnode.buffers[outbuffername] print "--- Executing script ..." # catch stdout/print stdout = stdout_deluxe() stdout.enable() try: bytecode = compile(code, buffer.get_name(), "exec") globaldict = {} globaldict['__builtins__'] = __builtins__ globaldict['versoo'] = versoo globaldict['session'] = session eval(bytecode, globaldict) except Exception, inst: print "Error:", inst stdout.disable() text = stdout.get_text() outbuffer.all_text_set(text) print "--- ... Finished" return True def cb_run(sender, params): node_id = params[0][1] buffer_id = params[1][1] run_script(node_id, buffer_id) def cb_repeat(sender, params): global scripts node_id = params[0][1] buffer_id = params[1][1] if not (node_id, buffer_id) in scripts: scripts.append((node_id, buffer_id)) def cb_kill(sender, params): global scripts node_id = params[0][1] buffer_id = params[1][1] for i in range(0, len(scripts)): if scripts[i][0] == node_id and scripts[i][0] == buffer_id: del scripts[i] for i in range(0, len(autoscripts)): if autoscripts[i][0] == node_id and autoscripts[i][0] == buffer_id: del autoscripts[i] def cb_autorun(sender, params): global autoscripts node_id = params[0][1] buffer_id = params[1][1] run_script(node_id, buffer_id) if not (node_id, buffer_id) in autoscripts: autoscripts.append((node_id, buffer_id)) def cb_text_set(node_id, buffer_id, pos, length, text): global autoscripts if (node_id, buffer_id) in autoscripts: run_script(node_id, buffer_id) session = versoo.Session() session.connect() scripts = [] autoscripts = [] while not session.get_avatar() in session.nodes: session.update() session.callback_set(v.SEND_T_TEXT_SET, cb_text_set) avatar = session.nodes[session.get_avatar()] avatar.name_set("PythonInterpreter") avatar.method_group_create("Scripting") outputnode = session.text_create("PythonOutput", "txt/en") avatar.link_set(outputnode.get_node_id(), "output", 0); params = [("node", v.O_METHOD_PTYPE_NODE), ("layer", v.O_METHOD_PTYPE_LAYER)] avatar.Scripting.method_create("Run", params, cb_run) avatar.Scripting.method_create("Repeat", params, cb_repeat) avatar.Scripting.method_create("Kill", params, cb_kill) avatar.Scripting.method_create("AutoRun", params, cb_autorun) print "--- Accepting Scripts ..." timer = time.time() while session.connected(): # run scripts set to repeat if time.time() - 1.0 > timer: for script in scripts: run_script(script[0], script[1]) timer = time.time() session.update() # never reached session.connect_terminate()