-
Notifications
You must be signed in to change notification settings - Fork 0
/
JythonConnector.java
57 lines (47 loc) · 1.45 KB
/
JythonConnector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package sk.yin.yngine.scripts.jython;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import sk.yin.yngine.util.Log;
/**
* Connects Jython and Java types, performs Python script execution
* and Python type instantiation. In the future, script file change monitoring
* ans online script reloading should be implemented here.
*
* @author Matej 'Yin' Gagyi <[email protected]>
*/
public class JythonConnector {
private String filename;
private PythonInterpreter python = null;
public JythonConnector(String filename) {
this.filename = filename;
}
public void set(String name, Object value) {
python().set(name, value);
}
public PyObject get(String name) {
return python().get(name);
}
public Object getTranslate(String name, Class clazz) {
PyObject obj = get(name);
if (clazz.isInstance(obj)) {
return clazz.cast(obj);
} else {
return null;
}
}
public void run() {
try {
python().execfile("../scripts/jython/" + filename);
} catch (Exception ex) {
// TODO(yin): Add exception handling for script.
Log.log("Python exception has been caught");
ex.printStackTrace();
}
}
private PythonInterpreter python() {
if (python == null) {
python = new PythonInterpreter();
}
return python;
}
}