Skip to content
Snippets Groups Projects
Commit 4f8ff529 authored by silvergl's avatar silvergl
Browse files

Working magics

parent bcc3ad9b
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,7 @@ import io.github.spencerpark.jupyter.kernel.magic.common.WriteFile;
import io.github.spencerpark.jupyter.kernel.magic.registry.Magics;
public class DummyKernel extends BaseKernel {
private static final DummyKernel kernel = new DummyKernel();
// private static final NashornScriptEngineFactory NASHORN_ENGINE_FACTORY = new
// NashornScriptEngineFactory();
......@@ -42,7 +43,7 @@ public class DummyKernel extends BaseKernel {
* this(DummyKernel.NASHORN_ENGINE_FACTORY.getScriptEngine(args)); }
*/
public DummyKernel() {
private DummyKernel() {
this.magics = new Magics();
this.mgcp = new Magicsprocessor();
this.magics.registerMagics(new WriteFile());
......@@ -51,6 +52,17 @@ public class DummyKernel extends BaseKernel {
.fileExtension(".mydsl2").pygments("hsail").codemirror("java").build();
}
public static DummyKernel getInstance() {
if (DummyKernel.kernel == null) {
return new DummyKernel();
}
return DummyKernel.kernel;
}
public Magics getMagics() {
return this.magics;
}
@Override
public LanguageInfo getLanguageInfo() {
return this.languageInfo;
......@@ -58,8 +70,7 @@ public class DummyKernel extends BaseKernel {
public Object evalRaw(String expr) throws Exception {
expr = this.mgcp.transformMagics(expr);
System.out.println(expr);
return this.evalCode(expr);
return expr;
}
private Object evalCode(final String code) {
......@@ -77,20 +88,4 @@ public class DummyKernel extends BaseKernel {
return null;
}
/*
* @Override public DisplayData eval(final String expr) throws Exception { ScriptContext ctx =
* this.engine.getContext();
*
* // Redirect the streams ctx.setWriter(new OutputStreamWriter(System.out));
* ctx.setErrorWriter(new OutputStreamWriter(System.err)); ctx.setReader(new
* InputStreamReader(System.in));
*
* // Evaluate the code Object res = this.engine.eval(expr, ctx);
*
* // If the evaluation returns a non-null value (the code is an expression like // 'a + b')
* then the return value should be this result as text. Otherwise // return null for nothing to
* be emitted for 'Out[n]'. Side effects may have // still printed something return new
* DisplayData(res.toString()); }
*/
}
......@@ -35,7 +35,7 @@ public class DummyMain {
String[] engineArgs = envEngineArgs.split(" ");
DummyKernel kernel = new DummyKernel();
DummyKernel kernel = DummyKernel.getInstance();
kernel.becomeHandlerForConnection(connection);
connection.connect();
......
package Dummykernel;
import java.util.Base64;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import Xtextgenerator.MyXtextGenerator;
import io.github.spencerpark.jupyter.kernel.magic.CellMagicParseContext;
import io.github.spencerpark.jupyter.kernel.magic.LineMagicParseContext;
import io.github.spencerpark.jupyter.kernel.magic.MagicParser;
import io.github.spencerpark.jupyter.kernel.magic.registry.Magics;
public class Magicsprocessor {
// private final DummyKernel kernel = DummyKernel.getInstance();
private static final Pattern UNESCAPED_QUOTE = Pattern.compile("(?<!\\\\)\"");
private final MagicParser parser;
......@@ -20,8 +24,9 @@ public class Magicsprocessor {
public String transformMagics(final String source) {
CellMagicParseContext ctx = this.parser.parseCellMagic(source);
if (ctx != null) {
return this.transformCellMagic(ctx);
return this.eval(ctx);
}
return this.transformLineMagics(source);
......@@ -44,6 +49,23 @@ public class Magicsprocessor {
});
}
private String eval(final CellMagicParseContext ctx) {
Magics magics = DummyKernel.getInstance().getMagics();
String name = ctx.getMagicCall().getName();
List<String> args = ctx.getMagicCall().getArgs();
String body = ctx.getMagicCall().getBody();
String result = MyXtextGenerator.generate(body);
try {
magics.applyCellMagic(name, args, result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
// Poor mans string escape
private String b64Transform(final String arg) {
String encoded = Base64.getEncoder().encodeToString(arg.getBytes());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment