-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sh0derun
committed
Feb 13, 2020
1 parent
0aa4090
commit ab7cd68
Showing
10 changed files
with
140 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
*.py | ||
/target | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.oracle.challenge.common; | ||
|
||
public class Enums { | ||
public enum InterpreterIdentifier{ | ||
PYTHON("python"); | ||
private String value; | ||
|
||
InterpreterIdentifier(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String toString() { | ||
return String.valueOf(value); | ||
} | ||
|
||
public static InterpreterIdentifier fromValue(String text) { | ||
for (InterpreterIdentifier b : InterpreterIdentifier.values()) { | ||
if (String.valueOf(b.value).equals(text)) { | ||
return b; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.oracle.challenge.common; | ||
|
||
public interface Interpreter { | ||
|
||
public void interpret(String code); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.oracle.challenge.common; | ||
|
||
public class Python implements Interpreter{ | ||
|
||
public void interpret(String code) { | ||
System.out.println(code); | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/com/oracle/challenge/service/NoteBookService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.oracle.challenge.service; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.oracle.challenge.common.Enums; | ||
import com.oracle.challenge.common.Interpreter; | ||
import com.oracle.challenge.common.Python; | ||
import com.oracle.challenge.model.NoteBookInput; | ||
import com.oracle.challenge.model.NoteBookOutput; | ||
|
||
import antlr.PythonCodeGenerator; | ||
|
||
@Service | ||
public class NoteBookService { | ||
|
||
@Autowired | ||
ApplicationContext context; | ||
|
||
private static Map<Enums.InterpreterIdentifier, Class<?>> interpretersMap = null; | ||
|
||
static { | ||
interpretersMap = new HashMap<>(); | ||
interpretersMap.put(Enums.InterpreterIdentifier.PYTHON, Python.class); | ||
} | ||
|
||
public NoteBookOutput HandelNoteBookInput(NoteBookInput noteBookInput) { | ||
NoteBookOutput noteBookOutput = new NoteBookOutput(); | ||
try { | ||
String[] splitedInput = noteBookInput.getCode().split(" ", 2); | ||
String interpreter = splitedInput[0].substring(1); | ||
String code = splitedInput[1]; | ||
|
||
System.out.println(interpreter); | ||
System.out.println(code); | ||
/* | ||
interpretersMap.get(Enums.InterpreterIdentifier.fromValue(interpreter)); | ||
*/ | ||
//script that will hold all inputs, and gets fed with new input when ever the endpoint is called | ||
BufferedWriter out = new BufferedWriter(new FileWriter("script.py", true)); | ||
out.append("\n"+code); | ||
out.close(); | ||
|
||
Process process = null; | ||
|
||
//script execution | ||
if(System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0) { | ||
process = Runtime.getRuntime().exec(String.format("cmd.exe /c python script.py")); | ||
} | ||
else { | ||
//other os | ||
} | ||
|
||
//preparing result of script execution to be binded in the output | ||
ByteArrayOutputStream result = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[1024]; | ||
int length; | ||
while ((length = process.getInputStream().read(buffer)) != -1) { | ||
result.write(buffer, 0, length); | ||
} | ||
|
||
noteBookOutput.setResult(result.toString().replaceAll("(\\r|\\n)", "|")); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
System.out.println(e.getMessage()); | ||
} catch (SecurityException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
|
||
return noteBookOutput; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Manifest-Version: 1.0 | ||
Build-Jdk-Spec: 11 | ||
Implementation-Title: BackendNoteBookChallenge | ||
Implementation-Version: 0.0.1-SNAPSHOT | ||
Build-Jdk-Spec: 1.8 | ||
Created-By: Maven Integration for Eclipse | ||
|
8 changes: 4 additions & 4 deletions
8
target/classes/META-INF/maven/com.oracle/BackendNoteBookChallenge/pom.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#Generated by Maven Integration for Eclipse | ||
#Wed Feb 12 23:16:14 CET 2020 | ||
m2e.projectLocation=D\:\\shadrunWorks\\oracleChallenge\\BackendNoteBookChallenge | ||
m2e.projectName=BackendNoteBookChallenge | ||
#Thu Feb 13 08:31:09 WET 2020 | ||
version=0.0.1-SNAPSHOT | ||
groupId=com.oracle | ||
m2e.projectName=BackendNoteBookChallenge | ||
m2e.projectLocation=C\:\\Users\\arazik\\Downloads\\AnassGithub\\BackendNoteBookChallenge | ||
artifactId=BackendNoteBookChallenge | ||
version=0.0.1-SNAPSHOT |
Binary file not shown.