org.python.util.PythonInterpreter Java Examples

The following examples show how to use org.python.util.PythonInterpreter. 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: InterpreterUtils.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private static synchronized PythonInterpreter initPythonInterpreter(String[] args, String pythonPath, String scriptName) {
	if (!jythonInitialized) {
		// the java stack traces within the jython runtime aren't useful for users
		System.getProperties().put("python.options.includeJavaStackInExceptions", "false");
		PySystemState.initialize(System.getProperties(), new Properties(), args);

		pythonInterpreter = new PythonInterpreter();

		pythonInterpreter.getSystemState().path.add(0, pythonPath);

		pythonInterpreter.setErr(System.err);
		pythonInterpreter.setOut(System.out);

		pythonInterpreter.exec("import " + scriptName);
		jythonInitialized = true;
	}
	return pythonInterpreter;
}
 
Example #2
Source File: JythonAnnotator.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(UimaContext context)
		throws ResourceInitializationException {
	super.initialize(context);

	interp = new PythonInterpreter(null, new PySystemState());
	// LATER allow for nonstandard Python modules
	// PySystemState sys = Py.getSystemState();
	// sys.path.append(new PyString(rootPath));
	// sys.path.append(new PyString(modulesDir));

	try {
		scriptFileIs = ResourceHelper.getInputStream(scriptPath);
	} catch (Exception e) {
		throw new ResourceInitializationException(
				ResourceInitializationException.NO_RESOURCE_FOR_PARAMETERS,
				new Object[] { scriptPath });
	}
}
 
Example #3
Source File: JythonServer.java    From floodlight_with_topoguard with Apache License 2.0 6 votes vote down vote up
/**
 * The main thread for this class invoked by Thread.run()
 *
 * @see java.lang.Thread#run()
 */
public void run() {
    PythonInterpreter p = new PythonInterpreter();
    for (String name : this.locals.keySet()) {
        p.set(name, this.locals.get(name));
    }

    URL jarUrl = JythonServer.class.getProtectionDomain().getCodeSource().getLocation();
    String jarPath = jarUrl.getPath();
    if (jarUrl.getProtocol().equals("file")) {
        // If URL is of type file, assume that we are in dev env and set path to python dir.
        // else use the jar file as is
        jarPath = jarPath + "../../src/main/python/";
    }

    p.exec("import sys");
    p.exec("sys.path.append('" + jarPath + "')");
    p.exec("from debugserver import run_server");
    if (this.host == null) {
    	p.exec("run_server(port=" + this.port + ", locals=locals())");
    } else {
    	p.exec("run_server(port=" + this.port + ", host='" + this.host + "', locals=locals())");
    }
}
 
Example #4
Source File: ScriptRunner.java    From AndroidRobot with Apache License 2.0 6 votes vote down vote up
private void initPython(String executablePath, Collection<String> pythonPath, String[] argv)
/*     */   {
/* 160 */     Properties props = new Properties();
/*     */ 
/* 163 */     StringBuilder sb = new StringBuilder();
/* 164 */     sb.append(System.getProperty("java.class.path"));
/* 165 */     for (String p : pythonPath) {
/* 166 */       sb.append(":").append(p);
/*     */     }
/* 168 */     props.setProperty("python.path", sb.toString());
/*     */ 
/* 173 */     props.setProperty("python.verbose", "error");
/*     */ 
/* 176 */     props.setProperty("python.executable", executablePath);
/*     */ 
/* 178 */     PythonInterpreter.initialize(System.getProperties(), props, argv);

/*     */     
/* 180 */     //String frameworkDir = System.getProperty("java.ext.dirs");
/* 181 */     //File monkeyRunnerJar = new File(frameworkDir, "monkeyrunner.jar");
/* 182 */     //if (monkeyRunnerJar.canRead())
/* 183 */      // PySystemState.packageManager.addJar(monkeyRunnerJar.getAbsolutePath(), false);
/*     */   }
 
