Python: normalize newlines in Java

This commit is contained in:
Alexander Bezzubov 2016-06-16 11:17:35 +09:00
parent 1bc3e9c811
commit 58efcc98a4
2 changed files with 5 additions and 37 deletions

View file

@ -52,11 +52,12 @@ public class PythonInterpreter extends Interpreter {
private Integer port;
private GatewayServer gatewayServer;
PythonProcess process = null;
private long pythonPid;
private Boolean py4J = false;
private InterpreterContext context;
PythonProcess process = null;
static {
Interpreter.register(
"python",
@ -76,10 +77,7 @@ public class PythonInterpreter extends Interpreter {
@Override
public void open() {
logger.info("Starting Python interpreter .....");
logger.info("Python path is set to:" + property.getProperty(ZEPPELIN_PYTHON));
process = getPythonProcess();
@ -116,13 +114,10 @@ public class PythonInterpreter extends Interpreter {
"initialize Zeppelin inputs in python process", e);
}
}
}
@Override
public void close() {
logger.info("closing Python interpreter .....");
try {
if (process != null) {
@ -134,13 +129,10 @@ public class PythonInterpreter extends Interpreter {
} catch (IOException e) {
logger.error("Can't close the interpreter", e);
}
}
@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
this.context = contextInterpreter;
String output = sendCommandToPython(cmd);
@ -148,7 +140,6 @@ public class PythonInterpreter extends Interpreter {
.replaceAll("\\.\\.\\.", "").trim());
}
@Override
public void cancel(InterpreterContext context) {
try {
@ -199,7 +190,6 @@ public class PythonInterpreter extends Interpreter {
private String sendCommandToPython(String cmd) {
String output = "";
logger.info("Sending : \n " + cmd);
try {
@ -207,13 +197,10 @@ public class PythonInterpreter extends Interpreter {
} catch (IOException e) {
logger.error("Error when sending commands to python process", e);
}
return output;
}
private void bootStrapInterpreter(String file) throws IOException {
BufferedReader bootstrapReader = new BufferedReader(
new InputStreamReader(
PythonInterpreter.class.getResourceAsStream(file)));
@ -230,26 +217,19 @@ public class PythonInterpreter extends Interpreter {
sendCommandToPython(bootstrapCode);
}
public GUI getGui() {
return context.getGui();
}
public Integer getPy4JPort() {
return port;
}
public Boolean isPy4jInstalled() {
String output = sendCommandToPython("\n\nimport py4j\n");
if (output.contains("ImportError"))
return false;
else return true;
}
private int findRandomOpenPortOnAllLocalInterfaces() {

View file

@ -33,15 +33,15 @@ import java.lang.reflect.Field;
* Object encapsulated interactive
* Python process (REPL) used by python interpreter
*/
public class PythonProcess {
Logger logger = LoggerFactory.getLogger(PythonProcess.class);
InputStream stdout;
OutputStream stdin;
BufferedWriter writer;
BufferedReader reader;
Process process;
private String binPath;
private long pid;
@ -64,22 +64,17 @@ public class PythonProcess {
logger.warn("Can't find python pid process", e);
pid = -1;
}
}
public void close() throws IOException {
process.destroy();
reader.close();
writer.close();
stdin.close();
stdout.close();
}
public void interrupt() throws IOException {
if (pid > -1) {
logger.info("Sending SIGINT signal to PID : " + pid);
Runtime.getRuntime().exec("kill -SIGINT " + pid);
@ -87,12 +82,9 @@ public class PythonProcess {
logger.warn("Non UNIX/Linux system, close the interpreter");
close();
}
}
public String sendAndGetResult(String cmd) throws IOException {
writer.write(cmd + "\n\n");
writer.write("print (\"*!?flush reader!?*\")\n\n");
writer.flush();
@ -106,18 +98,13 @@ public class PythonProcess {
output += "Syntax error ! ";
break;
}
output += "\r" + line + "\n";
}
return output;
}
private long findPid() throws NoSuchFieldException, IllegalAccessException {
long pid = -1;
if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
Field f = process.getClass().getDeclaredField("pid");
f.setAccessible(true);
@ -130,4 +117,5 @@ public class PythonProcess {
public long getPid() {
return pid;
}
}