]> gerrit.simantics Code Review - simantics/python.git/blob - org.simantics.pythonlink/src/org/simantics/pythonlink/Python.java
Initial commit of Python Integration feature.
[simantics/python.git] / org.simantics.pythonlink / src / org / simantics / pythonlink / Python.java
1 package org.simantics.pythonlink;\r
2 \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
6 \r
7 public class Python {\r
8     private static final String PYTHON_CONTEXT = "Simantics/Python/Python";\r
9 \r
10     public static PythonContext openPythonContext() {\r
11         return new PythonContext();\r
12     }\r
13     \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
21         }\r
22         finally {\r
23             sclContext.put(PYTHON_CONTEXT, oldContext);\r
24         }\r
25     }\r
26     \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
31         try {\r
32             sclContext.put(PYTHON_CONTEXT, context);\r
33             return f.apply(Tuple0.INSTANCE);\r
34         }\r
35         finally {\r
36             sclContext.put(PYTHON_CONTEXT, oldContext);\r
37         }\r
38     }    \r
39 }\r