Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit fe24e27

Browse files
committed
Add JupyterService and refactor the way the kernel is executed
1 parent 360aaf3 commit fe24e27

19 files changed

Lines changed: 372 additions & 412 deletions

‎notebooks/Scijava Jython.ipynb‎

Lines changed: 0 additions & 162 deletions
This file was deleted.

‎src/main/java/org/scijava/jupyter/ScijavaKernelConfigurationFile.java‎

Lines changed: 0 additions & 113 deletions
This file was deleted.

‎src/main/java/org/scijava/jupyter/commands/RunScijavaKernel.java‎

Lines changed: 0 additions & 63 deletions
This file was deleted.

‎src/main/java/org/scijava/jupyter/kernel/ScijavaKernel.java‎

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,18 @@
1515
*/
1616
package org.scijava.jupyter.kernel;
1717

18-
1918
import com.twosigma.beaker.jupyter.handler.CommOpenHandler;
20-
import com.twosigma.jupyter.KernelRunner;
2119
import com.twosigma.jupyter.handler.KernelHandler;
2220
import com.twosigma.jupyter.message.Message;
2321
import com.twosigma.jupyter.Kernel;
2422

25-
import java.io.IOException;
26-
27-
import static com.twosigma.beaker.jupyter.Utils.uuid;
2823
import com.twosigma.jupyter.KernelSocketsFactoryImpl;
2924
import org.scijava.Context;
30-
import org.scijava.jupyter.ScijavaKernelConfigurationFile;
31-
import org.scijava.jupyter.comm.ScijavaCommOpenHandler;
32-
import org.scijava.jupyter.evaluator.ScijavaEvaluator;
33-
import org.scijava.jupyter.handler.ScijavaKernelInfoHandler;
25+
import org.scijava.jupyter.kernel.configuration.ScijavaKernelConfigurationFile;
26+
import org.scijava.jupyter.kernel.comm.ScijavaCommOpenHandler;
27+
import org.scijava.jupyter.kernel.evaluator.ScijavaEvaluator;
28+
import org.scijava.jupyter.kernel.handler.ScijavaKernelInfoHandler;
29+
import org.scijava.jupyter.service.JupyterService;
3430
import org.scijava.log.LogService;
3531
import org.scijava.plugin.Parameter;
3632
import org.scijava.script.ScriptLanguage;
@@ -48,7 +44,7 @@ public class ScijavaKernel extends Kernel {
4844
private transient LogService log;
4945

5046
// Ugly but needed (should be fixed upstream soon)
51-
private static ScriptLanguage scriptLanguage;
47+
public static ScriptLanguage scriptLanguage;
5248

5349
private final ScijavaKernelConfigurationFile config;
5450
private final ScijavaEvaluator evaluator;
@@ -67,6 +63,8 @@ public ScijavaKernel(final Context context, final String id, final ScijavaEvalua
6763

6864
this.setLogLevel(config.getLogLevel());
6965
log.info("Log level used is : " + this.config.getLogLevel());
66+
67+
log.info("Scijava Kernel is started and ready to use.");
7068
}
7169

7270
@Override
@@ -99,21 +97,10 @@ private void setLogLevel(String logLevel) {
9997
}
10098
}
10199

102-
public static void main(final String[] args) throws InterruptedException, IOException {
103-
104-
// TODO : Should this be a Scijava command ?
105-
KernelRunner.run(() -> {
106-
107-
Context context = new Context();
108-
String id = uuid();
109-
110-
ScijavaKernelConfigurationFile config = new ScijavaKernelConfigurationFile(context, args);
111-
KernelSocketsFactoryImpl kernelSocketsFactory = new KernelSocketsFactoryImpl(config);
112-
ScijavaEvaluator evaluator = new ScijavaEvaluator(context, id, id, config.getLanguageName());
113-
114-
ScijavaKernel.scriptLanguage = evaluator.getScriptLanguage();
115-
116-
return new ScijavaKernel(context, id, evaluator, config, kernelSocketsFactory);
117-
});
100+
public static void main(String... args) {
101+
Context context = new Context();
102+
JupyterService jupyter = context.service(JupyterService.class);
103+
jupyter.runKernel(args);
104+
context.dispose();
118105
}
119106
}

src/main/java/org/scijava/jupyter/comm/ScijavaCommKernelControlSetShellHandler.java renamed to src/main/java/org/scijava/jupyter/kernel/comm/ScijavaCommKernelControlSetShellHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.scijava.jupyter.comm;
16+
package org.scijava.jupyter.kernel.comm;
1717

