Search in sources :

Example 36 with PyObject

use of org.python.core.PyObject in project score by CloudSlang.

the class EmbeddedPythonExecutorWrapper method processExecResults.

private PythonExecutionResult processExecResults() {
    Iterator<PyObject> localsIterator = pythonInterpreter.getLocals().asIterable().iterator();
    Map<String, Serializable> returnValue = new HashMap<>();
    while (localsIterator.hasNext()) {
        PyObject next = localsIterator.next();
        String key = next.asString();
        PyObject value = pythonInterpreter.get(key);
        if (!isLocalEntryExcluded(key, value)) {
            returnValue.put(key, resolveJythonObjectToJavaForExec(value, key));
        }
    }
    return new PythonExecutionResult(returnValue);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) PyString(org.python.core.PyString) PyObject(org.python.core.PyObject) PythonExecutionResult(io.cloudslang.runtime.api.python.PythonExecutionResult)

Example 37 with PyObject

use of org.python.core.PyObject in project Pydev by fabioz.

the class JythonModules method makeISort.

public static String makeISort(String fileContents, File f, Set<String> knownThirdParty) {
    IPythonInterpreter iPythonInterpreter = iSortThreadLocalInterpreter.get();
    IPythonInterpreter interpreter;
    String outputLine = "output = getattr(isort.SortImports(file_contents=fileContents, settings_path=settingsPath, known_third_party=knownThirdParty), 'output', None)\n";
    if (iPythonInterpreter == null) {
        // The first call may be slow because doing the imports is slow, but subsequent calls should be
        // fast as we'll be reusing the same interpreter.
        String s = "" + "import sys\n" + "import os\n" + "add_to_pythonpath = '%s'\n" + "os.chdir(add_to_pythonpath)\n" + "if add_to_pythonpath not in sys.path:\n" + "    sys.path.append(add_to_pythonpath)\n" + "import isort\n" + outputLine;
        boolean useConsole = false;
        interpreter = JythonPlugin.newPythonInterpreter(useConsole, false);
        String isortContainerLocation = null;
        try {
            isortContainerLocation = CorePlugin.getScriptWithinPySrc(new Path("third_party").append("isort_container").toString()).toString();
            File isortContainer = new File(isortContainerLocation);
            if (!isortContainer.exists()) {
                Log.log("Specified location for isort_container does not exist (" + isortContainerLocation + ").");
                return null;
            }
        } catch (CoreException e) {
            Log.log("Error getting isort_container location", e);
            return null;
        }
        interpreter.set("fileContents", fileContents);
        if (f != null) {
            interpreter.set("settingsPath", f.getAbsoluteFile().getParent());
        } else {
            interpreter.set("settingsPath", "");
        }
        interpreter.set("knownThirdParty", new PyList(knownThirdParty));
        s = StringUtils.format(s, StringUtils.replaceAllSlashes(isortContainerLocation));
        interpreter.exec(s);
        iSortThreadLocalInterpreter.set(interpreter);
    } else {
        interpreter = iPythonInterpreter;
        // Found interpreter in thread local storage, just use it to do the sort.
        interpreter.set("fileContents", fileContents);
        if (f != null) {
            interpreter.set("settingsPath", f.getAbsoluteFile().getParent());
        } else {
            interpreter.set("settingsPath", "");
        }
        interpreter.set("knowhThirdParty", new PyList(knownThirdParty));
        // Note that we have to clear the global caches that isort has for it to reload the settings (otherwise,
        // eclipse needs to be restarted just to get the updated caches).
        interpreter.exec("" + "isort.settings._get_config_data.cache_clear()\n" + "isort.settings.from_path.cache_clear()\n" + outputLine);
    }
    PyObject pyObject = (PyObject) interpreter.get("output");
    if (pyObject != null && pyObject.__nonzero__()) {
        return pyObject.toString();
    }
    return null;
}
Also used : IPythonInterpreter(org.python.pydev.shared_core.jython.IPythonInterpreter) Path(org.eclipse.core.runtime.Path) CoreException(org.eclipse.core.runtime.CoreException) PyList(org.python.core.PyList) File(java.io.File) PyObject(org.python.core.PyObject)

Example 38 with PyObject

use of org.python.core.PyObject in project Pydev by fabioz.

the class JythonPlugin method exec.

/**
 * @param pythonpathFolders folders that should be in the pythonpath when executing the script
 * @see JythonPlugin#exec(HashMap, String, PythonInterpreter)
 * Same as before but the file to execute is passed as a parameter
 */
