Skip to content

Commit f85aaac

Browse files
committed
Keep developing and testing the script language
1 parent a6672bd commit f85aaac

File tree

5 files changed

+73
-53
lines changed

5 files changed

+73
-53
lines changed

pom.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
<!-- NB: Deploy releases to the SciJava Maven repository. -->
9090
<releaseProfiles>sign,deploy-to-scijava</releaseProfiles>
9191

92-
<appose.version>0.10.0</appose.version>
92+
<appose.version>0.11.0</appose.version>
93+
<imglib2-appose.version>0.9.0</imglib2-appose.version>
9394
</properties>
9495

9596
<dependencies>
@@ -121,9 +122,15 @@
121122
<scope>test</scope>
122123
</dependency>
123124
<dependency>
124-
<groupId>org.scijava</groupId>
125-
<artifactId>scijava-common</artifactId>
126-
<classifier>tests</classifier>
125+
<groupId>net.imglib2</groupId>
126+
<artifactId>imglib2-appose</artifactId>
127+
<version>${imglib2-appose.version}</version>
128+
<scope>test</scope>
129+
</dependency>
130+
<dependency>
131+
<!-- For net.imglib2.img.Img parameter handling. -->
132+
<groupId>net.imagej</groupId>
133+
<artifactId>imagej-common</artifactId>
127134
<scope>test</scope>
128135
</dependency>
129136
</dependencies>

src/main/java/org/scijava/plugins/scripting/appose/python/ApposePythonScriptEngine.java

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import java.util.concurrent.ConcurrentHashMap;
4242

4343
import javax.script.Bindings;
44-
import javax.script.ScriptContext;
4544
import javax.script.ScriptEngine;
4645
import javax.script.ScriptException;
4746

