Search in sources :

Example 1 with PythonInterpreter

use of org.python.util.PythonInterpreter in project oxCore by GluuFederation.

the class PythonService method initPythonInterpreter.

/*
	 * Initialize singleton instance during startup
	 */
public boolean initPythonInterpreter(String pythonModulesDir) {
    boolean result = false;
    if (isInitInterpreter()) {
        try {
            PythonInterpreter.initialize(getPreProperties(), getPostProperties(pythonModulesDir), null);
            this.pythonInterpreter = new PythonInterpreter();
            // Init output redirect for all new interpreters
            this.pythonInterpreter.setOut(new PythonLoggerOutputStream(log, false));
            this.pythonInterpreter.setErr(new PythonLoggerOutputStream(log, true));
            result = true;
        } catch (PyException ex) {
            log.error("Failed to initialize PythonInterpreter correctly", ex);
        } catch (Exception ex) {
            log.error("Failed to initialize PythonInterpreter correctly", ex);
        }
    }
    this.interpereterReady = result;
    return result;
}
Also used : PythonInterpreter(org.python.util.PythonInterpreter) PyException(org.python.core.PyException) PythonException(org.xdi.exception.PythonException) IOException(java.io.IOException) PyException(org.python.core.PyException)

Example 2 with PythonInterpreter

use of org.python.util.PythonInterpreter in project JMRI by JMRI.

the class JmriScriptEngineManager method initializePython.

/**
     * The Python ScriptEngine can be configured using a custom
     * python.properties file and will run jmri_defaults.py if found in the
     * user's configuration profile or settings directory. See python.properties
     * in the JMRI installation directory for details of how to configure the
     * Python ScriptEngine.
     */
public void initializePython() {
    if (!this.engines.containsKey(PYTHON)) {
        // Get properties for interpreter
        // Search in user files, the settings directory, and in the program path
        InputStream is = FileUtil.findInputStream("python.properties", new String[] { FileUtil.getUserFilesPath(), FileUtil.getPreferencesPath(), FileUtil.getProgramPath() });
        boolean execJython = false;
        if (is != null) {
            Properties properties;
            try {
                properties = new Properties(System.getProperties());
                // NOI18N
                properties.setProperty("python.console.encoding", "UTF-8");
                // NOI18N
                properties.setProperty("python.cachedir", FileUtil.getAbsoluteFilename(properties.getProperty("python.cachedir", "settings:jython/cache")));
                properties.load(is);
                String path = properties.getProperty("python.path", "");
                if (path.length() != 0) {
                    path = path.concat(File.pathSeparator);
                }
                properties.setProperty("python.path", path.concat(FileUtil.getScriptsPath().concat(File.pathSeparator).concat(FileUtil.getAbsoluteFilename("program:jython"))));
                execJython = Boolean.valueOf(properties.getProperty("jython.exec", Boolean.toString(false)));
            } catch (IOException ex) {
                log.error("Found, but unable to read python.properties: {}", ex.getMessage());
                properties = null;
            }
            PySystemState.initialize(null, properties);
            log.debug("Jython path is {}", PySystemState.getBaseProperties().getProperty("python.path"));
        }
        // Create the interpreter
        try {
            log.debug("create interpreter");
            ScriptEngine python = this.manager.getEngineByName(PYTHON);
            python.setContext(this.context);
            is = FileUtil.findInputStream(JYTHON_DEFAULTS, new String[] { FileUtil.getUserFilesPath(), FileUtil.getPreferencesPath() });
            if (execJython) {
                this.jython = new PythonInterpreter();
            }
            if (is != null) {
                python.eval(new InputStreamReader(is));
                if (this.jython != null) {
                    this.jython.execfile(is);
                }
            }
            this.engines.put(PYTHON, python);
        } catch (ScriptException e) {
            log.error("Exception creating jython system objects", e);
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) PythonInterpreter(org.python.util.PythonInterpreter) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) ScriptEngine(javax.script.ScriptEngine)

Example 3 with PythonInterpreter

use of org.python.util.PythonInterpreter in project bamboobsc by billchen198318.

the class ScriptExpressionUtils method executeJython.

private static void executeJython(String scriptExpression, Map<String, Object> results, Map<String, Object> parameters) throws Exception {
    //PythonInterpreter pyInterpreter = new PythonInterpreter();
    PythonInterpreter pyInterpreter = buildPythonInterpreter(null, true);
    if (parameters != null) {
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            pyInterpreter.set(entry.getKey(), entry.getValue());
        }
    }
    pyInterpreter.exec(scriptExpression);
    if (results != null) {
        for (Map.Entry<String, Object> entry : results.entrySet()) {
            entry.setValue(pyInterpreter.get(entry.getKey()));
        }
    }
}
Also used : PythonInterpreter(org.python.util.PythonInterpreter) PyObject(org.python.core.PyObject) Map(java.util.Map)

Example 4 with PythonInterpreter

use of org.python.util.PythonInterpreter in project spring-security by spring-projects.

the class PythonInterpreterPreInvocationAdvice method before.

public boolean before(Authentication authentication, MethodInvocation mi, PreInvocationAttribute preAttr) {
    PythonInterpreterPreInvocationAttribute pythonAttr = (PythonInterpreterPreInvocationAttribute) preAttr;
    String script = pythonAttr.getScript();
    PythonInterpreter python = new PythonInterpreter();
    python.set("authentication", authentication);
    python.set("args", createArgumentMap(mi));
    python.set("method", mi.getMethod().getName());
    Resource scriptResource = new PathMatchingResourcePatternResolver().getResource(script);
    try {
        python.execfile(scriptResource.getInputStream());
    } catch (IOException e) {
        throw new IllegalArgumentException("Couldn't run python script, " + script, e);
    }
    PyObject allowed = python.get("allow");
    if (allowed == null) {
        throw new IllegalStateException("Python script did not set the permit flag");
    }
    return (Boolean) Py.tojava(allowed, Boolean.class);
}
Also used : PythonInterpreter(org.python.util.PythonInterpreter) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PyObject(org.python.core.PyObject)

Example 5 with PythonInterpreter

use of org.python.util.PythonInterpreter in project Orchid by JavaEden.

the class CodeFilter method apply.

public Object apply(String input, String language) {
    PythonInterpreter interpreter = new PythonInterpreter();
    String pythonScript = "from pygments import highlight\n" + "from pygments.lexers import get_lexer_by_name\n" + "from pygments.formatters import HtmlFormatter\n" + "\n" + "lexer = get_lexer_by_name(codeLanguage, stripall=True)\n" + "formatter = HtmlFormatter(linenos=True)\n" + "\nresult = highlight(code, lexer, formatter)";
    // Set a variable with the content you want to work with
    interpreter.set("code", input);
    interpreter.set("codeLanguage", ((!EdenUtils.isEmpty(language)) ? language : "java"));
    // Simple use Pygments as you would in Python
    interpreter.exec(pythonScript);
    return interpreter.get("result", String.class);
}
Also used : PythonInterpreter(org.python.util.PythonInterpreter)

Aggregations

PythonInterpreter (org.python.util.PythonInterpreter)5 IOException (java.io.IOException)3 PyObject (org.python.core.PyObject)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptException (javax.script.ScriptException)1 PyException (org.python.core.PyException)1 Resource (org.springframework.core.io.Resource)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1 PythonException (org.xdi.exception.PythonException)1