19 changed files with 250 additions and 341 deletions
@ -1,47 +1,134 @@
|
||||
#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::shared_ptr<Menu::Controller> libjuci::ApiServiceProvider::menu_; |
||||
std::shared_ptr<Notebook::Controller> libjuci::ApiServiceProvider::notebook_; |
||||
|
||||
/////////////////////////////
|
||||
//// API ServiceProvider ////
|
||||
/////////////////////////////
|
||||
libjuci::ApiServiceProvider::ApiServiceProvider( ) { |
||||
std::cout << "Apiservice std.ctor" << std::endl; |
||||
} |
||||
|
||||
void libjuci::ApiServiceProvider::ReplaceWord(std::string word) { |
||||
Glib::RefPtr<Gtk::TextBuffer> 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 libjuci::ApiServiceProvider::ReplaceLine(std::string line) { |
||||
std::cout << "Unimplemented function: ApiServiceProvider::ReplaceLine(string)" |
||||
<< std::endl; |
||||
} |
||||
|
||||
std::string libjuci::ApiServiceProvider::GetWord() { |
||||
Glib::RefPtr<Gtk::TextBuffer> buffer = libjuci::BufferFromNotebook(); |
||||
Gtk::TextIter word_start = libjuci::IterFromNotebook(); |
||||
Gtk::TextIter word_end = libjuci::IterFromNotebook(); |
||||
|
||||
std::cout << "The string: " << g_test_string << std::endl; |
||||
libjuci::IterToWordStart(word_start); |
||||
libjuci::IterToWordEnd(word_end); |
||||
|
||||
std::cout <<"start: " << word_start << std::endl << "end: " << word_end << std::endl; |
||||
if(word_start < word_end) { |
||||
std::string word = buffer->get_text(word_start, word_end); |
||||
return word; |
||||
} |
||||
return ""; |
||||
} |
||||
|
||||
void libjuci::ApiServiceProvider::AddKeybinding() { |
||||
|
||||
libjuci::ApiServiceProvider::menu_->keybindings_.action_group_menu() |
||||
->add(Gtk::Action::create("PluginAddSnippet", |
||||
"Add snippet"), |
||||
Gtk::AccelKey("<control><alt>space"), |
||||
[]() { |
||||
std::cout << "ctrl alt space" << std::endl; |
||||
libjuci::LoadPlugin("snippet"); |
||||
}); |
||||
std::cout << "addkeybinding" << std::endl; |
||||
} |
||||
|
||||
///////////////////////
|
||||
//// Api to python ////
|
||||
///////////////////////
|
||||
void libjuci::ReplaceWord(const std::string word_) { |
||||
libjuci::ApiServiceProvider::ReplaceWord(word_); |
||||
} |
||||
|
||||
void cpp::ReplaceLine(const std::string line) { |
||||
//TODO implement api::ReplaceLine / change string to iter?
|
||||
void libjuci::ReplaceLine(const std::string line) { |
||||
//TODO forgie: implement libjuci::ReplaceLine / change string to iter?
|
||||
//some_namespace::controller::replaceLine(line_);
|
||||
std::cout << "unimplemented function: 'api::ReplaceLine()' called" |
||||
std::cout << "unimplemented function: 'libjuci::ReplaceLine()' called" |
||||
<< std::endl; |
||||
} |
||||
std::string libjuci::GetWord() { |
||||
// boost::python::str converted(libjuci::ApiServiceProvider::GetWord() );
|
||||
return libjuci::ApiServiceProvider::GetWord(); |
||||
// return converted;
|
||||
} |
||||
//////////////////////////////
|
||||
//// Boost.Python methods ////
|
||||
//////////////////////////////
|
||||
|
||||
//helpers
|
||||
boost::python::api::object py::openPythonScript(const std::string path, |
||||
boost::python::api::object libjuci::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) { |
||||
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<std::string>(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<std::string>(pySnippet);
|
||||
//do something with return_value
|
||||
}catch(boost::python::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<Gtk::TextBuffer> libjuci::BufferFromNotebook() { |
||||
return Glib::RefPtr<Gtk::TextBuffer>(libjuci::ApiServiceProvider::notebook_->source_vec_.back()->view().get_buffer()); |
||||
} |
||||
|
||||
Gtk::TextIter libjuci::IterFromNotebook() { |
||||
return libjuci::BufferFromNotebook()->get_insert()->get_iter(); |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
@ -1,7 +1,10 @@
|
||||
#include "api.h" |
||||
|
||||
BOOST_PYTHON_MODULE(juci_to_python_api) { |
||||
using namespace boost::python; |
||||
// text editing
|
||||
def("replaceLine", &juci_api::cpp::ReplaceLine); |
||||
def("replaceWord", &juci_api::cpp::ReplaceWord); |
||||
}// module::juci
|
||||
def("replaceLine", &libjuci::ReplaceLine); |
||||
def("replaceWord", &libjuci::ReplaceWord); |
||||
def("getWord", &libjuci::GetWord); |
||||
//something more
|
||||
}// module::juci_to_python_api
|
||||
|
||||
@ -1,57 +0,0 @@
|
||||
cmake_minimum_required (VERSION 2.4) |
||||
set(project_name plugin) |
||||
project (${project_name}) |
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") |
||||
|
||||
include(FindPkgConfig) |
||||
|
||||
#Boost_FOUND - True if headers and requested libraries were found |
||||
#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.55 REQUIRED COMPONENTS python system ) |
||||
|
||||
if(!${Boost_FOUND}) |
||||
message(FATAL_ERROR "Boost libraries not found") |
||||
endif() |
||||
|
||||
#GTKMM_FOUND - True if headers and requested libraries were found |
||||
#GTKMM_INCLUDE_DIRS - GTKMM include directories |
||||
#GTKMM_LIBRARY_DIRS - Link directories for GTKMM libraries |
||||
#GTKMM_LIBRARIES - GTKMM component libraries to be linked |
||||
pkg_check_modules(GTKMM REQUIRED gtkmm-3.0) |
||||
|
||||
if(!${GTKMM_FOUND}) |
||||
message(FATAL_ERROR "Gtkmm not found") |
||||
endif() |
||||
|
||||
#PYTHONLIBS_FOUND - True if headers and requested libraries were found |
||||
#PYTHON_INCLUDE_DIRS - GTKMM include directories |
||||
#PYTHON_LIBRARIES - GTKMM component libraries to be linked |
||||
#$PYTHONLIBS_VERSION_STRING - ersion string |
||||
find_package(PythonLibs 2.7 REQUIRED) |
||||
|
||||
if(!${PYTHONLIBS_FOUND}) |
||||
message(FATAL_ERROR "Pythonlibs not found") |
||||
else() |
||||
message("Found python libs ${PYTHONLIBS_VERSION_STRING}") |
||||
endif() |
||||
|
||||
ADD_LIBRARY(plugintest_ext MODULE |
||||
plugin.h |
||||
plugin.cc |
||||
plugin_ext.cc |
||||
) |
||||
|
||||
set_target_properties(plugintest_ext PROPERTIES PREFIX "") |
||||
target_link_libraries(plugintest_ext ${Boost_LIBRARIES}) |
||||
|
||||
add_executable(${project_name} |
||||
plugin.h |
||||
plugin.cc |
||||
) |
||||
|
||||
include_directories(${PYTHON_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${GTKMM_INCLUDE_DIRS}) |
||||
link_directories(${GTKMM_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS} ${PYTHON_LIBRARIES}) |
||||
target_link_libraries(${project_name} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${GTKMM_LIBRARIES}) |
||||
@ -1,118 +0,0 @@
|
||||
#include "plugin.h" |
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// setPythonVar:
|
||||
// initiates a string value (k_var_value) to a declared variable name
|
||||
// (k_var_name) within a given namespace(python_name_space)
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bp::object Plugin::setPythonVar(const std::string k_var_name, |
||||
const std::string k_var_value, |
||||
bp::object python_name_space ) {
|
||||
std::string temp = k_var_name + " = \"" + k_var_value + "\""; |
||||
bp::str the_var(temp); |
||||
return bp::exec(the_var, python_name_space); |
||||
} |
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// openPythonScript:
|
||||
// Opens a python plugin script within a file path and a python namespace
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bp::object Plugin::openPythonScript(const std::string path, |
||||
bp::object python_name_space) { |
||||
std::string temp = g_project_root + "plugins/" + path + "/" + path + ".py"; |
||||
bp::str the_path(temp); |
||||
return bp::exec_file(the_path, python_name_space); |
||||
} |
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// for testing purposes only
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::string Plugin::get_test_value2(){ |
||||
return "STRING FROM WITHIN C++"; |
||||
} |
||||
|
||||
/* public functions */ |
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// snippet:
|
||||
// takes a std::string and converts it into a matching snippet
|
||||
// if no matching snippet, returns the same string
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Plugin::snippet(std::string& word){ |
||||
try{ |
||||
/* initialize python interpreter */ |
||||
Py_Initialize(); |
||||
bp::object main_module = bp::import("__main__"); |
||||
bp::object main_namespace = main_module.attr("__dict__"); |
||||
|
||||
/* runs script from python */ |
||||
bp::object ignored1 = setPythonVar("word", word, main_namespace); |
||||
bp::object ignored2 = openPythonScript(__func__, main_namespace); |
||||
|
||||
/* extracts desired values */
|
||||
bp::object pySnippet = bp::eval("getSnippet()", main_namespace); |
||||
word = bp::extract<std::string>(pySnippet); |
||||
/* add snippet to textView */ |
||||
//TODO add snippet
|
||||
|
||||
}catch(bp::error_already_set const&){ |
||||
PyErr_Print(); |
||||
} |
||||
} |
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// get_test_value:
|
||||
// for testing purposes
|
||||
// uses a python module generated from c++
|
||||
// calls c++ function from python
|
||||
// returns string to c++ from python
|
||||
// prints the string from python within c++
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Plugin::get_test_value(){ |
||||
try{ |
||||
/* initialize python interpreter */ |
||||
Py_Initialize(); |
||||
bp::object main_module = bp::import("__main__"); |
||||
bp::object main_namespace = main_module.attr("__dict__"); |
||||
|
||||
/* runs script from python */ |
||||
const std::string path("test"); |
||||
bp::object ignored2 = openPythonScript(path, main_namespace); |
||||
|
||||
/* extracts desired values */ |
||||
bp::object pySnippet = bp::eval("get_test_value()", main_namespace); |
||||
std::string mword = bp::extract<std::string>(pySnippet); |
||||
/* add snippet to textView */ |
||||
std::cout << mword << std::endl; |
||||
//TODO add snippet
|
||||
|
||||
}catch(bp::error_already_set const&){ |
||||
PyErr_Print(); |
||||
} |
||||
} |
||||
|
||||
int main(int argc, char *argv[]) |
||||
{ |
||||
std::string word("ifelse"); |
||||
Plugin::snippet(word); |
||||
//Plugin::get_test_value();
|
||||
std::cout << word << std::endl; |
||||
return 0; |
||||
|
||||
} |
||||
@ -1,28 +0,0 @@
|
||||
#ifndef JUCI_PLUGIN_H_ |
||||
#define JUCI_PLUGIN_H_ |
||||
|
||||
#include <boost/python.hpp> |
||||
#include <Python.h> |
||||
|
||||
#include <string> |
||||
#include <iostream> |
||||
|
||||
namespace bp = boost::python; |
||||
|
||||
const std::string g_project_root("~/q6/testing/plugin/"); |
||||
//TODO (forgie) get current working directory..
|
||||
|
||||
class Plugin{ |
||||
public: |
||||
static void snippet(std::string& word); |
||||
static void get_test_value(); |
||||
static std::string get_test_value2();
|
||||
private: |
||||
static bp::object setPythonVar(const std::string varName, |
||||
const std::string varValue, |
||||
bp::object python_name_space); |
||||
static bp::object openPythonScript(const std::string path, |
||||
bp::object python_name_space); |
||||
}; |
||||
|
||||
#endif // JUCI_PLUGIN_H_
|
||||
@ -1,8 +0,0 @@
|
||||
#include "plugin.h" |
||||
#include <boost/python.hpp> |
||||
using namespace boost::python; |
||||
|
||||
BOOST_PYTHON_MODULE(plugintest_ext) |
||||
{ |
||||
def("get_test_value2", &Plugin::get_test_value2); |
||||
} |
||||
@ -1,39 +0,0 @@
|
||||
/* |
||||
void theprogram() |
||||
{ |
||||
std::string input; |
||||
std::cout << "Enter string to transform: "; |
||||
std::getline(std::cin, input); |
||||
//std::string inttransformed = CallPythonPlugIn(input); |
||||
std::cout << "The transformed string is: " << input << std::endl; |
||||
} |
||||
|
||||
int test1() |
||||
{ |
||||
try{ |
||||
Py_Initialize(); |
||||
object main_module = import("__main__"); |
||||
object main_namespace = main_module.attr("__dict__"); |
||||
object ignored = exec("result = 5 ** 2", main_namespace); |
||||
int five_squared = extract<int>(main_namespace["result"]); |
||||
|
||||
printf("%d\n", five_squared); |
||||
|
||||
return five_squared; |
||||
} |
||||
catch(error_already_set const&){ |
||||
PyErr_Print(); |
||||
} |
||||
|
||||
return -1; |
||||
} |
||||
|
||||
void test2(int theNum, int &test) |
||||
{ |
||||
int seven = 7; |
||||
|
||||
test = 7+ theNum; |
||||
|
||||
std::cout << theNum << std::endl; |
||||
} |
||||
*/ |
||||
@ -0,0 +1,50 @@
|
||||
#!/usr/bin/python |
||||
#snippet plugin |
||||
import juci_to_python_api |
||||
|
||||
snippets = {} |
||||
snippets["for"] = """\ |
||||
for(#int i=0; #i<#v.size(); #i++) { |
||||
std::cout << v[i] << std::endl; |
||||
} |
||||
""" |
||||
snippets["if"] = """\ |
||||
if(#) { |
||||
# |
||||
} |
||||
""" |
||||
snippets["ifelse"] = """\ |
||||
if(#) { |
||||
# |
||||
} else { |
||||
# |
||||
} |
||||
""" |
||||
snippets["while"] = """\ |
||||
while(#) { |
||||
# |
||||
} |
||||
""" |
||||
snippets["main"] = """\ |
||||
int main(int argc, char *argv[]) { |
||||
//Do something |
||||
} |
||||
""" |
||||
snippets["hello"] = """\ |
||||
#include <iostream> |
||||
|
||||
int main(int argc, char *argv[]) { |
||||
std::cout << "Hello, world! << std::endl; |
||||
} |
||||
""" |
||||
def getSnippet(word): |
||||
try: |
||||
output = snippets[word] |
||||
except KeyError: |
||||
output = word |
||||
return output |
||||
|
||||
theWord=juci_to_python_api.getWord() |
||||
output=getSnippet(theWord) |
||||
|
||||
juci_to_python_api.replaceWord(output) |
||||
Loading…
Reference in new issue