|
| 1 | +#include <conduit.hpp> |
| 2 | +#include <conduit_cpp_to_c.hpp> |
| 3 | +// conduit python module capi header |
| 4 | +#include "conduit_python.hpp" |
| 5 | +// embedded interp |
| 6 | +#include "python_interpreter.hpp" |
| 7 | + |
| 8 | +// single python interp instance for our example. |
| 9 | +PythonInterpreter *interp = NULL; |
| 10 | + |
| 11 | +extern "C" { |
| 12 | + |
| 13 | + //---------------------------------------------------------------------------- |
| 14 | + // returns our static instance of our python interpreter |
| 15 | + // if not already inited initializes it |
| 16 | + //---------------------------------------------------------------------------- |
| 17 | + |
| 18 | + PythonInterpreter *init_python_interpreter() |
| 19 | + { |
| 20 | + if( interp == NULL) |
| 21 | + { |
| 22 | + interp = new PythonInterpreter(); |
| 23 | + if( !interp->initialize() ) |
| 24 | + { |
| 25 | + std::cout << "ERROR: interp->initialize() failed " << std::endl; |
| 26 | + return NULL; |
| 27 | + } |
| 28 | + // setup for conduit python c api |
| 29 | + if(!interp->run_script("import conduit")) |
| 30 | + { |
| 31 | + std::cout << "ERROR: `import conduit` failed" << std::endl; |
| 32 | + return NULL; |
| 33 | + } |
| 34 | + |
| 35 | + if(import_conduit() < 0) |
| 36 | + { |
| 37 | + std::cout << "failed to import Conduit Python C-API"; |
| 38 | + return NULL; |
| 39 | + } |
| 40 | + |
| 41 | + // Turn this on if you want to see every line |
| 42 | + // the python interpreter executes |
| 43 | + //interp->set_echo(true); |
| 44 | + } |
| 45 | + return interp; |
| 46 | + } |
| 47 | + |
| 48 | + //---------------------------------------------------------------------------- |
| 49 | + // access node passed from fortran to python |
| 50 | + //---------------------------------------------------------------------------- |
| 51 | + |
| 52 | + void conduit_fort_to_py(conduit_node *data, const char *py_script) { |
| 53 | + // create python interpreter |
| 54 | + PythonInterpreter *pyintp = init_python_interpreter(); |
| 55 | + |
| 56 | + // add extra system paths |
| 57 | + // pyintp->add_system_path("/usr/local/lib/python3.9/site-packages"); |
| 58 | + |
| 59 | + // show code |
| 60 | + // pyintp->set_echo(true); |
| 61 | + |
| 62 | + // get global dict and insert wrapped conduit node |
| 63 | + PyObject *py_mod_dict = pyintp->global_dict(); |
| 64 | + |
| 65 | + // get cpp ref to passed node |
| 66 | + conduit::Node &n = conduit::cpp_node_ref(data); |
| 67 | + |
| 68 | + // create py object to wrap the conduit node |
| 69 | + PyObject *py_node = PyConduit_Node_Python_Wrap(&n, 0); // python owns => false |
| 70 | + |
| 71 | + // my_node is set in here statically, it will be used to access node under python |
| 72 | + pyintp->set_dict_object(py_mod_dict, py_node, "my_node"); |
| 73 | + |
| 74 | + // trigger script |
| 75 | + bool err = pyintp->run_script_file(py_script, py_mod_dict); |
| 76 | + |
| 77 | + // check error |
| 78 | + if (err) { |
| 79 | + pyintp->check_error(); |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments