|
| 1 | +#include "python_interpreter.h" |
| 2 | +#include "notebook.h" |
| 3 | +#include "config.h" |
| 4 | +#include <iostream> |
| 5 | +#include <pygobject.h> |
| 6 | + |
| 7 | +inline pybind11::module pyobject_from_gobj(gpointer ptr){ |
| 8 | + auto obj=G_OBJECT(ptr); |
| 9 | + if(obj) |
| 10 | + return pybind11::module(pygobject_new(obj), false); |
| 11 | + return pybind11::module(Py_None, false); |
| 12 | +} |
| 13 | + |
| 14 | +PythonInterpreter::PythonInterpreter(){ |
| 15 | + auto init_juci_api=[](){ |
| 16 | + pybind11::module(pygobject_init(-1,-1,-1), false); |
| 17 | + pybind11::module api("jucpp", "Python bindings for juCi++"); |
| 18 | + api.def("get_current_text_buffer", [](){ |
| 19 | + auto view=Notebook::get().get_current_view(); |
| 20 | + if(view) |
| 21 | + return pyobject_from_gobj(view->gobj()); |
| 22 | + return pybind11::module(Py_None, false); |
| 23 | + }); |
| 24 | + return api.ptr(); |
| 25 | + }; |
| 26 | + PyImport_AppendInittab("jucipp", init_juci_api); |
| 27 | + Py_Initialize(); |
| 28 | + // long unsigned size = 0L; |
| 29 | + // argv=Py_DecodeLocale("",&size); |
| 30 | + // PySys_SetArgv(0,&argv); |
| 31 | + Config::get().load(); |
| 32 | + add_path("/usr/lib/python3.5/site-packages");// Config::get().python.site_packages); |
| 33 | + auto plugin_path=Config::get().juci_home_path()/"plugins"; // Config::get().python.plugin_directory; |
| 34 | + add_path(plugin_path); |
| 35 | + boost::filesystem::directory_iterator end_it; |
| 36 | + for(boost::filesystem::directory_iterator it(plugin_path);it!=end_it;it++){ |
| 37 | + auto module_name=it->path().stem().string(); |
| 38 | + if(module_name!="__pycache__"){ |
| 39 | + auto module=import(module_name); |
| 40 | + if(!module){ |
| 41 | + auto err=PythonError(); |
| 42 | + if(err) |
| 43 | + std::cerr << std::string(err) << std::endl; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +pybind11::module PythonInterpreter::get_loaded_module(const std::string &module_name){ |
| 50 | + return pybind11::module(PyImport_AddModule(module_name.c_str()), true); |
| 51 | +} |
| 52 | + |
| 53 | +pybind11::module PythonInterpreter::import(const std::string &module_name){ |
| 54 | + return pybind11::module(PyImport_ImportModule(module_name.c_str()), false); |
| 55 | +} |
| 56 | + |
| 57 | +void PythonInterpreter::add_path(const boost::filesystem::path &path){ |
| 58 | + std::wstring sys_path(Py_GetPath()); |
| 59 | + if(!sys_path.empty()) |
| 60 | +#ifdef _WIN32 |
| 61 | + sys_path += ';'; |
| 62 | +#else |
| 63 | + sys_path += ':'; |
| 64 | +#endif |
| 65 | + sys_path += path.generic_wstring(); |
| 66 | + Py_SetPath(sys_path.c_str()); |
| 67 | +} |
| 68 | + |
| 69 | +PythonInterpreter::~PythonInterpreter(){ |
| 70 | + auto err=PythonError(); |
| 71 | + if(Py_IsInitialized()) |
| 72 | + Py_Finalize(); |
| 73 | + if(err) |
| 74 | + std::cerr << std::string(err) << std::endl; |
| 75 | +} |
| 76 | + |
| 77 | +PythonError::PythonError(){ |
| 78 | + pybind11::object error(PyErr_Occurred(), false); |
| 79 | + if(error){ |
| 80 | + PyObject *exception,*value,*traceback; |
| 81 | + PyErr_Fetch(&exception,&value,&traceback); |
| 82 | + PyErr_NormalizeException(&exception,&value,&traceback); |
| 83 | + try{ |
| 84 | + exp=std::string(pybind11::object(exception,false).str()); |
| 85 | + val=std::string(pybind11::object(value,false).str()); |
| 86 | + trace=std::string(pybind11::object(traceback,false).str()); |
| 87 | + } catch (const std::runtime_error &e){ |
| 88 | + exp=e.what(); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +PythonError::operator std::string(){ |
| 94 | + return exp + "\n" + val + "\n" + trace; |
| 95 | +} |
| 96 | + |
| 97 | +PythonError::operator bool(){ |
| 98 | + return !exp.empty(); |
| 99 | +} |
0 commit comments