Skip to content

Commit 49d5a36

Browse files
committed
Initial commit of the Python utility class
1 parent 7305139 commit 49d5a36

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package javaxt.express.utils;
2+
3+
import java.util.*;
4+
import javaxt.json.JSONObject;
5+
//import static javaxt.utils.Console.console;
6+
7+
8+
//******************************************************************************
9+
//** Python
10+
//******************************************************************************
11+
/**
12+
* Used to execute python commands and parse the output.
13+
*
14+
******************************************************************************/
15+
16+
public class Python {
17+
18+
private String python;
19+
20+
21+
//**************************************************************************
22+
//** Constructor
23+
//**************************************************************************
24+
public Python(String pythonCommand){
25+
try{
26+
test(pythonCommand);
27+
python = pythonCommand;
28+
}
29+
catch(Exception e){
30+
throw new IllegalArgumentException("Invalid");
31+
}
32+
}
33+
34+
35+
//**************************************************************************
36+
//** Constructor
37+
//**************************************************************************
38+
public Python(){
39+
40+
//Check which python is installed on the system
41+
for (String pythonCommand : new String[]{"python3", "python"}){
42+
try{
43+
test(pythonCommand);
44+
python = pythonCommand;
45+
break;
46+
}
47+
catch(Exception e){
48+
//e.printStackTrace();
49+
}
50+
}
51+
52+
if (python==null) throw new IllegalArgumentException("Invalid");
53+
}
54+
55+
56+
//**************************************************************************
57+
//** getScriptVersion
58+
//**************************************************************************
59+
/** Returns a version number for a given script. Under the hood, this method
60+
* simply passes "--version" as a command-line argument to the script and
61+
* returns the response.
62+
*/
63+
public String getScriptVersion(javaxt.io.File script) throws Exception {
64+
ArrayList<String> params = new ArrayList<>();
65+
params.add("--version");
66+
List<String> output = execute(script, params);
67+
return output.get(0);
68+
}
69+
70+
71+
//**************************************************************************
72+
//** executeScript
73+
//**************************************************************************
74+
/** Used to run a given python script. Assumes the script is implemented as
75+
* a command line application and that the application generates some sort
76+
* of JSON formatted response.
77+
*/
78+
public JSONObject executeScript(javaxt.io.File script, ArrayList<String> params)
79+
throws Exception {
80+
return parseOutput(execute(script, params));
81+
}
82+
83+
84+
//**************************************************************************
85+
//** execute
86+
//**************************************************************************
87+
private List<String> execute(javaxt.io.File script, ArrayList<String> params)
88+
throws Exception {
89+
90+
String[] cmdarray = new String[params.size()+2];
91+
cmdarray[0] = python;
92+
cmdarray[1] = script.toString();
93+
int i = 2;
94+
for (String param : params){
95+
cmdarray[i] = param;
96+
i++;
97+
}
98+
99+
javaxt.io.Shell cmd = new javaxt.io.Shell(cmdarray);
100+
cmd.run();
101+
List<String> output = cmd.getOutput();
102+
List<String> errors = cmd.getErrors();
103+
104+
105+
//Check if there are any errors
106+
parseErrors(errors);
107+
108+
109+
//Return output
110+
return output;
111+
}
112+
113+
114+
//**************************************************************************
115+
//** parseOutput
116+
//**************************************************************************
117+
/** Used to parse the standard output stream and return a JSONObject. Throws
118+
* an exception if the output can't be parsed.
119+
*/
120+
private static JSONObject parseOutput(List<String> output) throws Exception {
121+
try{
122+
123+
StringBuilder str = new StringBuilder();
124+
Iterator<String> i2 = output.iterator();
125+
while (i2.hasNext()){
126+
String out = i2.next();
127+
if (out!=null) str.append(out);
128+
}
129+
130+
return new JSONObject(str.toString());
131+
}
132+
catch(Exception e){
133+
StringBuilder err = new StringBuilder();
134+
err.append("Error parsing script output");
135+
StringBuilder result = new StringBuilder();
136+
Iterator<String> i2 = output.iterator();
137+
while (i2.hasNext()){
138+
String out = i2.next();
139+
if (out!=null) result.append(out + "\r\n");
140+
}
141+
err.append(":\r\n" + result);
142+
throw new Exception(err.toString());
143+
}
144+
}
145+
146+
147+
//**************************************************************************
148+
//** parseErrors
149+
//**************************************************************************
150+
/** Used to parse the error output stream. Throws an exception if there are
151+
* any errors.
152+
*/
153+
private static void parseErrors(List<String> errors) throws Exception{
154+
if (errors.size()>0){
155+
StringBuilder err = new StringBuilder();
156+
Iterator<String> i2 = errors.iterator();
157+
while (i2.hasNext()){
158+
String error = i2.next();
159+
if (error!=null) err.append(error + "\r\n");
160+
}
161+
if (err.length()>0){
162+
throw new Exception(err.toString());
163+
}
164+
}
165+
}
166+
167+
168+
//**************************************************************************
169+
//** test
170+
//**************************************************************************
171+
/** Used to test a given string to see if it is a valid "python" command.
172+
*/
173+
private static void test(String python) throws Exception {
174+
String[] cmdarray = new String[]{python, "--version"};
175+
javaxt.io.Shell cmd = new javaxt.io.Shell(cmdarray);
176+
cmd.run();
177+
parseErrors(cmd.getErrors());
178+
179+
//TODO: check output
180+
//console.log("Found python " + cmd.getOutput().get(0));
181+
}
182+
}

0 commit comments

Comments
 (0)