Search in sources :

Example 6 with PyList

use of org.python.core.PyList in project org.csstudio.display.builder by kasemir.

the class JythonScriptSupport method init.

/**
 * Perform static, one-time initialization
 */
private static boolean init() {
    try {
        final Properties pre_props = System.getProperties();
        final Properties props = new Properties();
        // Locate the jython plugin for 'home' to allow use of /Lib in there
        final String home = getPluginPath("org.python.jython", "/");
        if (home == null)
            throw new Exception("Cannot locate jython bundle. No OSGi?");
        // Jython 2.7(b3) needs these to set sys.prefix and sys.executable.
        // If left undefined, initialization of Lib/site.py fails with
        // posixpath.py", line 394, in normpath AttributeError:
        // 'NoneType' object has no attribute 'startswith'
        props.setProperty("python.home", home);
        props.setProperty("python.executable", "None");
        // Disable cachedir to avoid creation of cachedir folder.
        // See http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html#java-package-scanning
        // and http://wiki.python.org/jython/PackageScanning
        props.setProperty(PySystemState.PYTHON_CACHEDIR_SKIP, "true");
        // With python.home defined, there is no more
        // "ImportError: Cannot import site module and its dependencies: No module named site"
        // Skipping the site import still results in faster startup
        props.setProperty("python.import.site", "false");
        // Prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
        props.setProperty("python.console.encoding", "UTF-8");
        // This will replace entries found on JYTHONPATH
        final String python_path = Preferences.getPythonPath();
        if (!python_path.isEmpty())
            props.setProperty("python.path", python_path);
        // Options: error, warning, message (default), comment, debug
        // props.setProperty("python.verbose", "debug");
        // Options.verbose = Py.DEBUG;
        PythonInterpreter.initialize(pre_props, props, new String[0]);
        final PyList paths = Py.getSystemState().path;
        paths.add(getPluginPath("org.csstudio.display.builder.runtime", "scripts"));
        return true;
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Once this worked OK, but now the Jython initialization failed. Don't you hate computers?", ex);
    }
    return false;
}
Also used : PyList(org.python.core.PyList) Properties(java.util.Properties) IOException(java.io.IOException)

Aggregations

PyList (org.python.core.PyList)6 PyString (org.python.core.PyString)3 PyObject (org.python.core.PyObject)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 Bindings (javax.script.Bindings)1 SimpleBindings (javax.script.SimpleBindings)1 Schema (org.apache.avro.Schema)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 PyTuple (org.python.core.PyTuple)1 JsonGenericRecordConverter (org.talend.daikon.avro.converter.JsonGenericRecordConverter)1 JsonSchemaInferrer (org.talend.daikon.avro.inferrer.JsonSchemaInferrer)1