-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython.java
More file actions
182 lines (150 loc) · 5.98 KB
/
Python.java
File metadata and controls
182 lines (150 loc) · 5.98 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package javaxt.express.utils;
import java.util.*;
import javaxt.json.JSONObject;
//import static javaxt.utils.Console.console;
//******************************************************************************
//** Python
//******************************************************************************
/**
* Used to execute python commands and parse the output.
*
******************************************************************************/
public class Python {
private String python;
//**************************************************************************
//** Constructor
//**************************************************************************
public Python(String pythonCommand){
try{
test(pythonCommand);
python = pythonCommand;
}
catch(Exception e){
throw new IllegalArgumentException("Invalid");
}
}
//**************************************************************************
//** Constructor
//**************************************************************************
public Python(){
//Check which python is installed on the system
for (String pythonCommand : new String[]{"python3", "python"}){
try{
test(pythonCommand);
python = pythonCommand;
break;
}
catch(Exception e){
//e.printStackTrace();
}
}
if (python==null) throw new IllegalArgumentException("Invalid");
}
//**************************************************************************
//** getScriptVersion
//**************************************************************************
/** Returns a version number for a given script. Under the hood, this method
* simply passes "--version" as a command-line argument to the script and
* returns the response.
*/
public String getScriptVersion(javaxt.io.File script) throws Exception {
ArrayList<String> params = new ArrayList<>();
params.add("--version");
List<String> output = execute(script, params);
return output.get(0);
}
//**************************************************************************
//** executeScript
//**************************************************************************
/** Used to run a given python script. Assumes the script is implemented as
* a command line application and that the application generates some sort
* of JSON formatted response.
*/
public JSONObject executeScript(javaxt.io.File script, ArrayList<String> params)
throws Exception {
return parseOutput(execute(script, params));
}
//**************************************************************************
//** execute
//**************************************************************************
private List<String> execute(javaxt.io.File script, ArrayList<String> params)
throws Exception {
String[] cmdarray = new String[params.size()+2];
cmdarray[0] = python;
cmdarray[1] = script.toString();
int i = 2;
for (String param : params){
cmdarray[i] = param;
i++;
}
javaxt.io.Shell cmd = new javaxt.io.Shell(cmdarray);
cmd.run();
List<String> output = cmd.getOutput();
List<String> errors = cmd.getErrors();
//Check if there are any errors
parseErrors(errors);
//Return output
return output;
}
//**************************************************************************
//** parseOutput
//**************************************************************************
/** Used to parse the standard output stream and return a JSONObject. Throws
* an exception if the output can't be parsed.
*/
private static JSONObject parseOutput(List<String> output) throws Exception {
try{
StringBuilder str = new StringBuilder();
Iterator<String> i2 = output.iterator();
while (i2.hasNext()){
String out = i2.next();
if (out!=null) str.append(out);
}
return new JSONObject(str.toString());
}
catch(Exception e){
StringBuilder err = new StringBuilder();
err.append("Error parsing script output");
StringBuilder result = new StringBuilder();
Iterator<String> i2 = output.iterator();
while (i2.hasNext()){
String out = i2.next();
if (out!=null) result.append(out + "\r\n");
}
err.append(":\r\n" + result);
throw new Exception(err.toString());
}
}
//**************************************************************************
//** parseErrors
//**************************************************************************
/** Used to parse the error output stream. Throws an exception if there are
* any errors.
*/
private static void parseErrors(List<String> errors) throws Exception{
if (errors.size()>0){
StringBuilder err = new StringBuilder();
Iterator<String> i2 = errors.iterator();
while (i2.hasNext()){
String error = i2.next();
if (error!=null) err.append(error + "\r\n");
}
if (err.length()>0){
throw new Exception(err.toString());
}
}
}
//**************************************************************************
//** test
//**************************************************************************
/** Used to test a given string to see if it is a valid "python" command.
*/
private static void test(String python) throws Exception {
String[] cmdarray = new String[]{python, "--version"};
javaxt.io.Shell cmd = new javaxt.io.Shell(cmdarray);
cmd.run();
parseErrors(cmd.getErrors());
//TODO: check output
//console.log("Found python " + cmd.getOutput().get(0));
}
}