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;
}
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);
}
}
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);
}
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);
}
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;
}
Aggregations