forked from CommonWealthRobotics/bowler-script-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJythonHelper.java
More file actions
116 lines (97 loc) · 3.08 KB
/
JythonHelper.java
File metadata and controls
116 lines (97 loc) · 3.08 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.neuronrobotics.bowlerstudio.scripting;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
import javafx.scene.control.Tab;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import com.neuronrobotics.sdk.common.BowlerAbstractDevice;
import com.neuronrobotics.sdk.common.DeviceManager;
import com.neuronrobotics.sdk.common.Log;
import eu.mihosoft.vrl.v3d.CSG;
public class JythonHelper implements IScriptingLanguage {
PythonInterpreter interp;
@Override
public Object inlineScriptRun(eu.mihosoft.vrl.v3d.parametrics.CSGDatabaseInstance db,String code, ArrayList<Object> args) {
Properties props = new Properties();
PythonInterpreter.initialize(System.getProperties(), props,
new String[]{""});
if (interp == null) {
interp = new PythonInterpreter();
interp.exec("import sys");
}
// for (String pm : DeviceManager.listConnectedDevice(null)) {
// BowlerAbstractDevice bad = DeviceManager.getSpecificDevice(null, pm);
// // passing into the scipt
// try {
// interp.set(bad.getScriptingName(),
// Class.forName(bad.getClass().getName())
// .cast(bad));
// } catch (ClassNotFoundException e) {
// // Auto-generated catch block
// com.neuronrobotics.sdk.common.Log.error(e);
// }
// com.neuronrobotics.sdk.common.Log.error("Device " + bad.getScriptingName() + " is "
// + bad);
// }
interp.set("args", args);
interp.exec(code);
ArrayList<Object> results = new ArrayList<>();
PyObject localVariables = interp.getLocals();
try {
results.add(interp.get("csg", CSG.class));
} catch (Exception e) {
com.neuronrobotics.sdk.common.Log.error(e);
}
try {
results.add(interp.get("tab", Tab.class));
} catch (Exception e) {
com.neuronrobotics.sdk.common.Log.error(e);
}
try {
results.add(interp.get("device", BowlerAbstractDevice.class));
} catch (Exception e) {
com.neuronrobotics.sdk.common.Log.error(e);
}
Log.debug("Jython return = " + results);
return results;
}
@Override
public Object inlineScriptRun(eu.mihosoft.vrl.v3d.parametrics.CSGDatabaseInstance db,File code, ArrayList<Object> args) {
byte[] bytes;
try {
bytes = Files.readAllBytes(code.toPath());
String s = new String(bytes, "UTF-8");
return inlineScriptRun( db,s, args);
} catch (IOException e1) {
// Auto-generated catch block
com.neuronrobotics.sdk.common.Log.error(e1);
}
return null;
}
@Override
public String getShellType() {
return "Jython";
}
/**
* Get the contents of an empty file
*
* @return
*/
public String getDefaultContents() {
return "print( 'Hello World')";
}
@Override
public boolean getIsTextFile() {
// Auto-generated method stub
return true;
}
@Override
public ArrayList<String> getFileExtension() {
// Auto-generated method stub
return new ArrayList<>(Arrays.asList( "jy"));
}
}