Scripting python en java6
Hola amigos.
les cuento que estoy trabajando en actualizar un proyecto, la idea es hacer scripting de python en java.
implementar ScriptEngineManager();
La versión original de la clase que debo modificar es la Sgte:
public class PythonScriptManager implements ScriptManager {
private static final String FRESSIA_EXPECTED_PASS_STAT = "passed";
private static final String FRESSIA_EXPECTED_FAIL_STAT = "failed";
private static final String MSG = "Message sent by Fressia: couldn't determine the python test script status: ";
private static final String FRESSIA_STATUS_VAR_NAME = "tell_fressia";
private static final String PYUNIT_MOD_NAME = "unittest";
public ScriptResult execute(String scriptCode)
throws ScriptManagerException {
// create the fresh new system state
ClassLoader classLoader = new JythonClassLoader ();
PySystemState state = new PySystemState();
state.setClassLoader(classLoader);
state.path.insert(0, new PyString(""));
// create the interpreter global dictionary
PyDictionary table = new PyDictionary();
// create the interpreter
PythonInterpreter interp = new PythonInterpreter (table, state);
PyModule mod = imp.addModule("__main__");
interp.setLocals(mod.__dict__);
StringWriter out = new StringWriter();
interp.setOut(out);
interp.setErr(out);
// run the code
boolean error = false;
try {
interp.exec(scriptCode);
} catch (Throwable t) {
if (t instanceof PyException) {
PyException exc = (PyException) t;
PyObject value = exc.value;
if (value instanceof PyInstance) {
PyObject tmp = value.__findattr__("code");
if (tmp != null)
value = tmp;
}
if ((value instanceof PyInteger && ((PyInteger) value)
.getValue() != 0)
|| !(value instanceof PyInteger)) {
error = true;
if (!(value instanceof PyInteger))
Py.println(value);
}
} else {
throw new ScriptManagerException(t);
}
}
out.flush();
// check if it's not a PyUnit script
if (!error && interp.get(PYUNIT_MOD_NAME) == null) {
PyObject fressiaStatus = interp.get(FRESSIA_STATUS_VAR_NAME);
if (fressiaStatus != null && (fressiaStatus instanceof PyList)) {
PyList status = (PyList) fressiaStatus;
int size = status.size();
if (size >= 1) {
PyObject[] list = status.getArray();
String stat = "" + list[0];
if (FRESSIA_EXPECTED_FAIL_STAT.equals(stat))
error = true;
if (FRESSIA_EXPECTED_PASS_STAT.equals(stat))
error = false;
if (size > 1) {
String msg = "Message captured by Fressia: " + list[1];
out.append(msg);
}
}
} else {
String msg = MSG
+ "variable '" + FRESSIA_STATUS_VAR_NAME + "' couldn't be found.";
out.append(msg);
}
}
// cleanup everything to try to avoid
// the memory cost of using a different
// system state in every run.
interp.cleanup();
classLoader = null;
state = null;
table = null;
interp = null;
System.gc();
final boolean isError = error;
final String outBuff = out.toString();
final String errBuff = outBuff;
return new ScriptResult() {
private boolean isErr = isError;
private String out = outBuff;
private String err = errBuff;
public String getError() {
return err;
}
public String getOutput() {
return out;
}
public boolean isError() {
return isErr;
}
};
}
/**
*
* This classloader is just to force jython to use a new system state
* for every script run. This needs to be done this way cause python
* doesn't allow to unload modules - for this reason some modules
* pass from one script to another in different runs if the same system
* state is used.
*
*
*/
private static class JythonClassLoader extends ClassLoader {
}
}
....
Ahora estoy trabajando en usar las potencialidades del ScriptEngineManager() si alguién tiene experiencia, y me pueda colaborar u orientar le estaré agradecido.
no sé de qué manera recibir los parametros state y table usando el motor, se supone que ya no utilizaría el diccionario.
cualquier ayuda la agradezco
saludos
- Inicie sesión o regístrese para enviar comentarios