|
| 1 | +#include "python_interpreter.h" |
| 2 | +#include "notebook.h" |
| 3 | +#include "config.h" |
| 4 | +#include <iostream> |
| 5 | +#include <pygobject.h> |
| 6 | +#include "menu.h" |
| 7 | +#include "directories.h" |
| 8 | + |
| 9 | +inline pybind11::module pyobject_from_gobj(gpointer ptr){ |
| 10 | + auto obj=G_OBJECT(ptr); |
| 11 | + if(obj) |
| 12 | + return pybind11::module(pygobject_new(obj), false); |
| 13 | + return pybind11::module(Py_None, false); |
| 14 | +} |
| 15 | + |
| 16 | +Python::Interpreter::Interpreter(){ |
| 17 | +#ifdef _WIN32 |
| 18 | + auto root_path=Config::get().terminal.msys2_mingw_path; |
| 19 | + append_path(root_path/"include/python3.5m"); |
| 20 | + append_path(root_path/"lib/python3.5"); |
| 21 | + long long unsigned size = 0L; |
| 22 | +#else |
| 23 | + long unsigned size = 0L; |
| 24 | +#endif |
| 25 | + auto init_juci_api=[](){ |
| 26 | + pybind11::module(pygobject_init(-1,-1,-1),false); |
| 27 | + pybind11::module api("jucpp","Python bindings for juCi++"); |
| 28 | + api |
| 29 | + .def("get_juci_home",[](){return Config::get().juci_home_path().string();}) |
| 30 | + .def("get_plugin_folder",[](){return Config::get().python.plugin_directory;}); |
| 31 | + api |
| 32 | + .def_submodule("editor") |
| 33 | + .def("get_current_gtk_source_view",[](){ |
| 34 | + auto view=Notebook::get().get_current_view(); |
| 35 | + if(view) |
| 36 | + return pyobject_from_gobj(view->gobj()); |
| 37 | + return pybind11::module(Py_None,false); |
| 38 | + }) |
| 39 | + .def("get_file_path",[](){ |
| 40 | + auto view=Notebook::get().get_current_view(); |
| 41 | + if(view) |
| 42 | + return view->file_path.string(); |
| 43 | + return std::string(); |
| 44 | + }); |
| 45 | + api |
| 46 | + .def("get_gio_plugin_menu",[](){ |
| 47 | + auto &plugin_menu=Menu::get().plugin_menu; |
| 48 | + if(!plugin_menu){ |
| 49 | + plugin_menu=Gio::Menu::create(); |
| 50 | + plugin_menu->append("<empty>"); |
| 51 | + Menu::get().window_menu->append_submenu("_Plugins",plugin_menu); |
| 52 | + } |
| 53 | + return pyobject_from_gobj(plugin_menu->gobj()); |
| 54 | + }) |
| 55 | + .def("get_gio_window_menu",[](){return pyobject_from_gobj(Menu::get().window_menu->gobj());}) |
| 56 | + .def("get_gio_juci_menu",[](){return pyobject_from_gobj(Menu::get().juci_menu->gobj());}) |
| 57 | + .def("get_gtk_notebook",[](){return pyobject_from_gobj(Notebook::get().gobj());}) |
| 58 | + .def_submodule("terminal") |
| 59 | + .def("get_gtk_text_view",[](){return pyobject_from_gobj(Terminal::get().gobj());}) |
| 60 | + .def("println", [](const std::string &message){ Terminal::get().print(message +"\n"); }); |
| 61 | + api.def_submodule("directories") |
| 62 | + .def("get_gtk_treeview",[](){return pyobject_from_gobj(Directories::get().gobj());}) |
| 63 | + .def("open",[](const std::string &directory){Directories::get().open(directory);}) |
| 64 | + .def("update",[](){Directories::get().update();}); |
| 65 | + return api.ptr(); |
| 66 | + }; |
| 67 | + PyImport_AppendInittab("jucipp", init_juci_api); |
| 68 | + Config::get().load(); |
| 69 | + auto plugin_path=Config::get().python.plugin_directory; |
| 70 | + add_path(Config::get().python.site_packages); |
| 71 | + add_path(plugin_path); |
| 72 | + Py_Initialize(); |
| 73 | + argv=Py_DecodeLocale("",&size); |
| 74 | + PySys_SetArgv(0,&argv); |
| 75 | + auto sys=get_loaded_module("sys"); |
| 76 | + auto exc_func=[](pybind11::object type,pybind11::object value,pybind11::object traceback){ |
| 77 | + if(!given_exception_matches(type,PyExc_SyntaxError)) |
| 78 | + Terminal::get().print(Error(type,value,traceback),true); |
| 79 | + else |
| 80 | + Terminal::get().print(SyntaxError(type,value,traceback),true); |
| 81 | + }; |
| 82 | + sys.attr("excepthook")=pybind11::cpp_function(exc_func); |
| 83 | + boost::filesystem::directory_iterator end_it; |
| 84 | + for(boost::filesystem::directory_iterator it(plugin_path);it!=end_it;it++){ |
| 85 | + auto module_name=it->path().stem().string(); |
| 86 | + if(module_name.empty()) |
| 87 | + break; |
| 88 | + auto is_directory=boost::filesystem::is_directory(it->path()); |
| 89 | + auto has_py_extension=it->path().extension()==".py"; |
| 90 | + auto is_pycache=module_name=="__pycache__"; |
| 91 | + if((is_directory && !is_pycache)||has_py_extension){ |
| 92 | + auto module=import(module_name); |
| 93 | + if(!module){ |
| 94 | + auto msg="Error loading plugin `"+module_name+"`:\n"; |
| 95 | + auto err=std::string(Error()); |
| 96 | + Terminal::get().print(msg+err+"\n"); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +pybind11::module Python::get_loaded_module(const std::string &module_name){ |
| 103 | + return pybind11::module(PyImport_AddModule(module_name.c_str()), true); |
| 104 | +} |
| 105 | + |
| 106 | +pybind11::module Python::import(const std::string &module_name){ |
| 107 | + return pybind11::module(PyImport_ImportModule(module_name.c_str()), false); |
| 108 | +} |
| 109 | + |
| 110 | +pybind11::module Python::reload(pybind11::module &module){ |
| 111 | + return pybind11::module(PyImport_ReloadModule(module.ptr()),false); |
| 112 | +} |
| 113 | + |
| 114 | +Python::SyntaxError::SyntaxError(pybind11::object type,pybind11::object value,pybind11::object traceback) |
| 115 | +: Error(type,value,traceback){} |
| 116 | + |
| 117 | +Python::Error::Error(pybind11::object type,pybind11::object value,pybind11::object traceback){ |
| 118 | + exp=type; |
| 119 | + val=value; |
| 120 | + trace=traceback; |
| 121 | +} |
| 122 | + |
| 123 | +void Python::Interpreter::add_path(const boost::filesystem::path &path){ |
| 124 | + if(path.empty()) |
| 125 | + return; |
| 126 | + std::wstring sys_path(Py_GetPath()); |
| 127 | + if(!sys_path.empty()) |
| 128 | +#ifdef _WIN32 |
| 129 | + sys_path += ';'; |
| 130 | +#else |
| 131 | + sys_path += ':'; |
| 132 | +#endif |
| 133 | + sys_path += path.generic_wstring(); |
| 134 | + Py_SetPath(sys_path.c_str()); |
| 135 | +} |
| 136 | + |
| 137 | +Python::Interpreter::~Interpreter(){ |
| 138 | + auto err=Error(); |
| 139 | + if(Py_IsInitialized()) |
| 140 | + Py_Finalize(); |
| 141 | + if(err) |
| 142 | + std::cerr << std::string(err) << std::endl; |
| 143 | +} |
| 144 | + |
| 145 | +pybind11::object Python::error_occured(){ |
| 146 | + return pybind11::object(PyErr_Occurred(),true); |
| 147 | +} |
| 148 | + |
| 149 | +bool Python::thrown_exception_matches(pybind11::handle exception_type){ |
| 150 | + return PyErr_ExceptionMatches(exception_type.ptr()); |
| 151 | +} |
| 152 | + |
| 153 | +bool Python::given_exception_matches(const pybind11::object &exception, pybind11::handle exception_type){ |
| 154 | + return PyErr_GivenExceptionMatches(exception.ptr(),exception_type.ptr()); |
| 155 | +} |
| 156 | + |
| 157 | +Python::Error::Error(){ |
| 158 | + if(error_occured()){ |
| 159 | + try{ |
| 160 | + PyErr_Fetch(&exp.ptr(),&val.ptr(),&trace.ptr()); |
| 161 | + PyErr_NormalizeException(&exp.ptr(),&val.ptr(),&trace.ptr()); |
| 162 | + }catch(const std::exception &e) { |
| 163 | + Terminal::get().print(e.what(),true); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +Python::Error::operator std::string(){ |
| 169 | + return std::string(exp.str())+"\n"+std::string(val.str())+"\n"; |
| 170 | +} |
| 171 | + |
| 172 | +Python::SyntaxError::SyntaxError():Error(){ |
| 173 | + if(val){ |
| 174 | + _Py_IDENTIFIER(msg); |
| 175 | + _Py_IDENTIFIER(lineno); |
| 176 | + _Py_IDENTIFIER(offset); |
| 177 | + _Py_IDENTIFIER(text); |
| 178 | + exp=std::string(pybind11::str(_PyObject_GetAttrId(val.ptr(),&PyId_msg),false)); |
| 179 | + text=std::string(pybind11::str(_PyObject_GetAttrId(val.ptr(),&PyId_text),false)); |
| 180 | + pybind11::object py_line_number(_PyObject_GetAttrId(val.ptr(),&PyId_lineno),false); |
| 181 | + pybind11::object py_line_offset(_PyObject_GetAttrId(val.ptr(),&PyId_offset),false); |
| 182 | + line_number=pybind11::cast<int>(py_line_number); |
| 183 | + line_offset=pybind11::cast<int>(py_line_offset); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +Python::SyntaxError::operator std::string(){ |
| 188 | + return exp+" ("+std::to_string(line_number)+":"+std::to_string(line_offset)+"):\n"+text; |
| 189 | +} |
| 190 | + |
| 191 | +Python::Error::operator bool(){ |
| 192 | + return exp || trace || val; |
| 193 | +} |
0 commit comments