X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.pythonlink%2Fsrc%2Forg%2Fsimantics%2Fpythonlink%2FPython.java;fp=org.simantics.pythonlink%2Fsrc%2Forg%2Fsimantics%2Fpythonlink%2FPython.java;h=af5c9e287b424090137d83f721fd278c5e01758f;hb=52bef206878a4384b43494243dd39813b2bdf5ad;hp=0000000000000000000000000000000000000000;hpb=034e25e65348f61edc54354e94b0ae9ea8b58317;p=simantics%2Fpython.git diff --git a/org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java b/org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java new file mode 100644 index 0000000..af5c9e2 --- /dev/null +++ b/org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java @@ -0,0 +1,39 @@ +package org.simantics.pythonlink; + +import org.simantics.scl.runtime.SCLContext; +import org.simantics.scl.runtime.function.Function; +import org.simantics.scl.runtime.tuple.Tuple0; + +public class Python { + private static final String PYTHON_CONTEXT = "Simantics/Python/Python"; + + public static PythonContext openPythonContext() { + return new PythonContext(); + } + + @SuppressWarnings( { "unchecked", "rawtypes" } ) + public static Object runPython(Function f) { + SCLContext sclContext = SCLContext.getCurrent(); + Object oldContext = sclContext.get(PYTHON_CONTEXT); + try (PythonContext newContext = openPythonContext()) { + sclContext.put(PYTHON_CONTEXT, newContext); + return f.apply(Tuple0.INSTANCE); + } + finally { + sclContext.put(PYTHON_CONTEXT, oldContext); + } + } + + @SuppressWarnings( { "unchecked", "rawtypes" } ) + public static Object runWithPythonContext(PythonContext context, Function f) { + SCLContext sclContext = SCLContext.getCurrent(); + Object oldContext = sclContext.get(PYTHON_CONTEXT); + try { + sclContext.put(PYTHON_CONTEXT, context); + return f.apply(Tuple0.INSTANCE); + } + finally { + sclContext.put(PYTHON_CONTEXT, oldContext); + } + } +}