From 8b1bbdd1f9fe1822df03fd5da582d0842ae88617 Mon Sep 17 00:00:00 2001 From: forgie Date: Wed, 11 Feb 2015 09:33:16 +0100 Subject: [PATCH] BAB-22 #comment getting the module to communicate #time 8h --- juci/CMakeLists.txt | 30 +++++++++++++------ juci/api.cc | 62 --------------------------------------- juci/api.h | 70 ++++++++++++++++++++++++++++++--------------- juci/api_ext.cc | 8 ++++++ 4 files changed, 77 insertions(+), 93 deletions(-) delete mode 100644 juci/api.cc create mode 100644 juci/api_ext.cc diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index 17febd9..05ff73a 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -1,8 +1,10 @@ cmake_minimum_required (VERSION 2.8.4) set(project_name juci) +set(module juci_to_python_api) project (${project_name}) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") INCLUDE(FindPkgConfig) @@ -28,7 +30,7 @@ endif() #Boost_INCLUDE_DIRS - Boost include directories #Boost_LIBRARY_DIRS - Link directories for Boost libraries #Boost_LIBRARIES - Boost component libraries to be linked -find_package(Boost 1.5 REQUIRED) +find_package(Boost 1.5 REQUIRED COMPONENTS python) #If boost is not found if(${Boost_FOUND}) @@ -60,20 +62,32 @@ else() endif() + + # name of the executable on Windows will be example.exe add_executable(${project_name} -# list of every needed file to create the executable - menu.h + # list of every needed file to create the executable + menu.h + menu.cc - menu.cc + window.cc + juci.cc - window.cc - juci.cc - ) + api.h +) +add_library(${module} SHARED + + api.h + api_ext.cc + ) # dependencies include_directories(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS} ${GTKMM_INCLUDE_DIRS}) -link_directories(${GTKMM_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS}) +link_directories(${GTKMM_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS} ${PYTHON_INCLUDE_DIRS}) +#module: +set_target_properties(${module} PROPERTIES PREFIX "") +target_link_libraries(${module} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES}) +#executable: target_link_libraries(${project_name} ${GTKMM_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) diff --git a/juci/api.cc b/juci/api.cc deleted file mode 100644 index d413767..0000000 --- a/juci/api.cc +++ /dev/null @@ -1,62 +0,0 @@ -#include "api.h" - -// -// Plugin API -// -namespace juci_plugin{ - // - // calls from python to C++ - // - namespace cpp{ - // - // ReplaceWord: - // replaces a word in the editor with a string - // - std::string ReplaceWord(const std::string word_){ - //TODO implement api::ReplaceWord / change string to iter? - //some_namespace::controller::replaceWord(word_*); - std::cout << "unimplemented function: 'api::ReplaceWord()' called" - << std::endl; - return "string from unimplemented method 'api::ReplaceWord'"; - } - // - // ReplaceLine: - // Replaces a line in the editor with a string - // - std::string ReplaceLine(const std::string line_){ - //TODO implement api::ReplaceLine / change string to iter? - //some_namespace::controller::replaceLine(line_); - std::cout << "unimplemented function: 'api::ReplaceLine()' called" - << std::endl; - return "string from unimplemented method 'api::ReplaceLine'"; - } - // - // The module - // - namespace bp = boost::python; - BOOST_PYTHON_MODULE(juci) { - // text editing - bp::def("replaceLine", &juci_plugin::cpp::ReplaceLine) - .bp::def("replaceWord", &juci_plugin::cpp::ReplaceWord); - //.bp::def(""); - } - - }//namespace #include "api.h" - - // - // calls from C++ to Python - // - namespace py{ - - - }// py - -}//juci_plugin - - -int main(int argc, char *argv[]) -{ - - return 0; -} - diff --git a/juci/api.h b/juci/api.h index 545f9ad..ef84968 100644 --- a/juci/api.h +++ b/juci/api.h @@ -4,50 +4,74 @@ // // Plugin API // -namespace juci_plugin{ +namespace juci_api{ // // calls from python to C++ // namespace cpp{ // - // ReplaceWord: + // Replaceword: // replaces a word in the editor with a string // - std::string ReplaceWord(const std::string word_); - + void ReplaceWord(const std::string word_){ + //TODO implement api::ReplaceWord / change string to iter? + //some_namespace::controller::replaceWord(word_*); + std::cout << "unimplemented function: 'api::ReplaceWord()' called" + << std::endl; + } + // // ReplaceLine: // Replaces a line in the editor with a string // - std::string ReplaceLine(const std::string line_); - - // - // The module - // - namespace bp = boost::python; - BOOST_PYTHON_MODULE(juci) { - // text editing - bp::def("replaceLine", &juci_plugin::cpp::ReplaceLine); - bp::def("replaceWord", &juci_plugin::cpp::ReplaceWord); - }// module::juci + void ReplaceLine(const std::string line_){ + //TODO implement api::ReplaceLine / change string to iter? + //some_namespace::controller::replaceLine(line_); + std::cout << "unimplemented function: 'api::ReplaceLine()' called" + << std::endl; + } - }//namespace #include "api.h" + }//namespace cpp // // calls from C++ to Python // namespace py{ + const std::string g_project_root("~/bachelor/app/juci/"); + //helpers + boost::python::api::object openPythonScript(const std::string path, + boost::python::api::object python_name_space) { + std::string temp = g_project_root + path + ".py"; + boost::python::str the_path(temp); + return boost::python::exec_file(the_path, python_name_space);//, python_name_space); + } + + //void snippet(std::string& word); + void plugin(const std::string& plugin_name){ + try{ + /* initialize python interpreter */ + Py_Initialize(); + boost::python::api::object main_module = boost::python::import("__main__"); + boost::python::api::object main_namespace = main_module.attr("__dict__"); + + /* runs script from python */ + //boost::python::object ignored1 = setPythonVar("word", word, main_namespace); + boost::python::api::object ignored2 = openPythonScript(plugin_name, main_namespace); + /* extracts desired values */ + //boost::python::object pySnippet = boost::python::eval("getSnippet()", main_namespace); + //word = boost::python::extract(pySnippet); + /* add snippet to textView */ + //TODO add snippet + }catch(boost::python::error_already_set const&){ + PyErr_Print(); + } + } }// py -}//juci_plugin +}//juci_api -int main(int argc, char *argv[]) -{ - - return 0; -} - + diff --git a/juci/api_ext.cc b/juci/api_ext.cc new file mode 100644 index 0000000..922b86b --- /dev/null +++ b/juci/api_ext.cc @@ -0,0 +1,8 @@ +#include "api.h" + +BOOST_PYTHON_MODULE(jucy_to_python_api) { + using namespace boost::python; + // text editing + def("replaceLine", &juci_api::cpp::ReplaceLine); + def("replaceWord", &juci_api::cpp::ReplaceWord); + }// module::juci