Example #5
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 #6
Source File: JythonSupport.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
/** Initialize
 *  @throws Exception on error
 */
public JythonSupport() throws Exception
{
    final PySystemState state = new PySystemState();

    // Creating a PythonInterpreter is very slow.
    //
    // In addition, concurrent creation is not supported, resulting in
    //     Lib/site.py", line 571, in <module> ..
    //     Lib/sysconfig.py", line 159, in _subst_vars AttributeError: {'userbase'}
    // or  Lib/site.py", line 122, in removeduppaths java.util.ConcurrentModificationException
    //
    // Sync. on JythonSupport to serialize the interpreter creation and avoid above errors.
    // Curiously, this speeds the interpreter creation up,
    // presumably because they're not concurrently trying to access the same resources?
    synchronized (JythonSupport.class)
    {
        interpreter = new PythonInterpreter(null, state);
    }
}
 
Example #7
Source File: JythonTest.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testPathWithThreads() throws Exception
{
    // Initialize on some other thread
    executor.submit(() -> init()).get();

    for (int i=0; i<RUNTIME_SECONDS; ++i)
    {
        final ExecutorService new_executor = Executors.newSingleThreadExecutor();
        new_executor.submit(() ->
        {
            final PythonInterpreter python = createInterpreter();
            try
            {
                testInterpreter(python);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            python.close();
        }).get();
        new_executor.shutdown();
    }
}
 
Example #8
Source File: JythonTest.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
private PythonInterpreter createInterpreter()
{
    // System.out.println("Creating interpreter on " + Thread.currentThread().getName());

    final PySystemState state = new PySystemState();
    // Check for default settings
    if (! state.path.toString().contains("always"))
    {
        System.out.println("Not running in fresh VM, missing /tmp/always in " + state.path);
        state.path.add(0, "/tmp/always");
    }
    assertThat(state.path.toString(), not(containsString("special")));
    // Add settings specific to this interpreter
    state.path.add(0, "/tmp/special");
    return new PythonInterpreter(null, state);
}
 
Example #9
Source File: JythonTest.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
private void init()
{
    // System.out.println("Initializing on " + Thread.currentThread().getName());
    final Properties pre_props = System.getProperties();
    final Properties props = new Properties();

    String home = PythonInterpreter.class
                                   .getProtectionDomain().getCodeSource().getLocation().toString();
    System.out.println("Jython home: " + home);
    assertThat(home, not(nullValue()));

    if (home.contains(".jar"))
        System.out.println("Jython provided as JAR");
    home = home.replace("file:", "");

    props.setProperty("python.home", home);
    // props.setProperty("python.executable", "None");
    props.setProperty(RegistryKey.PYTHON_CACHEDIR_SKIP, "true");
    props.setProperty("python.import.site", "false");
    // props.setProperty("python.console.encoding", "UTF-8");

    props.setProperty("python.path", "/tmp/always");
    // props.setProperty("python.verbose", "debug");
    // Options.verbose = Py.DEBUG;
    PythonInterpreter.initialize(pre_props, props, new String[0]);
}
 
Example #10
Source File: FlowPythonStrategy.java    From mcg-helper with Apache License 2.0 6 votes vote down vote up
public String resolve(String source, JSON param) throws Exception {
		String dataJson = null;
        
		final PythonInterpreter interpreter = JythonEnvironment.getInstance().getPythonInterpreter();
	    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
	    ScriptEngine engine = scriptEngineManager.getEngineByName("python");
		engine.eval(source);
	    Invocable invocable = (Invocable) engine;
	    PyDictionary pyDictionary = JSON.parseObject(JSON.toJSONString(param), PyDictionary.class);
	    Object result = invocable.invokeFunction("main", pyDictionary);
	    dataJson = JSON.toJSONString(result);
/*	    
		interpreter.exec(source);
		PyFunction function = (PyFunction) interpreter.get("main", PyFunction.class);
		PyObject pyobject = function.__call__(new PyString(JSON.toJSONString(param)));
		Object jot = (Object)pyobject.__tojava__(Object.class);
		dataJson = JSON.toJSONString(jot); 
        */
		interpreter.close();
	    return dataJson;
	}
 
Example #11
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 #12
Source File: FrmMain.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Load an application
 *
 * @param plugin Application
 */
public void loadApplication(Application plugin) {
    this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    try {
        PythonInterpreter interp = this.getConsoleDockable().getInterpreter();
        String path = plugin.getPath();
        interp.exec("import " + path);
        interp.exec("from " + path + ".loadApp import LoadApp");
        PyObject loadClass = interp.get("LoadApp");
        PyObject loadObj = loadClass.__call__();
        IPlugin instance = (IPlugin) loadObj.__tojava__(IPlugin.class);
        instance.setApplication(FrmMain.this);
        instance.setName(plugin.getName());
        plugin.setPluginObject(instance);
        plugin.setLoad(true);
        instance.load();
    } catch (Exception e) {
        e.printStackTrace();
    }
    this.setCursor(Cursor.getDefaultCursor());
}
 
Example #13
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 #14
Source File: ScriptExpressionUtils.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
public static PythonInterpreter buildPythonInterpreter(PyObject dist, boolean cleanup) {
	PythonInterpreter pyInterpreter = PythonInterpreter.threadLocalStateInterpreter(dist);
	if (cleanup) {
		pyInterpreter.cleanup();
	}
	return pyInterpreter;
}
 
Example #15
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
public void executeCommand(String cmd){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.exec(cmd); 
}
 
Example #16
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 #17
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 #18
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 #19
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 #20
Source File: ScriptRunner.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
public Map<String, PyObject> runStringAndGet(String executablePath, String script, Collection<String> names)
/*     */   {
/* 142 */     initPython(executablePath);
/* 143 */     PythonInterpreter python = new PythonInterpreter();
/* 144 */     python.exec(script);
/*     */ 
/* 146 */     ImmutableMap.Builder builder = ImmutableMap.builder();
/* 147 */     for (String name : names) {
/* 148 */       builder.put(name, python.get(name));
/*     */     }
/* 150 */     return builder.build();
/*     */   }
 
Example #21
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 #22
Source File: PythonProject.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private Map<String, Predicate<PythonInterpreter>> handlePlugins()
/*     */   {
/* 169 */     ImmutableMap.Builder builder = ImmutableMap.builder();
/* 170 */     for (File f : this.options.getPlugins()) {
/* 171 */       builder.put(f.getAbsolutePath(), handlePlugin(f));
/*     */     }
/* 173 */     return builder.build();
/*     */   }
 
Example #23
Source File: JPythonInterpreterDriver.java    From tn5250j with GNU General Public License v2.0 5 votes vote down vote up
public void executeScript(String script)
      throws InterpreterDriver.InterpreterException {
   try {
      _interpreter = new PythonInterpreter();
      _interpreter.exec(script);
   }
   catch (PyException ex) {
      throw new InterpreterDriver.InterpreterException(ex);
   }
}
 
Example #24
Source File: PythonExecutor.java    From score with Apache License 2.0 5 votes vote down vote up
protected PythonInterpreter initInterpreter(Set<String> dependencies) {
    logger.info("Creating python interpreter with [" + dependencies.size() + "] dependencies [" + dependencies + "]");
    if(!dependencies.isEmpty()) {
        PySystemState systemState = new PySystemState();
        for (String dependency: dependencies) {
            systemState.path.append(new PyString(dependency));
        }
        return new ThreadSafePythonInterpreter(systemState);
    }
    return GLOBAL_INTERPRETER;
}
 
Example #25
Source File: JythonScriptEngine.java    From spork with Apache License 2.0 5 votes vote down vote up
@Override
protected Map<String, List<PigStats>> main(PigContext pigContext, String scriptFile)
        throws IOException {
    if (System.getProperty(PySystemState.PYTHON_CACHEDIR_SKIP)==null)
        System.setProperty(PySystemState.PYTHON_CACHEDIR_SKIP, "false");
    
    PigServer pigServer = new PigServer(pigContext, false);

    // register dependencies
    String jythonJar = getJarPath(PythonInterpreter.class);
    if (jythonJar != null) {
        pigServer.registerJar(jythonJar);
    }

    File f = new File(scriptFile);

    if (!f.canRead()) {
        throw new IOException("Can't read file: " + scriptFile);
    }

    FileInputStream fis1 = new FileInputStream(scriptFile);
    try {
        if (hasFunction(fis1)) {
            registerFunctions(scriptFile, null, pigContext);
        }
    } finally {
        fis1.close();
    }

    Interpreter.setMain(true);
    FileInputStream fis = new FileInputStream(scriptFile);
    try {
        load(fis, scriptFile, pigServer.getPigContext());
    } finally {
        fis.close();
    }
    return getPigStatsMap();
}
 
Example #26
Source File: InterpreterUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the Jython interpreter and executes a python script.
 *
 * @param factory environment factory
 * @param scriptDirectory the directory containing all required user python scripts
 * @param scriptName the name of the main python script
 * @param args Command line arguments that will be delivered to the executed python script
 */
public static void initAndExecPythonScript(PythonEnvironmentFactory factory, java.nio.file.Path scriptDirectory, String scriptName, String[] args) {
	String[] fullArgs = new String[args.length + 1];
	fullArgs[0] = scriptDirectory.resolve(scriptName).toString();
	System.arraycopy(args, 0, fullArgs, 1, args.length);

	PythonInterpreter pythonInterpreter = initPythonInterpreter(fullArgs, scriptDirectory.toUri().getPath(), scriptName);

	pythonInterpreter.set("__flink_env_factory__", factory);
	pythonInterpreter.exec(scriptName + ".main(__flink_env_factory__)");
}
 
Example #27
Source File: JavaPythonInteropUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenPythonInterpreter_whenPrintExecuted_thenOutputDisplayed() {
    try (PythonInterpreter pyInterp = new PythonInterpreter()) {
        StringWriter output = new StringWriter();
        pyInterp.setOut(output);

        pyInterp.exec("print('Hello Baeldung Readers!!')");
        assertEquals("Should contain script output: ", "Hello Baeldung Readers!!", output.toString()
            .trim());
    }
}
 
Example #28
Source File: JavaPythonInteropUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenPythonInterpreter_whenNumbersAdded_thenOutputDisplayed() {
    try (PythonInterpreter pyInterp = new PythonInterpreter()) {
        pyInterp.exec("x = 10+10");
        PyObject x = pyInterp.get("x");
        assertEquals("x: ", 20, x.asInt());
    }
}
 
Example #29
Source File: JavaPythonInteropUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenPythonInterpreter_whenErrorOccurs_thenExceptionIsThrown() {
    thrown.expect(PyException.class);
    thrown.expectMessage("ImportError: No module named syds");

    try (PythonInterpreter pyInterp = new PythonInterpreter()) {
        pyInterp.exec("import syds");
    }
}
 
Example #30
Source File: PyInterpreterPool.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public void release(PythonInterpreter interpreter){
    // Clean the interpreter by setting the user defined instances to null
    PyObject locals = interpreter.getLocals();
    List<String> interpreterVars = new ArrayList<String>();
    for (PyObject item : locals.__iter__().asIterable()) {
        interpreterVars.add(item.toString());
    }
    for (String varName : interpreterVars) {
        if(varName.equals(NAME_VAR_STR) || varName.equals(DOC_VAR_STR)) continue;
        interpreter.set(varName, null);
    }
    pool.add(interpreter);
}