1 package org.simantics.pythonlink;
\r
3 import org.simantics.scl.runtime.SCLContext;
\r
4 import org.simantics.scl.runtime.function.Function;
\r
5 import org.simantics.scl.runtime.tuple.Tuple0;
\r
7 public class Python {
\r
8 private static final String PYTHON_CONTEXT = "Simantics/Python/Python";
\r
10 public static PythonContext openPythonContext() {
\r
11 return new PythonContext();
\r
14 @SuppressWarnings( { "unchecked", "rawtypes" } )
\r
15 public static Object runPython(Function f) {
\r
16 SCLContext sclContext = SCLContext.getCurrent();
\r
17 Object oldContext = sclContext.get(PYTHON_CONTEXT);
\r
18 try (PythonContext newContext = openPythonContext()) {
\r
19 sclContext.put(PYTHON_CONTEXT, newContext);
\r
20 return f.apply(Tuple0.INSTANCE);
\r
23 sclContext.put(PYTHON_CONTEXT, oldContext);
\r
27 @SuppressWarnings( { "unchecked", "rawtypes" } )
\r
28 public static Object runWithPythonContext(PythonContext context, Function f) {
\r
29 SCLContext sclContext = SCLContext.getCurrent();
\r
30 Object oldContext = sclContext.get(PYTHON_CONTEXT);
\r
32 sclContext.put(PYTHON_CONTEXT, context);
\r
33 return f.apply(Tuple0.INSTANCE);
\r
36 sclContext.put(PYTHON_CONTEXT, oldContext);
\r