Search in sources :

Example 1 with ImportPath

use of com.twosigma.beakerx.kernel.ImportPath in project beakerx by twosigma.

the class UnImportMagicCommand method execute.

@Override
public MagicCommandOutcomeItem execute(MagicCommandExecutionParam param) {
    String command = param.getCommand();
    String[] parts = MagicCommandUtils.splitPath(command);
    if (parts.length != 2) {
        return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, WRONG_FORMAT_MSG);
    }
    this.kernel.removeImport(new ImportPath(parts[1]));
    return new MagicCommandOutput(MagicCommandOutput.Status.OK);
}
Also used : MagicCommandOutput(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput) ImportPath(com.twosigma.beakerx.kernel.ImportPath)

Example 2 with ImportPath

use of com.twosigma.beakerx.kernel.ImportPath in project beakerx by twosigma.

the class AddImportMagicCommand method execute.

@Override
public MagicCommandOutcomeItem execute(MagicCommandExecutionParam param) {
    String command = param.getCommand();
    String[] parts = MagicCommandUtils.splitPath(command);
    if (parts.length != 2) {
        return new MagicCommandOutput(ERROR, WRONG_FORMAT_MSG + IMPORT);
    }
    ImportPath anImport = new ImportPath(parts[1]);
    AddImportStatus status = this.kernel.addImport(anImport);
    if (AddImportStatus.ERROR.equals(status)) {
        return new MagicCommandOutput(ERROR, "Could not import " + parts[1]);
    }
    return new MagicCommandOutput(OK);
}
Also used : AddImportStatus(com.twosigma.beakerx.kernel.AddImportStatus) MagicCommandOutput(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput) ImportPath(com.twosigma.beakerx.kernel.ImportPath)

Example 3 with ImportPath

use of com.twosigma.beakerx.kernel.ImportPath in project beakerx by twosigma.

the class ClojureEvaluator method doResetEnvironment.

@Override
protected void doResetEnvironment() {
    init();
    ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(loader);
    for (ImportPath s : imports.getImportPaths()) {
        addImportPathToShell(s);
    }
    for (String s : requirements) {
        if (s != null && !s.isEmpty())
            try {
                clojureLoadString.invoke(String.format("(require '%s)", s));
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
    }
    Thread.currentThread().setContextClassLoader(oldLoader);
    executorService.shutdown();
    executorService = Executors.newSingleThreadExecutor();
}
Also used : ImportPath(com.twosigma.beakerx.kernel.ImportPath) DynamicClassLoader(clojure.lang.DynamicClassLoader) IOException(java.io.IOException)

Example 4 with ImportPath

use of com.twosigma.beakerx.kernel.ImportPath in project beakerx by twosigma.

the class ImportMagicCommandTest method addImport.

@Test
public void addImport() {
    // given
    String allCode = "%import com.twosigma.beakerx.widget.IntSlider\n" + "w = new IntSlider()";
    Code code = CodeFactory.create(allCode, new Message(), kernel);
    // when
    code.execute(kernel, 1);
    // then
    PlainCode actual = (PlainCode) code.getCodeFrames().get(1);
    assertThat(actual.getPlainCode()).isEqualTo("w = new IntSlider()");
    assertThat(kernel.getImports().getImportPaths()).contains(new ImportPath("com.twosigma.beakerx.widget.IntSlider"));
}
Also used : Message(com.twosigma.beakerx.message.Message) ImportPath(com.twosigma.beakerx.kernel.ImportPath) PlainCode(com.twosigma.beakerx.kernel.PlainCode) Code(com.twosigma.beakerx.kernel.Code) PlainCode(com.twosigma.beakerx.kernel.PlainCode) EvaluatorTest(com.twosigma.beakerx.evaluator.EvaluatorTest) Test(org.junit.Test) KernelTest(com.twosigma.beakerx.KernelTest)

Example 5 with ImportPath

use of com.twosigma.beakerx.kernel.ImportPath in project beakerx by twosigma.

the class Codev method configurePackage.

private String configurePackage() {
    String pname = javaEvaluator.getPackageId();
    Codev.CodeLine codeLine = getNotBlankLine();
    Pattern p = Pattern.compile("\\s*package\\s+((?:[a-zA-Z]\\w*)(?:\\.[a-zA-Z]\\w*)*);.*");
    Matcher m = p.matcher(codeLine.getLine());
    if (m.matches()) {
        pname = m.group(1);
        lineNumbersMapping.put(1, codeLine.getIndex());
        moveToNextLine();
    }
    javaSourceCode.append("package ");
    javaSourceCode.append(pname);
    javaSourceCode.append(";\n");
    for (ImportPath i : javaEvaluator.getImports().getImportPaths()) {
        javaSourceCode.append("import ");
        javaSourceCode.append(i.asString());
        javaSourceCode.append(";\n");
    }
    return pname;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ImportPath(com.twosigma.beakerx.kernel.ImportPath)

Aggregations

ImportPath (com.twosigma.beakerx.kernel.ImportPath)10 KernelTest (com.twosigma.beakerx.KernelTest)3 EvaluatorTest (com.twosigma.beakerx.evaluator.EvaluatorTest)3 Code (com.twosigma.beakerx.kernel.Code)3 PlainCode (com.twosigma.beakerx.kernel.PlainCode)3 MagicCommandOutput (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput)3 Message (com.twosigma.beakerx.message.Message)3 Test (org.junit.Test)3 AddImportStatus (com.twosigma.beakerx.kernel.AddImportStatus)2 DynamicClassLoader (clojure.lang.DynamicClassLoader)1 Imports (com.twosigma.beakerx.kernel.Imports)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 NotNull (org.jetbrains.annotations.NotNull)1