public static Throwable exec(HashMap<String, Object> locals, IPythonInterpreter interpreter, File fileToExec, File[] pythonpathFolders, String... argv) {
    if (locals == null) {
        locals = new HashMap<String, Object>();
    }
    if (interpreter == null) {
        // already disposed
        return null;
    }
    locals.put("__file__", fileToExec.toString());
    try {
        String codeObjName;
        synchronized (codeCache) {
            // hold on there... one at a time... please?
            String fileName = fileToExec.getName();
            if (!fileName.endsWith(".py")) {
                throw new RuntimeException("The script to be executed must be a python file. Name:" + fileName);
            }
            codeObjName = "code" + fileName.substring(0, fileName.indexOf('.'));
            final String codeObjTimestampName = codeObjName + "Timestamp";
            for (Map.Entry<String, Object> entry : locals.entrySet()) {
                interpreter.set(entry.getKey(), entry.getValue());
            }
            boolean regenerate = false;
            if (interpreter instanceof PythonInterpreterWrapperNotShared) {
                // Always regenerate if the state is not shared! (otherwise the pythonpath might be wrong as the sys is not the same)
                regenerate = true;
            }
            Tuple<Long, Object> timestamp = codeCache.get(fileToExec);
            final long lastModified = FileUtils.lastModified(fileToExec);
            if (timestamp == null || timestamp.o1 != lastModified) {
                // the file timestamp changed, so, we have to regenerate it
                regenerate = true;
            }
            if (!regenerate) {
                // if the 'code' object does not exist or if it's timestamp is outdated, we have to re-set it.
                PyObject obj = (PyObject) interpreter.get(codeObjName);
                PyObject pyTime = (PyObject) interpreter.get(codeObjTimestampName);
                if (obj == null || pyTime == null || !pyTime.__tojava__(Long.class).equals(timestamp.o1)) {
                    if (DEBUG) {
                        System.out.println("Resetting object: " + codeObjName);
                    }
                    interpreter.set(codeObjName, timestamp.o2);
                    interpreter.set(codeObjTimestampName, timestamp.o1);
                }
            }
            if (regenerate) {
                if (DEBUG) {
                    System.out.println("Regenerating: " + codeObjName);
                }
                String path = FileUtils.getFileAbsolutePath(fileToExec);
                StringBuffer strPythonPathFolders = new StringBuffer();
                strPythonPathFolders.append("[");
                for (File file : pythonpathFolders) {
                    if (file != null) {
                        strPythonPathFolders.append("r'");
                        strPythonPathFolders.append(FileUtils.getFileAbsolutePath(file));
                        strPythonPathFolders.append("',");
                    }
                }
                strPythonPathFolders.append("]");
                StringBuffer addToSysPath = new StringBuffer();
                // we will only add the paths to the pythonpath if it was still not set or if it changed (but it will never remove the ones added before).
                // we have to put that in sys because it is the same across different interpreters
                addToSysPath.append("if not hasattr(sys, 'PYDEV_PYTHONPATH_SET') or sys.PYDEV_PYTHONPATH_SET != ");
                addToSysPath.append(strPythonPathFolders);
                addToSysPath.append(":\n");
                addToSysPath.append("    sys.PYDEV_PYTHONPATH_SET = ");
                addToSysPath.append(strPythonPathFolders);
                addToSysPath.append("\n");
                addToSysPath.append("    sys.path += ");
                addToSysPath.append(strPythonPathFolders);
                addToSysPath.append("\n");
                if (argv.length > 0) {
                    addToSysPath.append("sys.argv = [");
                    for (String s : argv) {
                        addToSysPath.append(s);
                        addToSysPath.append(",");
                    }
                    addToSysPath.append("];");
                    addToSysPath.append("\n");
                }
                String toExec = StringUtils.format(LOAD_FILE_SCRIPT, path, path, addToSysPath.toString());
                interpreter.exec(toExec);
                String exec = StringUtils.format("%s = compile(toExec, r'%s', 'exec')", codeObjName, path);
                interpreter.exec(exec);
                // set its timestamp
                interpreter.set(codeObjTimestampName, lastModified);
                Object codeObj = interpreter.get(codeObjName);
                codeCache.put(fileToExec, new Tuple<Long, Object>(lastModified, codeObj));
            }
        }
        interpreter.exec(StringUtils.format("exec(%s)", codeObjName));
    } catch (Throwable e) {
        if (!IN_TESTS && JythonPlugin.getDefault() == null) {
            // it is already disposed
            return null;
        }
        // the user requested it to exit
        if (e instanceof ExitScriptException) {
            return null;
        }
        // actually, this is more likely to happen when raising an exception in jython
        if (e instanceof PyException) {
            PyException pE = (PyException) e;
            if (pE.type instanceof PyJavaType) {
                PyJavaType t = (PyJavaType) pE.type;
                if (t.getName() != null && t.getName().indexOf("ExitScriptException") != -1) {
                    return null;
                }
            } else if (pE.type instanceof PyClass) {
                PyClass t = (PyClass) pE.type;
                if (t.__name__ != null && t.__name__.equals("SystemExit")) {
                    return null;
                }
            }
        }
        if (JyScriptingPreferencesPage.getShowScriptingOutput()) {
            Log.log(IStatus.ERROR, "Error while executing:" + fileToExec, e);
        }
        return e;
    }
    return null;
}
Also used : PyClass(org.python.core.PyClass) PyException(org.python.core.PyException) PyObject(org.python.core.PyObject) HashMap(java.util.HashMap) Map(java.util.Map) PyObject(org.python.core.PyObject) File(java.io.File) PyJavaType(org.python.core.PyJavaType)

