Search in sources :

Example 1 with PyObject

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

the class RegistrationInterceptionService method createRegistrationScriptFromStringWithPythonException.

private RegistrationScript createRegistrationScriptFromStringWithPythonException(RegistrationInterceptorScript script) {
    String pythonScriptText = script.getCustomScript();
    if (pythonScriptText == null) {
        return null;
    }
    RegistrationScript pythonScript = null;
    InputStream bis = null;
    try {
        bis = new ByteArrayInputStream(pythonScriptText.getBytes(Util.UTF8_STRING_ENCODING));
        pythonScript = pythonService.loadPythonScript(bis, "RegistrationScriptClass", RegistrationScript.class, new PyObject[] { new PyLong(System.currentTimeMillis()) });
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
    } catch (PythonException e) {
        log.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(bis);
    }
    return pythonScript;
}
Also used : RegistrationScript(org.gluu.oxtrust.service.python.interfaces.RegistrationScript) PyLong(org.python.core.PyLong) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PythonException(org.xdi.exception.PythonException) PyObject(org.python.core.PyObject)

Example 2 with PyObject

use of org.python.core.PyObject in project yamcs-studio by yamcs.

the class ExecutePythonScriptAction method dispose.

@Override
public void dispose() {
    if (interpreter != null) {
        PyObject o = interpreter.getLocals();
        if (o != null && o instanceof PyStringMap) {
            ((PyStringMap) o).clear();
        }
        o = state.getDict();
        if (o != null && o instanceof PyStringMap) {
            ((PyStringMap) o).clear();
        }
        state.close();
        state.cleanup();
        interpreter.close();
        interpreter.cleanup();
        interpreter = null;
        state = null;
    }
    super.dispose();
}
Also used : PyStringMap(org.python.core.PyStringMap) PyObject(org.python.core.PyObject)

Example 3 with PyObject

use of org.python.core.PyObject in project components by Talend.

the class PythonRowDoFn method flatMap.

private void flatMap(IndexedRecord input, ProcessContext context) throws IOException {
    // Prepare Python environment
    PyObject outputList = pyFn.__call__(new PyString(input.toString()));
    if (outputList instanceof PyList) {
        PyList list = (PyList) outputList;
        for (Object output : list) {
            if (jsonGenericRecordConverter == null) {
                JsonSchemaInferrer jsonSchemaInferrer = new JsonSchemaInferrer(new ObjectMapper());
                Schema jsonSchema = jsonSchemaInferrer.inferSchema(output.toString());
                jsonGenericRecordConverter = new JsonGenericRecordConverter(jsonSchema);
            }
            GenericRecord outputRecord = jsonGenericRecordConverter.convertToAvro(output.toString());
            context.output(outputRecord);
        }
    }
}
Also used : PyString(org.python.core.PyString) PyList(org.python.core.PyList) Schema(org.apache.avro.Schema) PyObject(org.python.core.PyObject) JsonGenericRecordConverter(org.talend.daikon.avro.converter.JsonGenericRecordConverter) GenericRecord(org.apache.avro.generic.GenericRecord) PyObject(org.python.core.PyObject) JsonSchemaInferrer(org.talend.daikon.avro.inferrer.JsonSchemaInferrer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with PyObject

use of org.python.core.PyObject in project components by Talend.

the class PythonRowDoFn method map.

private void map(IndexedRecord input, ProcessContext context) throws IOException {
    PyObject output = pyFn.__call__(new PyString(input.toString()));
    if (jsonGenericRecordConverter == null) {
        JsonSchemaInferrer jsonSchemaInferrer = new JsonSchemaInferrer(new ObjectMapper());
        Schema jsonSchema = jsonSchemaInferrer.inferSchema(output.toString());
        jsonGenericRecordConverter = new JsonGenericRecordConverter(jsonSchema);
    }
    GenericRecord outputRecord = jsonGenericRecordConverter.convertToAvro(output.toString());
    context.output(outputRecord);
}
Also used : PyString(org.python.core.PyString) Schema(org.apache.avro.Schema) JsonGenericRecordConverter(org.talend.daikon.avro.converter.JsonGenericRecordConverter) GenericRecord(org.apache.avro.generic.GenericRecord) PyObject(org.python.core.PyObject) JsonSchemaInferrer(org.talend.daikon.avro.inferrer.JsonSchemaInferrer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with PyObject

use of org.python.core.PyObject in project freemarker by apache.

the class JythonModelCache method create.

@Override
protected TemplateModel create(Object obj) {
    boolean asHash = false;
    boolean asSequence = false;
    JythonVersionAdapter versionAdapter = JythonVersionAdapterHolder.INSTANCE;
    if (versionAdapter.isPyInstance(obj)) {
        Object jobj = versionAdapter.pyInstanceToJava(obj);
        // FreeMarker-aware, Jython-wrapped Java objects are left intact
        if (jobj instanceof TemplateModel) {
            return (TemplateModel) jobj;
        }
        if (jobj instanceof Map) {
            asHash = true;
        }
        if (jobj instanceof Date) {
            return new DateModel((Date) jobj, BeansWrapper.getDefaultInstance());
        } else if (jobj instanceof Collection) {
            asSequence = true;
            // doesn't support sets.
            if (!(jobj instanceof List)) {
                obj = new ArrayList((Collection) jobj);
            }
        }
    }
    // If it's not a PyObject, first make a PyObject out of it.
    if (!(obj instanceof PyObject)) {
        obj = Py.java2py(obj);
    }
    if (asHash || obj instanceof PyDictionary || obj instanceof PyStringMap) {
        return JythonHashModel.FACTORY.create(obj, wrapper);
    }
    if (asSequence || obj instanceof PySequence) {
        return JythonSequenceModel.FACTORY.create(obj, wrapper);
    }
    if (obj instanceof PyInteger || obj instanceof PyLong || obj instanceof PyFloat) {
        return JythonNumberModel.FACTORY.create(obj, wrapper);
    }
    if (obj instanceof PyNone) {
        return null;
    }
    return JythonModel.FACTORY.create(obj, wrapper);
}
Also used : DateModel(freemarker.ext.beans.DateModel) ArrayList(java.util.ArrayList) PyFloat(org.python.core.PyFloat) TemplateModel(freemarker.template.TemplateModel) PyInteger(org.python.core.PyInteger) PyDictionary(org.python.core.PyDictionary) Date(java.util.Date) PyNone(org.python.core.PyNone) PyLong(org.python.core.PyLong) PyStringMap(org.python.core.PyStringMap) Collection(java.util.Collection) PyObject(org.python.core.PyObject) ArrayList(java.util.ArrayList) List(java.util.List) PySequence(org.python.core.PySequence) PyStringMap(org.python.core.PyStringMap) Map(java.util.Map) PyObject(org.python.core.PyObject)

Aggregations

PyObject (org.python.core.PyObject)17 IOException (java.io.IOException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 PyStringMap (org.python.core.PyStringMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 PyException (org.python.core.PyException)3 PyLong (org.python.core.PyLong)3 PythonException (org.xdi.exception.PythonException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 Schema (org.apache.avro.Schema)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 PyString (org.python.core.PyString)2 JsonGenericRecordConverter (org.talend.daikon.avro.converter.JsonGenericRecordConverter)2 JsonSchemaInferrer (org.talend.daikon.avro.inferrer.JsonSchemaInferrer)2 HasFields (com.google.refine.expr.HasFields)1 Environment (freemarker.core.Environment)1