Search in sources :

Example 11 with MagicCommandOutput

use of com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput in project beakerx by twosigma.

the class ClasspathAddDynamicMagicCommand method execute.

@Override
public MagicCommandOutcomeItem execute(MagicCommandExecutionParam param) {
    String command = param.getCommand();
    String[] split = splitPath(command);
    if (split.length < 4) {
        return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, WRONG_FORMAT_MSG + CLASSPATH_ADD_DYNAMIC);
    }
    String codeToExecute = command.substring(command.indexOf(DYNAMIC) + DYNAMIC.length()).trim();
    SimpleEvaluationObject seo = createSimpleEvaluationObject(codeToExecute, kernel, param.getCode().getMessage(), param.getExecutionCount());
    TryResult either = kernel.executeCode(codeToExecute, seo);
    if (either.isResult()) {
        try {
            Collection<String> newAddedJars = addJars(either.result());
            if (newAddedJars.isEmpty()) {
                return new MagicCommandOutput(MagicCommandOutput.Status.OK);
            }
            String textMessage = "Added jar" + (newAddedJars.size() > 1 ? "s: " : ": ") + newAddedJars;
            return new MagicCommandOutput(MagicCommandOutput.Status.OK, textMessage);
        } catch (Exception e) {
            return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, "There occurs problem during execution of " + CLASSPATH_ADD_DYNAMIC + " : " + e.getMessage());
        }
    } else {
        return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, "There occurs problem during execution of " + CLASSPATH_ADD_DYNAMIC + " : " + either.error());
    }
}
Also used : MagicCommandOutput(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput) TryResult(com.twosigma.beakerx.TryResult) PlainCode.createSimpleEvaluationObject(com.twosigma.beakerx.kernel.PlainCode.createSimpleEvaluationObject) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)

Example 12 with MagicCommandOutput

use of com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput in project beakerx by twosigma.

the class ClasspathAddMvnMagicCommand method execute.

@Override
public MagicCommandOutcomeItem execute(MagicCommandExecutionParam param) {
    String command = param.getCommand();
    String[] split = MagicCommandUtils.splitPath(command);
    if (!(isGradleFormat(split) || isMavenFormat(split))) {
        return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, ADD_MVN_FORMAT_ERROR_MESSAGE);
    }
    commandParams.setRepos(getRepos().get());
    MavenJarResolver classpathAddMvnCommand = new MavenJarResolver(commandParams, pomFactory);
    MvnLoggerWidget progress = new MvnLoggerWidget(param.getCode().getMessage());
    AddMvnCommandResult result = retrieve(getDependency(split), classpathAddMvnCommand, progress);
    if (result.isJarRetrieved()) {
        return handleAddedJars(classpathAddMvnCommand.getPathToMavenRepo() + "/*");
    }
    return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, result.getErrorMessage());
}
Also used : MagicCommandOutput(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput) AddMvnCommandResult(com.twosigma.beakerx.kernel.magic.command.MavenJarResolver.AddMvnCommandResult) MavenJarResolver(com.twosigma.beakerx.kernel.magic.command.MavenJarResolver)

Example 13 with MagicCommandOutput

use of com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput in project beakerx by twosigma.

the class ClasspathResetMagicCommand method execute.

@Override
public MagicCommandOutcomeItem execute(MagicCommandExecutionParam param) {
    String command = param.getCommand();
    String[] split = splitPath(command);
    if (split.length != 2) {
        return new MagicCommandOutput(Status.ERROR, WRONG_FORMAT_MSG + CLASSPATH_RESET);
    }
    ClasspathAddMvnMagicCommand mvnMagicCommand = MagicCommandTypesFactory.getClasspathAddMvnMagicCommand(kernel);
    mvnMagicCommand.resetRepo();
    try {
        FileUtils.deleteQuietly(new File(mvnMagicCommand.getCommandParams().getPathToCache()));
        FileUtils.deleteQuietly(new File(mvnMagicCommand.getCommandParams().getPathToNotebookJars()));
    } catch (Exception e) {
        return new MagicCommandOutput(Status.ERROR, e.getMessage());
    }
    return new MagicCommandOutput(Status.OK, "Reset done, please restart the kernel.");
}
Also used : MagicCommandOutput(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput) File(java.io.File)

Example 14 with MagicCommandOutput

use of com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput in project beakerx by twosigma.

the class LoadMagicMagicCommand method execute.

@Override
public MagicCommandOutcomeItem execute(MagicCommandExecutionParam param) {
    String command = param.getCommand();
    String[] split = splitPath(command);
    if (split.length != 2) {
        return new MagicCommandOutput(ERROR, WRONG_FORMAT_MSG + LOAD_MAGIC);
    }
    String clazzName = split[1];
    try {
        Class<?> aClass = this.kernel.loadClass(clazzName);
        Object instance = aClass.newInstance();
        if (instance instanceof MagicCommandFunctionality) {
            MagicCommandFunctionality commandFunctionality = (MagicCommandFunctionality) instance;
            kernel.registerMagicCommandType(new MagicCommandType(commandFunctionality.getMagicCommandName(), "", commandFunctionality));
            return new MagicCommandOutput(OK, "Magic command " + commandFunctionality.getMagicCommandName() + " was successfully added.");
        } else {
            return new MagicCommandOutput(ERROR, "Magic command have to implement " + MagicCommandFunctionality.class + " interface.");
        }
    } catch (Exception e) {
        return new MagicCommandOutput(ERROR, e.toString());
    }
}
Also used : MagicCommandOutput(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput) MagicCommandFunctionality(com.twosigma.beakerx.kernel.magic.command.MagicCommandFunctionality) MagicCommandType(com.twosigma.beakerx.kernel.magic.command.MagicCommandType)

Aggregations

MagicCommandOutput (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput)14 TryResult (com.twosigma.beakerx.TryResult)3 SimpleEvaluationObject (com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)3 ImportPath (com.twosigma.beakerx.kernel.ImportPath)3 PlainCode.createSimpleEvaluationObject (com.twosigma.beakerx.kernel.PlainCode.createSimpleEvaluationObject)3 Message (com.twosigma.beakerx.message.Message)3 AddImportStatus (com.twosigma.beakerx.kernel.AddImportStatus)2 Code (com.twosigma.beakerx.kernel.Code)2 MagicCommandFunctionality (com.twosigma.beakerx.kernel.magic.command.MagicCommandFunctionality)2 MavenJarResolver (com.twosigma.beakerx.kernel.magic.command.MavenJarResolver)2 ThreadMXBean (java.lang.management.ThreadMXBean)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 EvaluatorParameters (com.twosigma.beakerx.kernel.EvaluatorParameters)1 KernelFunctionality (com.twosigma.beakerx.kernel.KernelFunctionality)1 MagicCommandType (com.twosigma.beakerx.kernel.magic.command.MagicCommandType)1 AddMvnCommandResult (com.twosigma.beakerx.kernel.magic.command.MavenJarResolver.AddMvnCommandResult)1 MagicCommandResult (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandResult)1 MIMEContainer (com.twosigma.beakerx.mimetype.MIMEContainer)1 File (java.io.File)1