← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #124759]: Can you run Python and call Sikuli from there? --- use XML-RPC

 

Question #124759 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/124759

Michael Wei posted a new comment:
Sure, some codes is from net user, hope someone could get better idea from it.
Copied here, please save it to c:\InteractiveConsole.py and run it in command (for windows)
>>>java -cp "%SIKULI_HOME%\sikuli-script.jar" org.python.util.jython c:\InteractiveConsole.py


#! /usr/bin/env python
#coding=utf-8
import sys, time
from SimpleXMLRPCServer import SimpleXMLRPCServer as Server
from code import InteractiveConsole
#from sikuli.Sikuli import *

#java -jar c:/sikuli-script.jar -s c:\InteractiveConsole.py
#java -cp c:/sikuli-script.jar org.python.util.jython c:\InteractiveConsole.py

class FileCacher:
    "Cache the stdout text so we can analyze it before returning it"
    def __init__(self): self.reset()
    def reset(self): self.out = []
    def write(self,line): self.out.append(line)
    def flush(self):
        output = ''.join(self.out)
        self.reset()
        return output

class Shell(InteractiveConsole):
    "Wrapper around Python that can filter input/output to the shell"
    def __init__(self):
        self.stdout = sys.stdout
        self.stderr = sys.stderr
        self.cache = FileCacher()
        InteractiveConsole.__init__(self)
        return

    def get_output(self): sys.stdout = self.cache  
    def get_errput(self): sys.stderr = self.cache   
    def return_output(self): sys.stdout = self.stdout
    def return_errput(self): sys.stderr = self.stderr

    def push(self,line):
        self.get_output()
        self.get_errput()

        # you can filter input here by doing something like
        # line = filter(line)
        InteractiveConsole.push(self,line)

        self.return_output()
        self.return_errput()
        
        output = self.cache.flush()
        # you can filter the output here by doing something like
        # output = filter(output)
        return output
    
def runcmd(str): 
    print str    
    return sh.push(str)

if __name__ == '__main__':
    
    server_name = "127.0.0.1"
    server_port = 1337
    global sh
    
    srv = Server((server_name, server_port)) # as an example on the same machine

    #print the init information
    if not srv:
        print "Fail to start SimpleXMLRPCServer %s:%s" %(server_name, server_port)
        exit(1)
    else:
        print "Started SimpleXMLRPCServer: %s:%s" %(server_name, server_port)
        print time.ctime()+"\n"


    sh = Shell()
    runcmd("from sikuli.Sikuli import *") 
    
    srv.register_function(runcmd)
    srv.serve_forever()

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.