@@ -85,10 +84,6 @@
8584
*/
8685
public class ApposePythonScriptEngine extends AbstractScriptEngine {
8786

88-
/** Cache of built environments, keyed by resolved env file absolute path. */
89-
private static final Map<String, Environment> ENV_CACHE =
90-
new ConcurrentHashMap<>();
91-
9287
@Parameter
9388
private ConvertService convertService;
9489

@@ -235,9 +230,8 @@ public Bindings createBindings() {
235230
// -- Helper methods --
236231

237232
/**
238-
* Lazily builds (or fetches from cache) the Appose {@link Environment}
239-
* described by the {@code env} and {@code scheme} attributes of the script's
240-
* {@code #@script} directive.
233+
* Lazily builds the Appose {@link Environment} described by the {@code env}
234+
* and {@code scheme} attributes of the script's {@code #@script} directive.
241235
*/
242236
private Environment buildEnvironment(final ScriptInfo info)
243237
throws ScriptException
@@ -247,46 +241,21 @@ private Environment buildEnvironment(final ScriptInfo info)
247241
if (envRef == null) return null;
248242

249243
final File envFile = resolveEnvFile(envRef, info.getPath());
250-
final String cacheKey = envFile.getAbsolutePath();
251244
final String envName = sanitizeEnvName(info.getPath());
252245

246+
log.info("Building Appose environment: " + envName);
253247
try {
254-
return ENV_CACHE.computeIfAbsent(cacheKey, key -> {
255-
log.info("Building Appose environment: " + key);
256-
try {
257-
return selectBuilder(key, info.get("scheme")).name(envName).build();
258-
}
259-
catch (final BuildException e) {
260-
throw new RuntimeException(
261-
"Failed to build Appose environment: " + e.getMessage(), e);
262-
}
263-
});
264-
}
265-
catch (final RuntimeException e) {
266-
if (e.getCause() instanceof BuildException) {
267-
throw new ScriptException(e.getMessage());
268-
}
269-
throw e;
270-
}
271-
}
248+
String scheme = info.get("scheme");
249+
Builder<?> builder = scheme == null ?
250+
Appose.file(envFile) : Appose.file(envFile).scheme(scheme);
272251

273-
/**
274-
* Selects the appropriate Appose builder based on the {@code scheme} hint.
275-
* Falls back to auto-detection ({@link Appose#file}) if unspecified or
276-
* unrecognized.
277-
*/
278-
private Builder<?> selectBuilder(final String envFilePath,
279-
final String scheme) throws BuildException
280-
{
281-
if (scheme == null || scheme.isEmpty()) return Appose.file(envFilePath);
282-
switch (scheme.toLowerCase()) {
283-
case "pixi.toml": return Appose.pixi(envFilePath);
284-
case "environment.yml": return Appose.mamba(envFilePath);
285-
case "requirements.txt":
286-
case "pyproject.toml": return Appose.uv(envFilePath);
287-
default:
288-
log.warn("Unrecognized scheme '" + scheme + "'; using auto-detection.");
289-
return Appose.file(envFilePath);
252+
return builder.name(envName).build();
253+
}
254+
catch (final BuildException e) {
255+
ScriptException se = new ScriptException(
256+
"Failed to build Appose environment");
257+
se.initCause(e);
258+
throw se;
290259
}
291260
}
292261

@@ -308,9 +277,9 @@ private static File resolveEnvFile(final String envRef,
308277
* all non-alphanumeric characters (except {@code -}) with {@code _}.
309278
*/
310279
private static String sanitizeEnvName(final String scriptPath) {
311-
if (scriptPath == null) return "appose-python";
280+
if (scriptPath == null) return "scripting-appose-python";
312281
return new File(scriptPath).getAbsolutePath()
313-
.replaceAll("[^a-zA-Z0-9_-]", "_");
282+
.replaceAll("[^a-zA-Z0-9_]", "-");
314283
}
315284

316285
/**

src/main/resources/script_templates/appose-python/StarDist_cellcast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!appose-python
2-
#@script (language="appose-python", env="cellcast-stardist.toml", scheme="pixi.toml")
2+
#@script (language="appose-python", env="cellcast.toml", scheme="pixi.toml")
33

44
#@ Img image
55
#@output Img labels
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[workspace]
2+
authors = ["Curtis Rueden <ctrueden@wisc.edu>"]
3+
channels = ["conda-forge"]
4+
name = "stardist-cellcast"
5+
platforms = ["osx-arm64", "linux-64", "win-64"]
6+
version = "0.1.0"
7+
8+
[dependencies]
9+
python = ">=3.14.3,<3.15"
10+
11+
[pypi-dependencies]
12+
cellcast = ">=0.1.2, <0.2"
13+
appose = ">=0.11.0, <0.12"

src/main/java/org/scijava/plugins/scripting/appose/python/Main.java renamed to src/test/java/org/scijava/plugins/scripting/appose/python/Main.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,42 @@
2929

3030
package org.scijava.plugins.scripting.appose.python;
3131

32-
import org.scijava.script.ScriptREPL;
32+
import net.imagej.Dataset;
33+
import net.imagej.DatasetService;
34+
import net.imagej.axis.Axes;
35+
import net.imagej.axis.AxisType;
36+
import org.scijava.Context;
37+
import org.scijava.ui.UIService;
38+
import org.scijava.ui.swing.script.TextEditor;
39+
40+
import java.awt.event.WindowAdapter;
41+
import java.awt.event.WindowEvent;
3342

3443
public class Main {
3544

3645
public static void main(String... args) throws Exception {
37-
ScriptREPL.main(args);
46+
final Context ctx = new Context();
47+
48+
long[] dims = {512, 384};
49+
String name = "blank";
50+
AxisType[] axes = {Axes.X, Axes.Y};
51+
int bitsPerPixel = 8;
52+
boolean signed = false;
53+
boolean floating = false;
54+
UIService ui = ctx.service(UIService.class);
55+
ui.showUI();
56+
DatasetService ds = ctx.service(DatasetService.class);
57+
Dataset dataset = ds.create(dims, name, axes, bitsPerPixel, signed, floating);
58+
ui.show(dataset);
59+
System.out.println(dataset);
60+
61+
TextEditor scriptEditor = new TextEditor(ctx);
62+
scriptEditor.setVisible(true);
63+
scriptEditor.addWindowListener(new WindowAdapter() {
64+
@Override
65+
public void windowClosed(WindowEvent e) {
66+
ctx.dispose();
67+
}
68+
});
3869
}
3970
}

0 commit comments

Comments
 (0)