BowlerKernel
JythonHelper.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.scripting;
2 
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.file.Files;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.Properties;
9 
10 import javafx.scene.control.Tab;
11 
12 import org.python.core.PyObject;
13 import org.python.util.PythonInterpreter;
14 
15 import com.neuronrobotics.sdk.common.BowlerAbstractDevice;
16 import com.neuronrobotics.sdk.common.DeviceManager;
17 import com.neuronrobotics.sdk.common.Log;
18 
19 import eu.mihosoft.vrl.v3d.CSG;
20 
21 public class JythonHelper implements IScriptingLanguage {
22 
23  PythonInterpreter interp;
24 
25 
26  @Override
27  public Object inlineScriptRun(String code, ArrayList<Object> args) {
28  Properties props = new Properties();
29  PythonInterpreter.initialize(System.getProperties(), props,
30  new String[]{""});
31  if (interp == null) {
32  interp = new PythonInterpreter();
33 
34  interp.exec("import sys");
35  }
36 
37 // for (String pm : DeviceManager.listConnectedDevice(null)) {
38 // BowlerAbstractDevice bad = DeviceManager.getSpecificDevice(null, pm);
39 // // passing into the scipt
40 // try {
41 // interp.set(bad.getScriptingName(),
42 // Class.forName(bad.getClass().getName())
43 // .cast(bad));
44 // } catch (ClassNotFoundException e) {
45 // // TODO Auto-generated catch block
46 // e.printStackTrace();
47 // }
48 // System.err.println("Device " + bad.getScriptingName() + " is "
49 // + bad);
50 // }
51  interp.set("args", args);
52  interp.exec(code);
53  ArrayList<Object> results = new ArrayList<>();
54 
55  PyObject localVariables = interp.getLocals();
56 
57  try {
58  results.add(interp.get("csg", CSG.class));
59  } catch (Exception e) {
60  e.printStackTrace();
61  }
62  try {
63  results.add(interp.get("tab", Tab.class));
64  } catch (Exception e) {
65  e.printStackTrace();
66  }
67  try {
68  results.add(interp.get("device", BowlerAbstractDevice.class));
69  } catch (Exception e) {
70  e.printStackTrace();
71  }
72 
73  Log.debug("Jython return = " + results);
74  return results;
75  }
76 
77  @Override
78  public Object inlineScriptRun(File code, ArrayList<Object> args) {
79  byte[] bytes;
80  try {
81  bytes = Files.readAllBytes(code.toPath());
82  String s = new String(bytes, "UTF-8");
83  return inlineScriptRun(s, args);
84  } catch (IOException e1) {
85  // TODO Auto-generated catch block
86  e1.printStackTrace();
87  }
88  return null;
89  }
90 
91 
92  @Override
93  public String getShellType() {
94  return "Jython";
95  }
101  public String getDefaultContents() {
102  return "print( 'Hello World')";
103  }
104  @Override
105  public boolean getIsTextFile() {
106  // TODO Auto-generated method stub
107  return true;
108  }
109 
110  @Override
111  public ArrayList<String> getFileExtenetion() {
112  // TODO Auto-generated method stub
113  return new ArrayList<>(Arrays.asList("py", "jy"));
114  }
115 
116 }
Object inlineScriptRun(String code, ArrayList< Object > args)
Object inlineScriptRun(File code, ArrayList< Object > args)
static void debug(String message)
Definition: Log.java:128