Example 39 with PyObject

use of org.python.core.PyObject in project oxCore by GluuFederation.

the class PythonService method loadPythonScript.

@SuppressWarnings("unchecked")
private <T> T loadPythonScript(String scriptPythonType, Class<T> scriptJavaType, PyObject[] constructorArgs, PythonInterpreter interpreter) throws PythonException {
    PyObject scriptPythonTypeObject = interpreter.get(scriptPythonType);
    if (scriptPythonTypeObject == null) {
        return null;
    }
    PyObject scriptPythonTypeClass;
    try {
        scriptPythonTypeClass = scriptPythonTypeObject.__call__(constructorArgs);
    } catch (Exception ex) {
        log.error("Failed to initialize python class", ex.getMessage());
        throw new PythonException(String.format("Failed to initialize python class '%s'", scriptPythonType), ex);
    }
    Object scriptJavaClass = scriptPythonTypeClass.__tojava__(scriptJavaType);
    if (!ReflectHelper.assignableFrom(scriptJavaClass.getClass(), scriptJavaType)) {
        return null;
    }
    return (T) scriptJavaClass;
}
Also used : PythonException(org.gluu.exception.PythonException) PyObject(org.python.core.PyObject) PyObject(org.python.core.PyObject) IOException(java.io.IOException) PythonException(org.gluu.exception.PythonException) PyException(org.python.core.PyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 40 with PyObject

use of org.python.core.PyObject in project oxCore by GluuFederation.

the class CustomScriptManager method createExternalTypeFromStringWithPythonException.

public BaseExternalType createExternalTypeFromStringWithPythonException(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) throws Exception {
    String script = customScript.getScript();
    String scriptName = StringHelper.toLowerCase(customScript.getName()) + ".py";
    if (script == null) {
        return null;
    }
    CustomScriptType customScriptType = customScript.getScriptType();
    BaseExternalType externalType = null;
    InputStream bis = null;
    try {
        bis = new ByteArrayInputStream(script.getBytes("UTF-8"));
        externalType = pythonService.loadPythonScript(bis, scriptName, customScriptType.getPythonClass(), customScriptType.getCustomScriptType(), new PyObject[] { new PyLong(System.currentTimeMillis()) });
    } catch (UnsupportedEncodingException e) {
        log.error(String.format("%s. Script inum: %s", e.getMessage(), customScript.getInum()), e);
    } finally {
        IOUtils.closeQuietly(bis);
    }
    if (externalType == null) {
        return null;
    }
    boolean initialized = false;
    try {
        if (externalType.getApiVersion() > 10) {
            initialized = externalType.init(customScript, configurationAttributes);
        } else {
            initialized = externalType.init(configurationAttributes);
            log.warn(" Update the script's init method to init(self, customScript, configurationAttributes)", customScript.getName());
        }
    } catch (Exception ex) {
        log.error("Failed to initialize custom script: '{}'", ex, customScript.getName());
    }
    if (initialized) {
        return externalType;
    }
    return null;
}
Also used : PyLong(org.python.core.PyLong) ByteArrayInputStream(java.io.ByteArrayInputStream) CustomScriptType(org.gluu.model.custom.script.CustomScriptType) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BaseExternalType(org.gluu.model.custom.script.type.BaseExternalType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PyObject(org.python.core.PyObject) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

PyObject (org.python.core.PyObject)80 PyString (org.python.core.PyString)24 PyList (org.python.core.PyList)16 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)12 PyException (org.python.core.PyException)12 IOException (java.io.IOException)9 Serializable (java.io.Serializable)9 PyInteger (org.python.core.PyInteger)8 Test (org.junit.Test)7 PyStringMap (org.python.core.PyStringMap)7 PythonInterpreter (org.python.util.PythonInterpreter)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Map (java.util.Map)6 PyDictionary (org.python.core.PyDictionary)6 PyFloat (org.python.core.PyFloat)6 List (java.util.List)5 PyLong (org.python.core.PyLong)5 PyNone (org.python.core.PyNone)5 PyTuple (org.python.core.PyTuple)5