Search in sources :

Example 51 with PyObject

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

the class JythonPep8 method analyzePep8WithJython.

public static void analyzePep8WithJython(JythonPep8Core pep8Params) {
    FastStringBuffer args = new FastStringBuffer(pep8Params.pep8CommandLine.length * 20);
    for (String string : pep8Params.pep8CommandLine) {
        args.append(',').append("r'").append(string).append('\'');
    }
    // It's important that the interpreter is created in the Thread and not outside the thread (otherwise
    // it may be that the output ends up being shared, which is not what we want.)
    IPythonInterpreter interpreter = JythonPlugin.newPythonInterpreter(pep8Params.useConsole, false);
    String file = StringUtils.replaceAllSlashes(pep8Params.absolutePath);
    interpreter.set("visitor", pep8Params.visitor);
    List<String> splitInLines = StringUtils.splitInLines(pep8Params.document.get());
    interpreter.set("lines", splitInLines);
    PyObject tempReportError = reportError;
    if (tempReportError != null) {
        interpreter.set("ReportError", tempReportError);
    } else {
        interpreter.set("ReportError", Py.None);
    }
    PyObject pep8Module = JythonModules.getPep8Module(interpreter);
    interpreter.set("pycodestyle", pep8Module);
    String formatted = StringUtils.format(EXECUTE_PEP8, file, args.toString(), file);
    interpreter.exec(formatted);
    if (reportError == null) {
        synchronized (lock) {
            if (reportError == null) {
                reportError = (PyObject) interpreter.get("ReportError");
            }
        }
    }
}
Also used : IPythonInterpreter(org.python.pydev.shared_core.jython.IPythonInterpreter) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) PyObject(org.python.core.PyObject)

Example 52 with PyObject

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

the class EmbeddedPythonExecutorWrapper method getPythonLocals.

private Map<String, Serializable> getPythonLocals() {
    Map<String, Serializable> retVal = new HashMap<>();
    for (PyObject pyObject : pythonInterpreter.getLocals().asIterable()) {
        String key = pyObject.asString();
        PyObject value = pythonInterpreter.get(key);
        if (!isLocalEntryExcluded(key, value)) {
            retVal.put(key, value);
        }
    }
    return retVal;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) PyString(org.python.core.PyString) PyObject(org.python.core.PyObject)

Example 53 with PyObject

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

the class PythonExecutor method getPythonLocals.

private Map<String, Serializable> getPythonLocals() {
    Map<String, Serializable> result = new HashMap<>();
    if (interpreter.getLocals() != null) {
        for (PyObject pyObject : interpreter.getLocals().asIterable()) {
            String key = pyObject.asString();
            PyObject value = interpreter.get(key);
            if (keyIsExcluded(key, value)) {
                continue;
            }
            result.put(key, value);
        }
    }
    return result;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) PyString(org.python.core.PyString) PyObject(org.python.core.PyObject)

Example 54 with PyObject

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

the class PythonExecutor method eval.

protected Serializable eval(String prepareEnvironmentScript, String script) {
    if (interpreter.get(TRUE) == null) {
        interpreter.set(TRUE, Boolean.TRUE);
    }
    if (interpreter.get(FALSE) == null) {
        interpreter.set(FALSE, Boolean.FALSE);
    }
    if (prepareEnvironmentScript != null && !prepareEnvironmentScript.isEmpty()) {
        interpreter.exec(prepareEnvironmentScript);
    }
    PyObject evalResultAsPyObject = interpreter.eval(script);
    Serializable evalResult;
    evalResult = resolveJythonObjectToJavaEval(evalResultAsPyObject, script);
    return evalResult;
}
Also used : Serializable(java.io.Serializable) PyObject(org.python.core.PyObject)

Example 55 with PyObject

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

the class PythonExecutor method exec.

private PythonExecutionResult exec(String script) {
    interpreter.exec(script);
    Iterator<PyObject> localsIterator = interpreter.getLocals().asIterable().iterator();
    Map<String, Serializable> returnValue = new HashMap<>();
    while (localsIterator.hasNext()) {
        String key = localsIterator.next().asString();
        PyObject value = interpreter.get(key);
        if (keyIsExcluded(key, value)) {
            continue;
        }
        Serializable javaValue = resolveJythonObjectToJavaExec(value, key);
        returnValue.put(key, javaValue);
    }
    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)

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