From 909bf640c66ef8f5d7dc1307c1191ffee64677cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Wed, 4 Nov 2015 18:10:06 +0100 Subject: [PATCH 01/82] #69 Remove old implementation --- src/api.cc | 213 ------------------------------------------------- src/api.h | 72 ----------------- src/api_ext.cc | 15 ---- 3 files changed, 300 deletions(-) delete mode 100644 src/api.cc delete mode 100644 src/api.h delete mode 100644 src/api_ext.cc diff --git a/src/api.cc b/src/api.cc deleted file mode 100644 index be1381db..00000000 --- a/src/api.cc +++ /dev/null @@ -1,213 +0,0 @@ -#include "api.h" -#include "logging.h" -#include "singletons.h" - -Menu* PluginApi::menu=nullptr; -Notebook* PluginApi::notebook=nullptr; -///////////////////////////// -//// API ServiceProvider //// -///////////////////////////// -PluginApi::PluginApi(Notebook* notebook, Menu* menu) { - DEBUG("Adding pointers for the API"); - this->notebook = notebook; - this->menu = menu; - DEBUG("Initiating plugins(from plugins.py).."); -#ifndef __APPLE__ - //InitPlugins(); //TODO: fix this -#endif - DEBUG("Plugins initiated.."); -} - -void PluginApi::ReplaceWord(std::string word) { - Glib::RefPtr buffer = libjuci::BufferFromNotebook(); - Gtk::TextIter word_start = libjuci::IterFromNotebook(); - Gtk::TextIter word_end = libjuci::IterFromNotebook(); - libjuci::IterToWordStart(word_start); - libjuci::IterToWordEnd(word_end); - if (word_start != word_end) { - buffer->erase(word_start, word_end); - Gtk::TextIter current = libjuci::IterFromNotebook(); - buffer->insert(current, word); - } -} - -void PluginApi::ReplaceLine(std::string line) { - WARNING("use of unimplemented method"); -} - -std::string PluginApi::GetWord() { - Glib::RefPtr buffer = libjuci::BufferFromNotebook(); - Gtk::TextIter word_start = libjuci::IterFromNotebook(); - Gtk::TextIter word_end = libjuci::IterFromNotebook(); - - libjuci::IterToWordStart(word_start); - libjuci::IterToWordEnd(word_end); - - if (word_start < word_end) { - std::string word = buffer->get_text(word_start, word_end); - return word; - } - return ""; -} - -void PluginApi::InitPlugins() { - libjuci::LoadPlugin(Singleton::config_dir() + "plugins.py"); -} - -void PluginApi::AddMenuElement(std::string plugin_name) { - DEBUG("Adding menu element for "+plugin_name); - AddMenuXml(plugin_name, "PluginMenu"); - std::string plugin_action_name = plugin_name+"Menu"; - menu->action_group->add(Gtk::Action::create(plugin_action_name, plugin_name)); -} - -void PluginApi::AddSubMenuElement(std::string parent_menu, - std::string menu_name, - std::string menu_func_name, - std::string plugin_path, - std::string menu_keybinding) { - AddSubMenuXml(menu_func_name, parent_menu); - menu->action_group->add(Gtk::Action::create(menu_func_name, - menu_name), - Gtk::AccelKey(menu_keybinding), - [=]() { - libjuci::LoadPluginFunction(menu_func_name, plugin_path); - }); -} - -void PluginApi::AddMenuXml(std::string plugin_name, std::string parent_menu) { - std::string temp_menu = menu->ui; - std::size_t plugin_menu_pos = temp_menu.find(parent_menu); - // +2 gets you outside of the tag:<'menu_name'> - plugin_menu_pos+=parent_menu.size() +2; - std::string menu_prefix = temp_menu.substr(0, plugin_menu_pos); - std::string menu_suffix = temp_menu.substr(plugin_menu_pos); - std::string menu_input = - " " - " "; - - menu->ui = menu_prefix + menu_input + menu_suffix; -} - -void PluginApi::AddSubMenuXml(std::string plugin_name, - std::string parent_menu) { - std::string temp_menu = menu->ui; - - std::size_t parent_menu_pos = temp_menu.find(parent_menu); - // +2 gets you outside of the tag:<'menu_name'> - parent_menu_pos+=parent_menu.size() +2; - std::string menu_prefix = temp_menu.substr(0, parent_menu_pos); - std::string menu_suffix = temp_menu.substr(parent_menu_pos); - - std::string menu_input =""; - menu->ui = menu_prefix + menu_input + menu_suffix; -} - -/////////////////////// -//// Api to python //// -/////////////////////// -void libjuci::ReplaceWord(const std::string word) { - PluginApi::ReplaceWord(word); -} - -void libjuci::ReplaceLine(const std::string line) { - std::cout << "unimplemented: " << __func__ << " called" - << std::endl; -} -std::string libjuci::GetWord() { - return PluginApi::GetWord(); -} - -void libjuci::AddMenuElement(std::string plugin_name) { - PluginApi::AddMenuElement(plugin_name); -} - -void libjuci::AddSubMenuElement(std::string parent_menu, - std::string menu_name, - std::string menu_func_name, - std::string plugin_path, - std::string menu_keybinding) { - PluginApi::AddSubMenuElement(parent_menu, - menu_name, - menu_func_name, - plugin_path, - menu_keybinding); -} - -////////////////////////////// -//// Boost.Python methods //// -////////////////////////////// -namespace bp = boost::python; - -bp::api::object libjuci::OpenPythonScript(const std::string path, - bp::api::object python_name_space) { - bp::str str_path(path); - return bp::exec_file(str_path, python_name_space); -} - -void libjuci::LoadPlugin(const std::string& plugin_name) { - try { - Py_Initialize(); - bp::api::object main_module = bp::import("__main__"); - bp::api::object main_namespace = - main_module.attr("__dict__"); - bp::api::object ignored2 = - OpenPythonScript(plugin_name, main_namespace); - }catch (bp::error_already_set const&) { - PyErr_Print(); - } -} - -void libjuci::LoadPluginFunction(const std::string &function_name, - const std::string &plugin_path) { - try { - Py_Initialize(); - bp::api::object main_module = bp::import("__main__"); - bp::api::object main_namespace = main_module.attr("__dict__"); - bp::api::object ignored2 = OpenPythonScript(plugin_path, main_namespace); - bp::str func_name(function_name); - bp::api::object returnValue = bp::eval(func_name, main_namespace); - }catch (bp::error_already_set const&) { - PyErr_Print(); - } -} - -void libjuci::InitPlugin(const std::string& plugin_path) { - try { - Py_Initialize(); - bp::api::object main_module = bp::import("__main__"); - bp::api::object main_namespace = main_module.attr("__dict__"); - bp::api::object ignored2 = OpenPythonScript(plugin_path, main_namespace); - bp::object returnValue = bp::eval("initPlugin()", main_namespace); - // do something with return_value - }catch (bp::error_already_set const&) { - PyErr_Print(); - } -} - -/////////////////////// -//// Glib wrappers //// -/////////////////////// -void libjuci::IterToWordStart(Gtk::TextIter &iter) { - if (!iter.starts_line()) { - while (!iter.starts_word()) { - iter.backward_char(); - } - } -} - -void libjuci::IterToWordEnd(Gtk::TextIter &iter) { - if (!iter.ends_line()) { - while (!iter.ends_word()) { - iter.forward_char(); - } - } -} - -Glib::RefPtr libjuci::BufferFromNotebook() { - return Glib::RefPtr(PluginApi::notebook->get_current_view()->get_buffer()); -} - -Gtk::TextIter libjuci::IterFromNotebook() { - return libjuci::BufferFromNotebook()->get_insert()->get_iter(); -} diff --git a/src/api.h b/src/api.h deleted file mode 100644 index a3aafcaf..00000000 --- a/src/api.h +++ /dev/null @@ -1,72 +0,0 @@ -#ifndef JUCI_API_H_ -#define JUCI_API_H_ - -#include -#include -#include -#include "notebook.h" -#include "menu.h" - -//////////////////// -//// Plugin Api //// -//////////////////// -class PluginApi { -public: - PluginApi(Notebook* notebook, Menu* menu); - static Menu* menu; - static Notebook* notebook; - static void InitPlugins(); - // for Python module: - static std::string GetWord(); - // menu management - static void AddMenuElement(const std::string plugin_name); - static void AddSubMenuElement(const std::string parent_menu, - const std::string menu_name, - const std::string menu_func_name, - const std::string plugin_path, - const std::string menu_keybinding); - static void ReplaceWord(const std::string word); - static void ReplaceLine(const std::string line); -protected: - static void AddMenuXml(std::string plugin_name, std::string parent_menu); - static void AddSubMenuXml(std::string plugin_name, std::string parent_menu); -}; // PluginApi -namespace libjuci { - /////////////////////// - //// Glib wrappers //// - /////////////////////// - void IterToWordStart(Gtk::TextIter &iter); - void IterToWordEnd(Gtk::TextIter &iter); - Gtk::TextIter IterFromNotebook(); - Glib::RefPtr BufferFromNotebook(); - /////////////////////// - //// Api to python //// - /////////////////////// - void ReplaceWord(const std::string word); - void ReplaceLine(const std::string line); - std::string GetWord(); - - void AddMenuElement(const std::string plugin_name); - - void AddSubMenuElement(const std::string parent_menu, - const std::string menu_name, - const std::string menu_func_name, - const std::string plugin_path, - const std::string menu_keybinding); - void AddMenuXml(const std::string plugin_name, - const std::string parent_menu); - void AddSubMenuXml(const std::string plugin_name, - const std::string parent_menu); - ////////////////////////////// - //// Boost.Python methods //// - ////////////////////////////// - namespace bp = boost::python; - bp::api::object OpenPythonScript(const std::string path, - bp::api::object python_name_space); - void LoadPlugin(const std::string& plugin_name); - void LoadPluginFunction(const std::string &function_name, - const std::string &plugin_path); - - void InitPlugin(const std::string& plugin_path); -} // namespace libjuci -#endif // JUCI_API_H_ diff --git a/src/api_ext.cc b/src/api_ext.cc deleted file mode 100644 index 9b8b1ba2..00000000 --- a/src/api_ext.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include "api.h" - -BOOST_PYTHON_MODULE(juci_to_python_api) { - using namespace boost::python; - // plugin inclusion - def("addMenuElement", &libjuci::AddMenuElement); - def("addSubMenuElement", &libjuci::AddSubMenuElement); - def("loadPlugin", &libjuci::LoadPlugin); - def("initPlugin", &libjuci::InitPlugin); - - // text editing - def("replaceLine", &libjuci::ReplaceLine); - def("replaceWord", &libjuci::ReplaceWord); - def("getWord", &libjuci::GetWord); - } // module::juci_to_python_api From 8c3d52dcaa5b4341dcb6dc09104745658f9096e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Thu, 5 Nov 2015 05:41:02 +0100 Subject: [PATCH 02/82] Cleanup of cmakelists and boost python bindings --- .gitmodules | 3 + CMakeLists.txt | 8 +-- src/CMakeLists.txt | 149 ++++++++++++++++++++------------------------- src/api.cc | 11 ++++ src/api.h | 18 ++++++ src/python_api.cc | 9 +++ src/singletons.h | 5 +- src/terminal.h | 2 +- src/test.py | 7 +++ src/window.cc | 4 +- src/window.h | 4 +- 11 files changed, 127 insertions(+), 93 deletions(-) create mode 100644 src/api.cc create mode 100644 src/api.h create mode 100644 src/python_api.cc create mode 100644 src/test.py diff --git a/.gitmodules b/.gitmodules index 2428fa65..b9228cc0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,3 +2,6 @@ path = libclangmm url = https://github.com/cppit/libclangmm.git branch = v0.9.3 +[submodule "pybind11"] + path = pybind11 + url = git@github.com:wjakob/pybind11 diff --git a/CMakeLists.txt b/CMakeLists.txt index f80481f9..ee45b6fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,8 @@ cmake_minimum_required (VERSION 2.8.4) -set(project_name juci) -#set(module juci_to_python_api) +set(application_name "juci") +set(python_module "jucipy") -#set(lib_install_path "/usr/local/lib/python2.7/dist-packages/") - -project (${project_name}) +project (${application_name}) add_subdirectory("src") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e5e7cdb9..b89db8e8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,53 +25,71 @@ if(MSYS) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DJUCI_CMAKE_INSTALL_PREFIX=\\\"${CMAKE_INSTALL_PREFIX}\\\"") endif() -INCLUDE(FindPkgConfig) - find_package(LibClang REQUIRED) +find_package(Boost 1.55 COMPONENTS thread log python system filesystem REQUIRED) +find_package(ASPELL REQUIRED) +include(FindPythonLibs) +set(PYBIND11_INCLUDE_DIR "../pybind11/include") +set(LIBCLANGMM_INCLUDE_DIR ../libclangmm/src) -#find_package(PythonLibs 2.7) - -#find_package(Boost 1.55 COMPONENTS python thread log system filesystem REQUIRED) -find_package(Boost 1.55 COMPONENTS thread log system filesystem REQUIRED) - -pkg_check_modules(GTKMM gtkmm-3.0 REQUIRED) # The name GTKMM is set here for the variables abouve - +include(FindPkgConfig) +pkg_check_modules(GTKMM gtkmm-3.0 REQUIRED) pkg_check_modules(GTKSVMM gtksourceviewmm-3.0 REQUIRED) -find_package(ASPELL REQUIRED) +set(global_includes + ${Boost_INCLUDE_DIRS} + ${GTKMM_INCLUDE_DIRS} + ${GTKSVMM_INCLUDE_DIRS} + ${LIBCLANG_INCLUDE_DIRS} + ${LIBCLANGMM_INCLUDE_DIR} + ${ASPELL_INCLUDE_DIR} + ${PYTHON_INCLUDE_DIRS} +) -set(source_files juci.h - juci.cc - menu.h - menu.cc - source.h - source.cc - source_clang.h - source_clang.cc - selectiondialog.h - selectiondialog.cc - config.h +set(global_libraries + ${LIBCLANG_LIBRARIES} + ${GTKMM_LIBRARIES} + ${GTKSVMM_LIBRARIES} + ${Boost_LIBRARIES} + ${ASPELL_LIBRARIES} + ${PYTHON_LIBRARIES} +) + +set(application_files + api.cc + api.h + python_api.cc + cmake.cc + cmake.h config.cc - filesystem.h - filesystem.cc - window.cc - window.h + config.h dialogs.h -# api.h -# api.cc + directories.cc + directories.h + entrybox.cc + entrybox.h + filesystem.cc + filesystem.h + juci.cc + juci.h + logging.h + menu.cc + menu.h notebook.cc notebook.h - entrybox.h - entrybox.cc - directories.h - directories.cc + selectiondialog.cc + selectiondialog.h + singletons.cc + singletons.h + source.cc + source.h + source_clang.cc + source_clang.h terminal.h - tooltips.h tooltips.cc - singletons.h - singletons.cc - cmake.h - cmake.cc + tooltips.h + window.cc + window.h ../libclangmm/src/CodeCompleteResults.cc ../libclangmm/src/CompilationDatabase.cc @@ -79,65 +97,32 @@ set(source_files juci.h ../libclangmm/src/CompileCommands.cc ../libclangmm/src/CompletionString.cc ../libclangmm/src/Cursor.cc + ../libclangmm/src/Diagnostic.cc ../libclangmm/src/Index.cc ../libclangmm/src/SourceLocation.cc ../libclangmm/src/SourceRange.cc ../libclangmm/src/Token.cc ../libclangmm/src/Tokens.cc ../libclangmm/src/TranslationUnit.cc - ../libclangmm/src/Diagnostic.cc - ../libclangmm/src/Utility.cc) + ../libclangmm/src/Utility.cc +) if(MSYS) - list(APPEND source_files terminal_win.cc) - list(APPEND source_files dialogs_win.cc) -else() - list(APPEND source_files terminal.cc) - list(APPEND source_files dialogs.cc) -endif() + list(APPEND application_files terminal_win.cc dialogs_win.cc) +else(MSYS) + list(APPEND application_files terminal.cc dialogs.cc) +endif(MSYS) -add_executable(${project_name} ${source_files}) +add_executable(${application_name} ${application_files}) +add_library(${python_module} SHARED ${application_files}) -# add_library(${module} SHARED -# api -# api_ext) +include_directories(${global_includes}) +target_link_libraries(${application_name} ${global_libraries} ${python_module}) +target_link_libraries(${python_module} ${global_libraries}) +set_target_properties(${python_module} PROPERTIES PREFIX "") -include_directories( - ${Boost_INCLUDE_DIRS} -# ${PYTHON_INCLUDE_DIRS} - ${GTKMM_INCLUDE_DIRS} - ${GTKSVMM_INCLUDE_DIRS} - ${LIBCLANG_INCLUDE_DIRS} - ${ASPELL_INCLUDE_DIR} - ../libclangmm/src -) -link_directories( - ${GTKMM_LIBRARY_DIRS} - ${GTKSVMM_LIBRARY_DIRS} - ${Boost_LIBRARY_DIRS} -# ${PYTHON_INCLUDE_DIRS} - ${LIBCLANG_LIBRARY_DIRS} -) -# set_target_properties(${module} -# PROPERTIES PREFIX "" -# LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib/") -# target_link_libraries(${module} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES}) -# target_link_libraries(${module} ${Boost_LIBRARIES}) -target_link_libraries(${project_name} - ${LIBCLANG_LIBRARIES} - ${GTKMM_LIBRARIES} - ${GTKSVMM_LIBRARIES} - ${Boost_LIBRARIES} - ${ASPELL_LIBRARIES} - #${PYTHON_LIBRARIES} -) -# install(TARGETS ${project_name} ${module} -install(TARGETS ${project_name} - RUNTIME DESTINATION bin -# LIBRARY DESTINATION ${lib_install_path} -) diff --git a/src/api.cc b/src/api.cc new file mode 100644 index 00000000..5239aad6 --- /dev/null +++ b/src/api.cc @@ -0,0 +1,11 @@ +#include "api.h" + +Terminal *Saus::api_terminal = nullptr; + +Saus::Saus(Terminal *ptr) { + api_terminal = ptr; +} + +void terminal::println(const std::string &message) { + Saus::api_terminal->print(message); +} \ No newline at end of file diff --git a/src/api.h b/src/api.h new file mode 100644 index 00000000..e2301759 --- /dev/null +++ b/src/api.h @@ -0,0 +1,18 @@ +#ifndef JUCI_API_H_ +#define JUCI_API_H_ + +#include +#include "terminal.h" + +class Saus { +public: + Saus(Terminal *t); + static Terminal *api_terminal; +}; + +class terminal { +public: + void println(const std::string &message); +}; + +#endif \ No newline at end of file diff --git a/src/python_api.cc b/src/python_api.cc new file mode 100644 index 00000000..0a2ccc1c --- /dev/null +++ b/src/python_api.cc @@ -0,0 +1,9 @@ +#include "api.h" +#include +namespace py = boost::python; + +BOOST_PYTHON_MODULE(jucipy) { + py::class_("terminal", py::init<>()) + .def("println",&terminal::println) + ; +}; \ No newline at end of file diff --git a/src/singletons.h b/src/singletons.h index 4ec1a36a..77438f8a 100644 --- a/src/singletons.h +++ b/src/singletons.h @@ -6,16 +6,17 @@ #include "terminal.h" #include "menu.h" #include "config.h" +#include "api.h" class Singleton { -public: +public: static std::unique_ptr config; static std::unique_ptr menu; static std::unique_ptr terminal; static std::unique_ptr directories; static std::unique_ptr info; static std::unique_ptr status; - + static void init(); }; diff --git a/src/terminal.h b/src/terminal.h index edbba026..02f72c48 100644 --- a/src/terminal.h +++ b/src/terminal.h @@ -18,6 +18,7 @@ class Terminal : public Gtk::TextView { ~InProgress(); void done(const std::string& msg); void cancel(const std::string& msg); + private: void start(const std::string& msg); size_t line_nr; @@ -25,7 +26,6 @@ class Terminal : public Gtk::TextView { Glib::Dispatcher waiting_print; std::thread wait_thread; }; - Terminal(); int execute(const std::string &command, const boost::filesystem::path &path="", bool use_pipes=true); int execute(std::istream &stdin_stream, std::ostream &stdout_stream, const std::string &command, const boost::filesystem::path &path=""); diff --git a/src/test.py b/src/test.py new file mode 100644 index 00000000..73572c26 --- /dev/null +++ b/src/test.py @@ -0,0 +1,7 @@ +import sys +sys.path.append('.') +from jucipy import terminal + +t = terminal() + +t.println('Dette er test') diff --git a/src/window.cc b/src/window.cc index 388c94dd..df1d951c 100644 --- a/src/window.cc +++ b/src/window.cc @@ -1,7 +1,6 @@ #include "window.h" #include "logging.h" #include "singletons.h" -//#include "api.h" #include "dialogs.h" #include "filesystem.h" @@ -22,7 +21,8 @@ namespace sigc { #endif } -Window::Window() : compiling(false) { +Window::Window() : compiling(false), + saus(Singleton::terminal.get()) { JDEBUG("start"); set_title("juCi++"); set_events(Gdk::POINTER_MOTION_MASK|Gdk::FOCUS_CHANGE_MASK|Gdk::SCROLL_MASK); diff --git a/src/window.h b/src/window.h index 957b44d3..ef7ccd2d 100644 --- a/src/window.h +++ b/src/window.h @@ -1,15 +1,17 @@ #ifndef JUCI_WINDOW_H_ #define JUCI_WINDOW_H_ -#include "gtkmm.h" +#include #include "entrybox.h" #include "notebook.h" #include +#include "api.h" class Window : public Gtk::ApplicationWindow { public: Window(); Notebook notebook; + Saus saus; protected: bool on_key_press_event(GdkEventKey *event); From 3ea6255f1af3de78dc05b52b8dc75a4eb98c93fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Wed, 11 Nov 2015 11:09:41 +0100 Subject: [PATCH 03/82] Working boost/pybind impl --- src/CMakeLists.txt | 1 + src/api.cc | 49 +++++++++++++++++++++++++++++++++++++++------- src/api.h | 23 +++++++++++++++------- src/juci.cc | 2 +- src/juci.h | 2 ++ src/python_api.cc | 16 ++++++++------- src/window.cc | 3 +-- src/window.h | 2 -- 8 files changed, 72 insertions(+), 26 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b89db8e8..451d79ac 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -44,6 +44,7 @@ set(global_includes ${LIBCLANGMM_INCLUDE_DIR} ${ASPELL_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS} + ${PYBIND11_INCLUDE_DIR} ) set(global_libraries diff --git a/src/api.cc b/src/api.cc index 5239aad6..197fb899 100644 --- a/src/api.cc +++ b/src/api.cc @@ -1,11 +1,46 @@ #include "api.h" +#include "singletons.h" -Terminal *Saus::api_terminal = nullptr; - -Saus::Saus(Terminal *ptr) { - api_terminal = ptr; +void PythonApi::directories_open(const std::string &dir) { + Singleton::directories->open(dir); } -void terminal::println(const std::string &message) { - Saus::api_terminal->print(message); -} \ No newline at end of file +PythonInterpreter::PythonInterpreter() { + try { + if (!Py_IsInitialized()) { + Py_Initialize(); + auto main = boost::python::import("__main__"); + name = main.attr("__dict__"); + } + } catch (boost::python::error_already_set &error) { + PyErr_Print(); + } +} +PythonInterpreter::~PythonInterpreter() { + if(Py_IsInitialized()){ + Py_Finalize(); + } +} +void PythonInterpreter::python_exec(const std::string &command) { + try { + boost::python::exec(command.c_str(), name); + } catch (boost::python::error_already_set &error) { + PyErr_Print(); + } +} +void PythonInterpreter::init() { + auto plugin_path = Singleton::config->juci_home_path() / "plugins"; + python_exec("import sys"); + python_exec("sys.path.append('" + plugin_path.string() + "')"); + python_exec("sys.path.append('/home/zalox/projects/juci/src')"); + python_exec("import jucipy"); + if (boost::filesystem::exists(plugin_path) && boost::filesystem::is_directory(plugin_path)) { + for (boost::filesystem::directory_iterator it(plugin_path); it != boost::filesystem::directory_iterator(); it++) { + auto import = it->path().filename(); + while (!import.extension().empty()) { // make sure extensions are stripped on files + import = import.stem(); + } + python_exec("import " + import.string()); + } + } +} diff --git a/src/api.h b/src/api.h index e2301759..90823044 100644 --- a/src/api.h +++ b/src/api.h @@ -1,18 +1,27 @@ #ifndef JUCI_API_H_ #define JUCI_API_H_ +#include #include -#include "terminal.h" +#include +#include +#include +#include "singletons.h" -class Saus { +class PythonApi { public: - Saus(Terminal *t); - static Terminal *api_terminal; + static void directories_open(const std::string &dir); }; -class terminal { +class PythonInterpreter { public: - void println(const std::string &message); + PythonInterpreter(); + ~PythonInterpreter(); + void init(); +private: + boost::python::object name; + void python_exec(const std::string &command); }; -#endif \ No newline at end of file + +#endif // JUCI_API_H_ \ No newline at end of file diff --git a/src/juci.cc b/src/juci.cc index 5dd05092..dac9eeef 100644 --- a/src/juci.cc +++ b/src/juci.cc @@ -89,7 +89,7 @@ void Application::on_startup() { Application::Application() : Gtk::Application("no.sout.juci", Gio::APPLICATION_NON_UNIQUE | Gio::APPLICATION_HANDLES_COMMAND_LINE) { Singleton::init(); init_logging(); - + pm.init(); Glib::set_application_name("juCi++"); window=std::unique_ptr(new Window()); diff --git a/src/juci.h b/src/juci.h index b34df62d..8e596707 100644 --- a/src/juci.h +++ b/src/juci.h @@ -4,6 +4,7 @@ #include #include "logging.h" #include "window.h" +#include "api.h" class Application : public Gtk::Application { public: @@ -16,6 +17,7 @@ class Application : public Gtk::Application { std::vector directories; std::vector files; void init_logging(); + PythonInterpreter pm; }; #endif // JUCI_JUCI_H_ diff --git a/src/python_api.cc b/src/python_api.cc index 0a2ccc1c..33dc66c2 100644 --- a/src/python_api.cc +++ b/src/python_api.cc @@ -1,9 +1,11 @@ #include "api.h" -#include -namespace py = boost::python; +#include -BOOST_PYTHON_MODULE(jucipy) { - py::class_("terminal", py::init<>()) - .def("println",&terminal::println) - ; -}; \ No newline at end of file +PYBIND11_PLUGIN(jucipy) { + pybind11::module m("jucipy", "Python API for juCi++"); + pybind11::class_(m, "API") + .def(pybind11::init<>()) + .def("directories_open", &PythonApi::directories_open, pybind11::arg("dir")) + ; + return m.ptr(); +} \ No newline at end of file diff --git a/src/window.cc b/src/window.cc index df1d951c..62eea092 100644 --- a/src/window.cc +++ b/src/window.cc @@ -21,8 +21,7 @@ namespace sigc { #endif } -Window::Window() : compiling(false), - saus(Singleton::terminal.get()) { +Window::Window() : compiling(false) { JDEBUG("start"); set_title("juCi++"); set_events(Gdk::POINTER_MOTION_MASK|Gdk::FOCUS_CHANGE_MASK|Gdk::SCROLL_MASK); diff --git a/src/window.h b/src/window.h index ef7ccd2d..0a032ad7 100644 --- a/src/window.h +++ b/src/window.h @@ -5,13 +5,11 @@ #include "entrybox.h" #include "notebook.h" #include -#include "api.h" class Window : public Gtk::ApplicationWindow { public: Window(); Notebook notebook; - Saus saus; protected: bool on_key_press_event(GdkEventKey *event); From 86e5a9c74dfa59563a4eea081aca89dc17ab727c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Fri, 13 Nov 2015 12:05:09 +0100 Subject: [PATCH 04/82] Working python impreter and importing of plugins, just api defenitions left --- CMakeLists.txt | 2 +- src/CMakeLists.txt | 16 ++++++++-------- src/api.cc | 8 ++++---- src/api.h | 2 +- src/juci.cc | 6 +----- src/main.cc | 5 +++++ src/python_api.cc | 8 ++++---- 7 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 src/main.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index ee45b6fc..0cf13ce9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required (VERSION 2.8.4) set(application_name "juci") -set(python_module "jucipy") +set(library_name "libjuci") project (${application_name}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 451d79ac..ee3de1e9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -56,7 +56,7 @@ set(global_libraries ${PYTHON_LIBRARIES} ) -set(application_files +set(library_files api.cc api.h python_api.cc @@ -109,18 +109,18 @@ set(application_files ) if(MSYS) - list(APPEND application_files terminal_win.cc dialogs_win.cc) + list(APPEND library_files terminal_win.cc dialogs_win.cc) else(MSYS) - list(APPEND application_files terminal.cc dialogs.cc) + list(APPEND library_files terminal.cc dialogs.cc) endif(MSYS) -add_executable(${application_name} ${application_files}) -add_library(${python_module} SHARED ${application_files}) +add_executable(${application_name} main.cc) +add_library(${library_name} SHARED ${library_files}) include_directories(${global_includes}) -target_link_libraries(${application_name} ${global_libraries} ${python_module}) -target_link_libraries(${python_module} ${global_libraries}) -set_target_properties(${python_module} PROPERTIES PREFIX "") +target_link_libraries(${application_name} ${library_name}) +target_link_libraries(${library_name} ${global_libraries}) +set_target_properties(${library_name} PROPERTIES PREFIX "") diff --git a/src/api.cc b/src/api.cc index 197fb899..9b183369 100644 --- a/src/api.cc +++ b/src/api.cc @@ -1,8 +1,8 @@ #include "api.h" #include "singletons.h" -void PythonApi::directories_open(const std::string &dir) { - Singleton::directories->open(dir); +void PythonApi::directories_open() { + Singleton::directories->open("/home/"); } PythonInterpreter::PythonInterpreter() { @@ -33,14 +33,14 @@ void PythonInterpreter::init() { python_exec("import sys"); python_exec("sys.path.append('" + plugin_path.string() + "')"); python_exec("sys.path.append('/home/zalox/projects/juci/src')"); - python_exec("import jucipy"); + python_exec("import libjuci"); if (boost::filesystem::exists(plugin_path) && boost::filesystem::is_directory(plugin_path)) { for (boost::filesystem::directory_iterator it(plugin_path); it != boost::filesystem::directory_iterator(); it++) { auto import = it->path().filename(); while (!import.extension().empty()) { // make sure extensions are stripped on files import = import.stem(); } - python_exec("import " + import.string()); + python_exec("import " + import.string()); // this imports and inits all packages and scrips in juci_home/plugins } } } diff --git a/src/api.h b/src/api.h index 90823044..0e46c50b 100644 --- a/src/api.h +++ b/src/api.h @@ -10,7 +10,7 @@ class PythonApi { public: - static void directories_open(const std::string &dir); + static void directories_open(); }; class PythonInterpreter { diff --git a/src/juci.cc b/src/juci.cc index dac9eeef..e15e255b 100644 --- a/src/juci.cc +++ b/src/juci.cc @@ -72,6 +72,7 @@ void Application::on_startup() { Singleton::menu->init(); Singleton::menu->build(); + pm.init(); auto object = Singleton::menu->builder->get_object("juci-menu"); auto juci_menu = Glib::RefPtr::cast_dynamic(object); @@ -89,12 +90,7 @@ void Application::on_startup() { Application::Application() : Gtk::Application("no.sout.juci", Gio::APPLICATION_NON_UNIQUE | Gio::APPLICATION_HANDLES_COMMAND_LINE) { Singleton::init(); init_logging(); - pm.init(); Glib::set_application_name("juCi++"); window=std::unique_ptr(new Window()); } - -int main(int argc, char *argv[]) { - return Application().run(argc, argv); -} diff --git a/src/main.cc b/src/main.cc new file mode 100644 index 00000000..6d2b79b3 --- /dev/null +++ b/src/main.cc @@ -0,0 +1,5 @@ +#include "juci.h" + +int main(int argc, char *argv[]) { + return Application().run(argc, argv); +} \ No newline at end of file diff --git a/src/python_api.cc b/src/python_api.cc index 33dc66c2..4ffc697a 100644 --- a/src/python_api.cc +++ b/src/python_api.cc @@ -1,11 +1,11 @@ #include "api.h" #include -PYBIND11_PLUGIN(jucipy) { - pybind11::module m("jucipy", "Python API for juCi++"); +PYBIND11_PLUGIN(libjuci) { + pybind11::module m("libjuci", "Python API for juCi++"); pybind11::class_(m, "API") - .def(pybind11::init<>()) - .def("directories_open", &PythonApi::directories_open, pybind11::arg("dir")) + .def(pybind11::init()) + .def("directories_open", &PythonApi::directories_open) ; return m.ptr(); } \ No newline at end of file From 7d2f4a9bc64d18e8dde132e446d7c8750487242d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Fri, 27 Nov 2015 12:31:05 +0100 Subject: [PATCH 05/82] #69 Testing different approches --- .gitmodules | 2 +- docs/install.md | 6 ++--- pybind11 | 1 + src/CMakeLists.txt | 11 +++----- src/api.cc | 66 ++++++++++++++++++++++++++-------------------- src/api.h | 22 +++++++--------- src/juci.cc | 2 -- src/juci.h | 2 -- src/python_api.cc | 10 ++++--- src/singletons.cc | 2 ++ src/singletons.h | 1 + src/test.py | 9 ++----- src/window.cc | 9 ++++--- 13 files changed, 71 insertions(+), 72 deletions(-) create mode 160000 pybind11 diff --git a/.gitmodules b/.gitmodules index b9228cc0..e30b4f91 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,4 +4,4 @@ branch = v0.9.3 [submodule "pybind11"] path = pybind11 - url = git@github.com:wjakob/pybind11 + url = https://github.com/wjakob/pybind11 diff --git a/docs/install.md b/docs/install.md index 9693755e..a1745823 100644 --- a/docs/install.md +++ b/docs/install.md @@ -3,7 +3,7 @@ ## Debian/Ubuntu 15 Install dependencies: ```sh -sudo apt-get install git cmake make g++ libclang-dev pkg-config libboost-system-dev libboost-thread-dev libboost-filesystem-dev libboost-log-dev libgtkmm-3.0-dev libgtksourceviewmm-3.0-dev aspell-en libaspell-dev +sudo apt-get install git cmake make g++ libclang-dev pkg-config libboost-system-dev libboost-thread-dev libboost-filesystem-dev libboost-log-dev libboost-python-dev libgtkmm-3.0-dev libgtksourceviewmm-3.0-dev aspell-en libaspell-dev ``` Get juCi++ source, compile and install: @@ -28,7 +28,7 @@ sudo add-apt-repository ppa:ubuntu-toolchain-r/test sudo apt-get update sudo apt-get install g++-4.9 sudo apt-get remove g++-4.8 -sudo apt-get install git cmake make g++ libclang-3.6-dev pkg-config libboost-system1.55-dev libboost-thread1.55-dev libboost-filesystem1.55-dev libboost-log1.55-dev libgtkmm-3.0-dev libgtksourceviewmm-3.0-dev aspell-en libaspell-dev +sudo apt-get install git cmake make g++ libclang-3.6-dev pkg-config libboost-system1.55-dev libboost-thread1.55-dev libboost-filesystem1.55-dev libboost-log1.55-dev libboost-python1.55-dev libgtkmm-3.0-dev libgtksourceviewmm-3.0-dev aspell-en libaspell-dev ``` Get juCi++ source, compile and install: @@ -75,7 +75,7 @@ cmake -G"MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=/mingw64 . make make install ``` - +