Skip to content

Commit

Permalink
Convert initialization scripts to static imports from the kernel runt…
Browse files Browse the repository at this point in the history
…ime. See #55.
  • Loading branch information
SpencerPark committed May 6, 2019
1 parent 8e7b6b6 commit fe14b73
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 127 deletions.
3 changes: 0 additions & 3 deletions src/main/java/io/github/spencerpark/ijava/IJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public class IJava {
public static final String STARTUP_SCRIPT_KEY = "IJAVA_STARTUP_SCRIPT";

public static final String DEFAULT_SHELL_INIT_RESOURCE_PATH = "ijava-jshell-init.jshell";
public static final String MAGICS_INIT_RESOURCE_PATH = "ijava-magics-init.jshell";
public static final String DISPLAY_INIT_RESOURCE_PATH = "ijava-display-init.jshell";
public static final String EVAL_INIT_RESOURCE_PATH = "ijava-eval-init.jshell";

public static final String VERSION;

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/io/github/spencerpark/ijava/JavaKernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ public JavaKernel() {
.addClasspathFromString(System.getenv(IJava.CLASSPATH_KEY))
.compilerOptsFromString(System.getenv(IJava.COMPILER_OPTS_KEY))
.startupScript(IJava.resource(IJava.DEFAULT_SHELL_INIT_RESOURCE_PATH))
.startupScript(IJava.resource(IJava.MAGICS_INIT_RESOURCE_PATH))
.startupScript(IJava.resource(IJava.DISPLAY_INIT_RESOURCE_PATH))
.startupScript(IJava.resource(IJava.EVAL_INIT_RESOURCE_PATH))
.startupScriptFiles(System.getenv(IJava.STARTUP_SCRIPTS_KEY))
.startupScript(System.getenv(IJava.STARTUP_SCRIPT_KEY))
.timeoutFromString(System.getenv(IJava.TIMEOUT_DURATION_KEY))
Expand Down Expand Up @@ -181,6 +178,13 @@ private List<String> formatCompilationException(CompilationException e) {
List<String> fmt = new ArrayList<>();
SnippetEvent event = e.getBadSnippetCompilation();
Snippet snippet = event.snippet();
System.out.println(snippet.kind());
System.out.println(snippet.subKind());
System.out.println(snippet.source());
System.out.println(event.status());
System.out.println(event.causeSnippet());
System.out.println(event.isSignatureChange());
System.out.println(event.value());
this.evaluator.getShell().diagnostics(snippet)
.forEach(d -> {
// If has line information related, highlight that span
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/io/github/spencerpark/ijava/runtime/Display.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.github.spencerpark.ijava.runtime;

import io.github.spencerpark.ijava.JavaKernel;
import io.github.spencerpark.jupyter.kernel.display.DisplayData;

import java.util.UUID;

public class Display {
public static DisplayData render(Object o) {
JavaKernel kernel = Kernel.getKernelInstance();

if (kernel != null) {
return kernel.getRenderer().render(o);
} else {
throw new RuntimeException("No IJava kernel running");
}
}

public static DisplayData render(Object o, String... as) {
JavaKernel kernel = Kernel.getKernelInstance();

if (kernel != null) {
return kernel.getRenderer().renderAs(o, as);
} else {
throw new RuntimeException("No IJava kernel running");
}
}

public static String display(Object o) {
JavaKernel kernel = Kernel.getKernelInstance();

if (kernel != null) {
DisplayData data = kernel.getRenderer().render(o);

String id = data.getDisplayId();
if (id == null) {
id = UUID.randomUUID().toString();
data.setDisplayId(id);
}

kernel.display(data);

return id;
} else {
throw new RuntimeException("No IJava kernel running");
}
}

public static String display(Object o, String... as) {
JavaKernel kernel = Kernel.getKernelInstance();

if (kernel != null) {
DisplayData data = kernel.getRenderer().renderAs(o, as);

String id = data.getDisplayId();
if (id == null) {
id = UUID.randomUUID().toString();
data.setDisplayId(id);
}

kernel.display(data);

return id;
} else {
throw new RuntimeException("No IJava kernel running");
}
}

public static void updateDisplay(String id, Object o) {
JavaKernel kernel = Kernel.getKernelInstance();

if (kernel != null) {
DisplayData data = kernel.getRenderer().render(o);
kernel.getIO().display.updateDisplay(id, data);
} else {
throw new RuntimeException("No IJava kernel running");
}
}

public static void updateDisplay(String id, Object o, String... as) {
JavaKernel kernel = Kernel.getKernelInstance();

if (kernel != null) {
DisplayData data = kernel.getRenderer().renderAs(o, as);
kernel.getIO().display.updateDisplay(id, data);
} else {
throw new RuntimeException("No IJava kernel running");
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/io/github/spencerpark/ijava/runtime/Kernel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.spencerpark.ijava.runtime;

import io.github.spencerpark.ijava.IJava;
import io.github.spencerpark.ijava.JavaKernel;

public class Kernel {
public static JavaKernel getKernelInstance() {
return IJava.getKernelInstance();
}

public static Object eval(String expr) throws Exception {
JavaKernel kernel = getKernelInstance();

if (kernel != null) {
return kernel.evalRaw(expr);
} else {
throw new RuntimeException("No IJava kernel running");
}
}
}
41 changes: 41 additions & 0 deletions src/main/java/io/github/spencerpark/ijava/runtime/Magics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.spencerpark.ijava.runtime;

import io.github.spencerpark.ijava.IJava;
import io.github.spencerpark.ijava.JavaKernel;
import io.github.spencerpark.jupyter.kernel.magic.registry.UndefinedMagicException;

import java.util.List;

public class Magics {
public static <T> T lineMagic(String name, List<String> args) {
JavaKernel kernel = IJava.getKernelInstance();

if (kernel != null) {
try {
return kernel.getMagics().applyLineMagic(name, args);
} catch (UndefinedMagicException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(String.format("Exception occurred while running line magic '%s': %s", name, e.getMessage()), e);
}
} else {
throw new RuntimeException("No IJava kernel running");
}
}

public static <T> T cellMagic(String name, List<String> args, String body) {
JavaKernel kernel = IJava.getKernelInstance();

if (kernel != null) {
try {
return kernel.getMagics().applyCellMagic(name, args, body);
} catch (UndefinedMagicException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(String.format("Exception occurred while running cell magic '%s': %s", name, e.getMessage()), e);
}
} else {
throw new RuntimeException("No IJava kernel running");
}
}
}
81 changes: 0 additions & 81 deletions src/main/resources/ijava-display-init.jshell

This file was deleted.

9 changes: 0 additions & 9 deletions src/main/resources/ijava-eval-init.jshell

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/resources/ijava-jshell-init.jshell
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import java.util.concurrent.*;
import java.util.prefs.*;
import java.util.regex.*;

import static io.github.spencerpark.ijava.runtime.Display.*;
import static io.github.spencerpark.ijava.runtime.Kernel.*;
import static io.github.spencerpark.ijava.runtime.Magics.*;

public void printf(String format, Object... args) {
System.out.printf(format, args);
}
31 changes: 0 additions & 31 deletions src/main/resources/ijava-magics-init.jshell

This file was deleted.

0 comments on commit fe14b73

Please sign in to comment.