Browse Source

BAB-22 #comment getting the module to communicate #time 8h

merge-requests/365/head
forgie 11 years ago
parent
commit
8b1bbdd1f9
  1. 20
      juci/CMakeLists.txt
  2. 62
      juci/api.cc
  3. 66
      juci/api.h
  4. 8
      juci/api_ext.cc

20
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
menu.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})

62
juci/api.cc

@ -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;
}

66
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);
}// py
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<std::string>(pySnippet);
/* add snippet to textView */
//TODO add snippet
}catch(boost::python::error_already_set const&){
PyErr_Print();
}
}
}//juci_plugin
}// py
}//juci_api
int main(int argc, char *argv[])
{
return 0;
}

8
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
Loading…
Cancel
Save