Skip to content

Commit

Permalink
#6560: CodeFrame refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslawmalekcodete committed Jan 25, 2018
1 parent 1b14ecd commit 56c89b2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,35 @@
*/
package com.twosigma.beakerx.kernel;

import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem;
import com.twosigma.beakerx.message.Message;

import java.util.Optional;

public abstract class CodeFrame {

private Optional<MagicCommandOutcomeItem> error;
public CodeFrame() {
}

public abstract void executeFrame(Code code, KernelFunctionality kernel, Message message, int executionCount);

public abstract void executeLastFrame(Code code, KernelFunctionality kernel, Message message, int executionCount);

public CodeFrame() {
this.error = Optional.empty();
}

public CodeFrame(MagicCommandOutcomeItem error) {
this.error = Optional.of(error);
}

public Optional<MagicCommandOutcomeItem> getError() {
return error;
public abstract Optional<MagicCommandOutcomeItem> getError();

protected void handleResult(SimpleEvaluationObject seo, TryResult either) {
if (either != null) {
try {
if (either.isResult()) {
seo.finished(either.result());
} else {
seo.error(either.error());
}
} catch (Exception e) {
seo.error(e.getLocalizedMessage());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem;
import com.twosigma.beakerx.message.Message;

import java.util.Optional;

public class PlainCode extends CodeFrame {

private String plainCode;
Expand All @@ -39,25 +42,18 @@ public void executeFrame(Code code, KernelFunctionality kernel, Message message,
handleResult(seo, either);
}

private void handleResult(SimpleEvaluationObject seo, TryResult either) {
try {
if (either.isResult()) {
seo.finished(either.result());
} else {
seo.error(either.error());
}
} catch (Exception e) {
seo.error(e.getLocalizedMessage());
}
}

@Override
public void executeLastFrame(Code code, KernelFunctionality kernel, Message message, int executionCount) {
SimpleEvaluationObject seo = createSimpleEvaluationObject(this.plainCode, kernel, message, executionCount);
TryResult either = kernel.executeCode(this.plainCode, seo);
handleResult(seo, either);
}

@Override
public Optional<MagicCommandOutcomeItem> getError() {
return Optional.empty();
}

public static SimpleEvaluationObject createSimpleEvaluationObject(String code, KernelFunctionality kernel, Message message, int executionCount) {
SimpleEvaluationObject seo = new SimpleEvaluationObject(code);
seo.setJupyterMessage(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@
import com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem;
import com.twosigma.beakerx.message.Message;

import java.util.Optional;

import static org.assertj.core.util.Preconditions.checkNotNull;

public class ErrorCodeFrame extends CodeFrame {

private MagicCommandOutcomeItem magicCommandOutcomeItem;

public ErrorCodeFrame(MagicCommandOutcomeItem magicCommandOutcomeItem) {
super(magicCommandOutcomeItem);
this.magicCommandOutcomeItem = checkNotNull(magicCommandOutcomeItem);
}

@Override
Expand All @@ -33,6 +39,10 @@ public void executeFrame(Code code, KernelFunctionality kernel, Message message,

@Override
public void executeLastFrame(Code code, KernelFunctionality kernel, Message message, int executionCount) {
}

@Override
public Optional<MagicCommandOutcomeItem> getError() {
return Optional.of(magicCommandOutcomeItem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem;
import com.twosigma.beakerx.message.Message;

import java.util.Optional;

import static com.twosigma.beakerx.kernel.handler.MagicCommandExecutor.sendMagicCommandOutcome;
import static com.twosigma.beakerx.kernel.handler.MagicCommandExecutor.sendRepliesWithStatus;
import static java.util.Collections.singletonList;
Expand Down Expand Up @@ -75,18 +77,9 @@ public void executeLastFrame(Code code, KernelFunctionality kernel, Message mess
handleResult(seo, result);
}

private void handleResult(SimpleEvaluationObject seo, TryResult either) {
if (either != null) {
try {
if (either.isResult()) {
seo.finished(either.result());
} else {
seo.error(either.error());
}
} catch (Exception e) {
seo.error(e.getLocalizedMessage());
}
}
@Override
public Optional<MagicCommandOutcomeItem> getError() {
return Optional.empty();
}

private MagicCommandOutcomeItem execute(Code code, int executionCount, boolean showResult) {
Expand Down

0 comments on commit 56c89b2

Please sign in to comment.