From f2acfd51ee5c241606df4258c0946367c3f99387 Mon Sep 17 00:00:00 2001 From: forgie Date: Wed, 11 Feb 2015 10:11:08 +0100 Subject: [PATCH 1/2] yay --- juci/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index 9d13438..fc2a948 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -79,8 +79,8 @@ add_executable(${project_name} add_library(${module} SHARED - api.h - api_ext.cc + api + api_ext ) # dependencies From bc40ad6721d628ce9379c1b524aa4bfe1ce9492e Mon Sep 17 00:00:00 2001 From: forgie Date: Wed, 11 Feb 2015 13:24:05 +0100 Subject: [PATCH 2/2] BPP21 #comment got it working #time 3h --- juci/CMakeLists.txt | 9 ++---- juci/api.cc | 47 +++++++++++++++++++++++++++++++ juci/api.h | 68 +++++++++++++-------------------------------- juci/api_ext.cc | 3 +- juci/menu.cc | 6 ++++ juci/menu.h | 2 ++ 6 files changed, 77 insertions(+), 58 deletions(-) create mode 100644 juci/api.cc diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index fc2a948..4e4b80d 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -4,7 +4,6 @@ set(module juci_to_python_api) project (${project_name}) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") INCLUDE(FindPkgConfig) @@ -61,24 +60,20 @@ else() message(FATAL_ERROR "The gtkmm libraries are required. Quitting.") endif() - - - # name of the executable on Windows will be example.exe add_executable(${project_name} - #list of every needed file to create the executable + #list of every needed file to create the executable keybindings menu source window juci api + #there is no need for extentions ) - add_library(${module} SHARED - api api_ext ) diff --git a/juci/api.cc b/juci/api.cc new file mode 100644 index 0000000..42149d0 --- /dev/null +++ b/juci/api.cc @@ -0,0 +1,47 @@ +#include "api.h" +namespace juci_api{ + + void cpp::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; + + std::cout << "The string: " << g_test_string << std::endl; + } + + void cpp::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; + } + + //helpers + boost::python::api::object py::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 py::LoadPlugin(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(); + } + } +} diff --git a/juci/api.h b/juci/api.h index ef84968..9bd42c4 100644 --- a/juci/api.h +++ b/juci/api.h @@ -1,77 +1,47 @@ +#ifndef JUCI_API_H_ +#define JUCI_API_H_ + #include #include #include -// + // Plugin API // -namespace juci_api{ +const std::string g_project_root("/home/forgie/bachelor/app/juci/"); +static std::string g_test_string("test"); +namespace juci_api { // // calls from python to C++ // - namespace cpp{ + namespace cpp { - // + // Replaceword: // replaces a word in the editor with a string - // - 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; - } + + void ReplaceWord(const std::string word_); // // ReplaceLine: // Replaces a line in the editor with a string // - 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; - } + void ReplaceLine(const std::string line_); }//namespace cpp // // calls from C++ to Python // - namespace py{ - const std::string g_project_root("~/bachelor/app/juci/"); + namespace py { + + //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); - } + boost::python::api::object 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 - + void LoadPlugin(const std::string& plugin_name); + }// py }//juci_api - - - +#endif // JUCI_API_H diff --git a/juci/api_ext.cc b/juci/api_ext.cc index 922b86b..8ea43b2 100644 --- a/juci/api_ext.cc +++ b/juci/api_ext.cc @@ -1,6 +1,5 @@ #include "api.h" - -BOOST_PYTHON_MODULE(jucy_to_python_api) { +BOOST_PYTHON_MODULE(juci_to_python_api) { using namespace boost::python; // text editing def("replaceLine", &juci_api::cpp::ReplaceLine); diff --git a/juci/menu.cc b/juci/menu.cc index 9d028f2..3d01629 100644 --- a/juci/menu.cc +++ b/juci/menu.cc @@ -196,8 +196,14 @@ void Menu::Controller::OnSystemQuit() { void Menu::Controller::OnPluginAddSnippet() { std::cout << "Add snipper" << std::endl; //TODO(Forgi add you snippet magic code) + juci_api::py::LoadPlugin("snippet"); } + +// void Menu::Controller::LoadPlugin() { +// juci_api::py::LoadPlugin("plugin_name"); +// } + void Menu::Controller::OnFileOpenFile() { std::cout << "Open file clicked" << std::endl; //TODO(Oyvang) Legg til funksjon diff --git a/juci/menu.h b/juci/menu.h index caa9e98..6fce909 100644 --- a/juci/menu.h +++ b/juci/menu.h @@ -4,6 +4,8 @@ #include #include "gtkmm.h" #include "keybindings.h" +#include "api.h" + namespace Menu { class Model {