Java Code Examples for org.python.util.PythonInterpreter#execfile()

The following examples show how to use org.python.util.PythonInterpreter#execfile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XunfengInner.java    From TrackRay with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void process() {

    try {
        PythonInterpreter interpreter = python.interpreter();
        interpreter.execfile(filename);
        PyFunction check = interpreter.get("check", PyFunction.class);

        PyObject check_call = check.__call__(new PyString(ip), new PyInteger(port), new PyInteger(timeout));

        String result = check_call.toString();
        if (result!=null &&
                !StringUtils.contains(result,"None")
                && !StringUtils.contains(result,"False")) {

            //PyObject get_plugin_info = interpreter.get("get_plugin_info").__call__();
            //Map map = (Map) get_plugin_info.getDict().__tojava__(Map.class);
            this.result = result;
            return;
        }
    }catch (Exception e){
        log.error(e.toString());
    }

    result="";
}
 
Example 2
Source File: Xunfeng.java    From TrackRay with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean check(Map param) {
    if (XunfengInner.init_state)
        return true;
    Python python = xunfengInner.getPython();
    println("正在初始化Python引擎");
    PythonInterpreter interpreter = python.interpreter();
    File pluginDir = new File(XunfengInner.PLUGIN_PATH);
    println("正在初始化插件");
    for (File plugin : pluginDir.listFiles()) {
        try {
            sendColorMsg(Message.GREEN(plugin.getName()+":初始化中"));
            interpreter.execfile(plugin.getCanonicalPath());
        } catch (IOException e) {
            sendColorMsg(Message.RED(plugin.getName()+":初始化失败"));
        }
    }
    XunfengInner.init_state = true;
    println("插件初始化结束");
    return true;
}
 
Example 3
Source File: JythonTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private void runTestOnce(String testScript, PythonInterpreter interp, int invocationsSoFar)
{
    try
    {
        interp.execfile(testScript);
    }
    catch (PyException e)
    {
        if( e.type.toString().equals("<type 'exceptions.SystemExit'>") && e.value.toString().equals("0") )
        {
            // Build succeeded.
        }
        else
        {
            if (LOGGER.isLoggable(Level.FINE))
            {
                LOGGER.log(Level.FINE, "Jython interpreter failed. Test failures?", e);
            }

            // This unusual code is necessary because PyException toString() contains the useful Python traceback
            // and getMessage() is usually null
            fail("Caught PyException on invocation number " + invocationsSoFar + ": " + e.toString() + " with message: " + e.getMessage());
        }
    }
}
 
Example 4
Source File: JIntrospect.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Return list of token for command.
 * @param command The command
 * @return Token list
 * @throws java.io.UnsupportedEncodingException
 */
public PyList getTokens(String command) throws UnsupportedEncodingException {
    StringBuilder sb = new StringBuilder();
    sb.append("import cStringIO");
    sb.append("\n");
    sb.append("import tokenize");
    sb.append("\n");
    sb.append("command = str('");
    sb.append(command);
    sb.append("')");
    sb.append("\n");
    sb.append("f = cStringIO.StringIO(command)");
    sb.append("\n");
    sb.append("tokens = []");
    sb.append("\n");
    sb.append("def eater(*args):");
    sb.append("\n");
    sb.append("    tokens.append(args)");
    sb.append("\n");
    sb.append("tokenize.tokenize_loop(f.readline, eater)");
    sb.append("\n");
    String code = sb.toString();
    String encoding = "utf-8";
    PythonInterpreter pi = new PythonInterpreter();
    try {
        pi.execfile(new ByteArrayInputStream(code.getBytes(encoding)));
        PyList tokens = (PyList)pi.get("tokens");            
        return tokens;
    } catch (Exception e){
        return null;
    }
}
 
Example 5
Source File: MeteoInfoMap.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void runScript(String args[], String fn, int idx) {
    String ext = GlobalUtil.getFileExtension(fn);
    System.out.println("Running Jython script...");
    //PySystemState state = Py.getSystemState();
    //Py.getSystemState().setdefaultencoding("utf-8");
    PySystemState state = new PySystemState();
    //state.setdefaultencoding("utf-8");
    if (args.length > idx + 1) {
        for (int i = idx + 1; i < args.length; i++) {
            state.argv.append(new PyString(args[i]));
        }
    }

    PythonInterpreter interp = new PythonInterpreter(null, state);
    String pluginPath = GlobalUtil.getAppPath(FrmMain.class) + File.separator + "plugins";
    List<String> jarfns = GlobalUtil.getFiles(pluginPath, ".jar");
    String path = GlobalUtil.getAppPath(FrmMain.class) + File.separator + "pylib";
    interp.exec("import sys");
    //interp.set("mis", mis);
    interp.exec("sys.path.append('" + path + "')");
    //interp.exec("import mipylib");
    //interp.exec("from mipylib.miscript import *");
    //interp.exec("from meteoinfo.numeric.JNumeric import *");
    for (String jarfn : jarfns) {
        interp.exec("sys.path.append('" + jarfn + "')");
    }
    interp.execfile(fn);
    System.exit(0);
}
 
Example 6
Source File: JIntrospect.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Return list of token for command.
 * @param command The command
 * @return Token list
 * @throws java.io.UnsupportedEncodingException
 */
public PyList getTokens(String command) throws UnsupportedEncodingException {
    StringBuilder sb = new StringBuilder();
    sb.append("import cStringIO");
    sb.append("\n");
    sb.append("import tokenize");
    sb.append("\n");
    sb.append("command = str('");
    sb.append(command);
    sb.append("')");
    sb.append("\n");
    sb.append("f = cStringIO.StringIO(command)");
    sb.append("\n");
    sb.append("tokens = []");
    sb.append("\n");
    sb.append("def eater(*args):");
    sb.append("\n");
    sb.append("    tokens.append(args)");
    sb.append("\n");
    sb.append("tokenize.tokenize_loop(f.readline, eater)");
    sb.append("\n");
    String code = sb.toString();
    String encoding = "utf-8";
    PythonInterpreter pi = new PythonInterpreter();
    try {
        pi.execfile(new ByteArrayInputStream(code.getBytes(encoding)));
        PyList tokens = (PyList)pi.get("tokens");            
        return tokens;
    } catch (Exception e){
        return null;
    }
}
 
Example 7
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
/**
 * 执行指定的python文件
 * @param filePath
 * @return
 */
private PyObject executePython(String filePath){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyObject ret = interpreter.eval("True");
	return ret;
}
 
Example 8
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__();
	return pyobj;
}
 
Example 9
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function, PyObject params){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__(params.__getitem__(0));
	return pyobj;
}
 
Example 10
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function, PyObject param0, PyObject param1){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__(param0, param1);
	return pyobj;
}
 
Example 11
Source File: GameScript.java    From marauroa with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor (singleton)
 *
 * @throws Exception
 */
private GameScript() throws Exception {
	conf = Configuration.getConfiguration();
	interpreter = new PythonInterpreter();
	interpreter.execfile(conf.get("python_script"));
}