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 runPythonF(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 runWithPythonContextF(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); } } }