1818
import com.twosigma.jupyter.KernelFunctionality;
1919
import com.twosigma.beaker.jupyter.comm.KernelControlGetDefaultShellHandler;
20-
import org.scijava.jupyter.ScijavaVariables;
20+
import org.scijava.jupyter.kernel.configuration.ScijavaVariables;
2121

2222
/**
2323
*

src/main/java/org/scijava/jupyter/comm/ScijavaCommOpenHandler.java renamed to src/main/java/org/scijava/jupyter/kernel/comm/ScijavaCommOpenHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.scijava.jupyter.comm;
16+
package org.scijava.jupyter.kernel.comm;
1717

1818
import com.twosigma.jupyter.KernelFunctionality;
1919
import com.twosigma.jupyter.handler.Handler;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2017 Hadrien Mary.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.scijava.jupyter.kernel.configuration;
17+
18+
import com.twosigma.jupyter.Config;
19+
import com.twosigma.jupyter.ConfigurationFile;
20+
import com.twosigma.jupyter.message.MessageSerializer;
21+
22+
import java.io.File;
23+
import java.io.IOException;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
26+
27+
import org.scijava.Context;
28+
import org.scijava.log.LogService;
29+
import org.scijava.plugin.Parameter;
30+
31+
/**
32+
*
33+
* @author Hadrien Mary
34+
*/
35+
public class ScijavaKernelConfigurationFile implements ConfigurationFile {
36+
37+
@Parameter
38+
private transient LogService log;
39+
40+
private File configFile;
41+
private Config configuration;
42+
43+
private String languageName;
44+
private String logLevel;
45+
46+
public ScijavaKernelConfigurationFile(Context context, String scriptLanguage, String logLevel, Path connectionFile) {
47+
context.inject(this);
48+
this.configFile = connectionFile.toFile();
49+
this.languageName = scriptLanguage;
50+
this.logLevel = logLevel;
51+
}
52+
53+
@Override
54+
public Config getConfig() {
55+
if (configuration == null) {
56+
try {
57+
configuration = MessageSerializer.parse(new String(Files.readAllBytes(this.configFile.toPath())), Config.class);
58+
} catch (IOException ex) {
59+
log.error("Issue loading connection file : " + ex);
60+
}
61+
}
62+
return configuration;
63+
}
64+
65+
public String getLanguageName() {
66+
return this.languageName;
67+
}
68+
69+
public String getLogLevel() {
70+
return this.logLevel;
71+
}
72+
73+
}

src/main/java/org/scijava/jupyter/ScijavaVariables.java renamed to src/main/java/org/scijava/jupyter/kernel/configuration/ScijavaVariables.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.scijava.jupyter;
16+
package org.scijava.jupyter.kernel.configuration;
1717

1818
import com.twosigma.beaker.DefaultJVMVariables;
1919

src/main/java/org/scijava/jupyter/evaluator/ScijavaEvaluator.java renamed to src/main/java/org/scijava/jupyter/kernel/evaluator/ScijavaEvaluator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.scijava.jupyter.evaluator;
16+
package org.scijava.jupyter.kernel.evaluator;
1717

1818
import com.twosigma.beaker.autocomplete.AutocompleteResult;
1919
import com.twosigma.beaker.evaluator.Evaluator;

src/main/java/org/scijava/jupyter/evaluator/Worker.java renamed to src/main/java/org/scijava/jupyter/kernel/evaluator/Worker.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.scijava.jupyter.evaluator;
16+
package org.scijava.jupyter.kernel.evaluator;
1717

1818
import com.twosigma.beaker.jvm.object.SimpleEvaluationObject;
1919
import java.io.Reader;
@@ -27,7 +27,6 @@
2727
import org.scijava.log.LogService;
2828
import org.scijava.module.ModuleException;
2929
import org.scijava.module.ModuleItem;
30-
import org.scijava.module.ModuleService;
3130
import org.scijava.module.event.ModulePostprocessEvent;
3231
import org.scijava.module.event.ModulePreprocessEvent;
3332
import org.scijava.module.process.ModulePostprocessor;
@@ -100,7 +99,6 @@ public void run() {
10099
Object returnValue = null;
101100
try {
102101
returnValue = engine.eval(info.getReader());
103-
log.info(returnValue);
104102
this.seo.finished(returnValue);
105103

106104
} catch (Throwable e) {

0 commit comments

Comments
 (0)