Search in sources :

Example 71 with PyObject

use of org.python.core.PyObject in project cloud-slang by CloudSlang.

the class ScriptExecutor method runJythonAction.

private Map<String, Value> runJythonAction(Set<String> dependencies, String script, Map<String, Value> callArguments) {
    Map<String, Serializable> executionResult = pythonRuntimeService.exec(dependencies, script, createJythonContext(callArguments)).getExecutionResult();
    Map<String, Value> result = new HashMap<>();
    for (Map.Entry<String, Serializable> entry : executionResult.entrySet()) {
        Value callArgument = callArguments.get(entry.getKey());
        Serializable theValue;
        if (entry.getValue() instanceof PyList) {
            ArrayList<String> localList = new ArrayList<>();
            PyList pyList = (PyList) entry.getValue();
            for (Object next : pyList) {
                localList.add(next == null ? null : next.toString());
            }
            theValue = localList;
        } else if (entry.getValue() instanceof PyObject) {
            theValue = entry.getValue().toString();
        } else {
            theValue = entry.getValue();
        }
        Value value = ValueFactory.create(theValue, callArgument != null && callArgument.isSensitive());
        result.put(entry.getKey(), value);
    }
    return result;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PyList(org.python.core.PyList) Value(io.cloudslang.lang.entities.bindings.values.Value) PyObject(org.python.core.PyObject) HashMap(java.util.HashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) PyObject(org.python.core.PyObject)

Example 72 with PyObject

use of org.python.core.PyObject in project cloud-slang by CloudSlang.

the class PyObjectValueProxyFactory method create.

public static PyObjectValue create(Serializable content, boolean sensitive) {
    PyObject pyObject = Py.java2py(content);
    try {
        PyObjectValueProxyClass proxyClass = getProxyClass(pyObject);
        PyObjectValue pyObjectValue = (PyObjectValue) proxyClass.getConstructor().newInstance(proxyClass.getParams());
        ((Proxy) pyObjectValue).setHandler(new PyObjectValueMethodHandler(content, sensitive, pyObject));
        return pyObjectValue;
    } catch (Exception e) {
        throw new RuntimeException("Failed to create a proxy to new instance for PyObjectValue and " + pyObject.getClass().getSimpleName(), e);
    }
}
Also used : Proxy(javassist.util.proxy.Proxy) PyObject(org.python.core.PyObject)

Example 73 with PyObject

use of org.python.core.PyObject in project spring-security by spring-projects.

the class PythonInterpreterPreInvocationAdvice method before.

@Override
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 ex) {
        throw new IllegalArgumentException("Couldn't run python script, " + script, ex);
    }
    PyObject allowed = python.get("allow");
    if (allowed == null) {
        throw new IllegalStateException("Python script did not set the permit flag");
    }
    return 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 74 with PyObject

use of org.python.core.PyObject in project Pogamut3 by kefik.

the class PyTupleWrapper method getNewValue.

/**
 * Accepts List<PyObject> as parameter of newValue.
 * Returns PyList
 */
@Override
public PyObject getNewValue(Object newValue) {
    if (!(newValue instanceof List)) {
        throw new IllegalArgumentException("newValue is not instance of List");
    }
    List list = (List) newValue;
    PyObject[] elements = new PyObject[list.size()];
    for (int i = 0; i < list.size(); ++i) {
        if (!(list.get(i) instanceof PyObject))
            throw new IllegalArgumentException("value of the list is not instance of PyObject");
        elements[i] = (PyObject) list.get(i);
    }
    return new PyTuple(elements);
}
Also used : List(java.util.List) PyList(org.python.core.PyList) ArrayList(java.util.ArrayList) PyTuple(org.python.core.PyTuple) PyObject(org.python.core.PyObject)

Example 75 with PyObject

use of org.python.core.PyObject in project Pogamut3 by kefik.

the class PyDictionaryWrapper method getNewValue.

/**
 * Accepts Map<String, PyObject> as newValue.
 */
@Override
public PyObject getNewValue(Object newValue) {
    if (!(newValue instanceof Map)) {
        throw new IllegalArgumentException("newValue not of type Map");
    }
    Map map = (Map) newValue;
    Set set = map.keySet();
    Iterator iter = set.iterator();
    PyDictionary dict = new PyDictionary();
    while (iter.hasNext()) {
        Object next = iter.next();
        if (!(next instanceof String))
            throw new IllegalArgumentException("Key of the map is not string.");
        String key = (String) next;
        if (!(map.get(key) instanceof PyObject))
            throw new IllegalArgumentException("Value of the item of the map is not PyObject.");
        dict.__setitem__(new PyString(key), (PyObject) map.get(key));
    }
    return dict;
}
Also used : PyString(org.python.core.PyString) Set(java.util.Set) Iterator(java.util.Iterator) PyObject(org.python.core.PyObject) PyString(org.python.core.PyString) PyDictionary(org.python.core.PyDictionary) Map(java.util.Map) HashMap(java.util.HashMap) PyObject(org.python.core.PyObject)

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