From b0b2400f5f1786af6c82abbdedc0ff4fa5d98a2e Mon Sep 17 00:00:00 2001 From: tedjk Date: Mon, 23 Feb 2015 09:14:52 +0100 Subject: [PATCH] BAB-21 #comment code-cleanup #time 15m --- juci/api.cc | 37 ++++++++++++++++++------------------- juci/api.h | 15 +++++---------- juci/api_ext.cc | 3 ++- juci/snippet.py | 4 ---- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/juci/api.cc b/juci/api.cc index a0625a1..ee38351 100644 --- a/juci/api.cc +++ b/juci/api.cc @@ -6,10 +6,11 @@ std::shared_ptr libjuci::ApiServiceProvider::notebook_; ///////////////////////////// //// API ServiceProvider //// ///////////////////////////// -libjuci::ApiServiceProvider::ApiServiceProvider( ){ +libjuci::ApiServiceProvider::ApiServiceProvider( ) { std::cout << "Apiservice std.ctor" << std::endl; } -void libjuci::ApiServiceProvider::ReplaceWord(std::string word){ + +void libjuci::ApiServiceProvider::ReplaceWord(std::string word) { Glib::RefPtr buffer = libjuci::BufferFromNotebook(); Gtk::TextIter word_start = libjuci::IterFromNotebook(); Gtk::TextIter word_end = libjuci::IterFromNotebook(); @@ -22,9 +23,13 @@ void libjuci::ApiServiceProvider::ReplaceWord(std::string word){ buffer->insert(current, word); } } -void libjuci::ApiServiceProvider::ReplaceLine(std::string line){} -std::string libjuci::ApiServiceProvider::GetWord(){ +void libjuci::ApiServiceProvider::ReplaceLine(std::string line) { + std::cout << "Unimplemented function: ApiServiceProvider::ReplaceLine(string)" + << std::endl; +} + +std::string libjuci::ApiServiceProvider::GetWord() { Glib::RefPtr buffer = libjuci::BufferFromNotebook(); Gtk::TextIter word_start = libjuci::IterFromNotebook(); Gtk::TextIter word_end = libjuci::IterFromNotebook(); @@ -34,11 +39,10 @@ std::string libjuci::ApiServiceProvider::GetWord(){ std::cout <<"start: " << word_start << std::endl << "end: " << word_end << std::endl; if(word_start < word_end) { - std::string test = buffer->get_text(word_start, word_end); - std::cout << "test: " << test << std::endl; - return test; + std::string word = buffer->get_text(word_start, word_end); + return word; } - return "no word, sry"; + return ""; } void libjuci::ApiServiceProvider::AddKeybinding() { @@ -85,19 +89,15 @@ boost::python::api::object libjuci::openPythonScript(const std::string path, void libjuci::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 + + /* for extracting return value from python: */ + //boost::python::object returnValue = boost::python::eval("function_name()", main_namespace); + //std::string return_value = boost::python::extract(pySnippet); + //do something with return_value }catch(boost::python::error_already_set const&) { PyErr_Print(); } @@ -113,6 +113,7 @@ void libjuci::IterToWordStart(Gtk::TextIter &iter) { } } } + void libjuci::IterToWordEnd(Gtk::TextIter &iter) { if(!iter.ends_line()) { while(!iter.ends_word()) { @@ -126,8 +127,6 @@ Glib::RefPtr libjuci::BufferFromNotebook() { } Gtk::TextIter libjuci::IterFromNotebook() { - // Glib::RefPtr buffer = libjuci::ApiServiceProvider::notebook_ - //->source_vec_.back()->view().get_buffer(); return libjuci::BufferFromNotebook()->get_insert()->get_iter(); } diff --git a/juci/api.h b/juci/api.h index a4b96cd..69e503c 100644 --- a/juci/api.h +++ b/juci/api.h @@ -18,20 +18,20 @@ namespace libjuci { public: static std::shared_ptr menu_; static std::shared_ptr notebook_; - static std::string text; ApiServiceProvider(); + static void AddKeybinding(); + + //for Python module: static std::string GetWord(); static void ReplaceWord(const std::string word); static void ReplaceLine(const std::string line); - - static void AddKeybinding(); }; + /////////////////////// //// Glib wrappers //// /////////////////////// - void IterToWordStart(Gtk::TextIter &iter); void IterToWordEnd(Gtk::TextIter &iter); Gtk::TextIter IterFromNotebook(); @@ -41,9 +41,7 @@ namespace libjuci { //// Api to python //// /////////////////////// void ReplaceWord(const std::string word_); - void ReplaceLine(const std::string line_); - std::string GetWord(); ////////////////////////////// @@ -51,10 +49,7 @@ namespace libjuci { ////////////////////////////// boost::python::api::object openPythonScript(const std::string path, boost::python::api::object python_name_space); - - //void snippet(std::string& word); - void LoadPlugin(const std::string& plugin_name); -}//api +}//libjuci #endif // JUCI_API_H diff --git a/juci/api_ext.cc b/juci/api_ext.cc index 8b1bb2b..2887831 100644 --- a/juci/api_ext.cc +++ b/juci/api_ext.cc @@ -6,4 +6,5 @@ BOOST_PYTHON_MODULE(juci_to_python_api) { def("replaceLine", &libjuci::ReplaceLine); def("replaceWord", &libjuci::ReplaceWord); def("getWord", &libjuci::GetWord); - }// module::juci + //something more + }// module::juci_to_python_api diff --git a/juci/snippet.py b/juci/snippet.py index 0013bd7..96c420c 100644 --- a/juci/snippet.py +++ b/juci/snippet.py @@ -2,10 +2,7 @@ #snippet plugin import juci_to_python_api -print("From Python: Snippet run") - snippets = {} -snippets["hoy"] = "hoyvalue" snippets["for"] = """\ for(#int i=0; #i<#v.size(); #i++) { std::cout << v[i] << std::endl; @@ -50,5 +47,4 @@ def getSnippet(word): theWord=juci_to_python_api.getWord() output=getSnippet(theWord) -print(theWord) juci_to_python_api.replaceWord(output)