From 22bf01be448a27dd675e5f1af545d31f6a5c486e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Sun, 10 Mar 2019 16:55:35 +0100 Subject: [PATCH] move plugin implementation to own file --- src/CMakeLists.txt | 2 +- src/plugins.cc | 41 ++++++++++++++++++++++++++++ src/plugins.h | 47 ++------------------------------ tests/python_interpreter_test.cc | 2 ++ 4 files changed, 47 insertions(+), 45 deletions(-) create mode 100644 src/plugins.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bc07f4e..fcce2b4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,6 +12,7 @@ set(JUCI_SHARED_FILES menu.cpp meson.cpp project_build.cpp + plugins.cc python_interpreter.cc snippets.cpp source.cpp @@ -61,7 +62,6 @@ if(APPLE) list(APPEND JUCI_SOURCES window_macos.m) endif() - set(JUCI_TARGET_LIBRARIES juci_shared ) diff --git a/src/plugins.cc b/src/plugins.cc new file mode 100644 index 0000000..96f27b3 --- /dev/null +++ b/src/plugins.cc @@ -0,0 +1,41 @@ +#include "plugins.h" +#include "config.h" +#include "python_module.h" + +Plugins::Plugins(): jucipp_module("Jucipp", Module::init_jucipp_module) { + auto &config = Config::get(); + config.load(); + const auto sys = py::module::import("sys"); + sys.attr("path").cast().append(config.plugins.path); + } + +void Plugins::load() { + std::cout << Config::get().plugins.path << std::endl; + boost::filesystem::directory_iterator end_it; + for(boost::filesystem::directory_iterator it(Config::get().plugins.path); it != end_it; it++) { + auto module_name = it->path().stem().string(); + if(module_name.empty()) + continue; + const auto is_directory = boost::filesystem::is_directory(it->path()); + const auto has_py_extension = it->path().extension() == ".py"; + const auto is_pycache = module_name == "__pycache__"; + if((is_directory && !is_pycache) || has_py_extension) { + try { + auto module = interpreter.add_module(module_name); + if (module) { + Terminal::get().print("Reloading plugin ´" + module_name + "´\n"); + interpreter.reload_module(module); + } else { + Terminal::get().print("Loading plugin ´" + module_name + "´\n"); + py::module::import(module_name.c_str()); + } + + } + catch(py::error_already_set &error) { + Terminal::get().print("Error loading plugin `" + module_name + "`:\n" + error.what() + "\n"); + } + } + if(interpreter.error()) + std::cerr << py::error_already_set().what() << std::endl; + } +} diff --git a/src/plugins.h b/src/plugins.h index 98a0d5e..f7177b5 100644 --- a/src/plugins.h +++ b/src/plugins.h @@ -1,54 +1,13 @@ #pragma once -#include "config.h" -#include "menu.h" -#include "python_interpreter.h" -#include "python_module.h" #include -#include +#include "python_interpreter.h" class __attribute__((visibility("default"))) Plugins { public: - Plugins() : jucipp_module("Jucipp", Module::init_jucipp_module) { - auto &config = Config::get(); - config.load(); - auto sys = py::module::import("sys"); - sys.attr("path").cast().append(config.plugins.path); - // sys.attr("excepthook") = py::cpp_function([](py::object type, py::object value, py::object traceback) { - // Terminal::get().print("const std::string &message\n"); - // }); - } - void load() { - boost::filesystem::directory_iterator end_it; - for(boost::filesystem::directory_iterator it(Config::get().plugins.path); it != end_it; it++) { - auto module_name = it->path().stem().string(); - if(module_name.empty()) - continue; - const auto is_directory = boost::filesystem::is_directory(it->path()); - const auto has_py_extension = it->path().extension() == ".py"; - const auto is_pycache = module_name == "__pycache__"; - if((is_directory && !is_pycache) || has_py_extension) { - try { - auto module = interpreter.add_module(module_name); - if (module) { - Terminal::get().print("Reloading plugin ´" + module_name + "´\n"); - interpreter.reload_module(module); - } else { - Terminal::get().print("Loading plugin ´" + module_name + "´\n"); - py::module::import(module_name.c_str()); - } - - } - catch(py::error_already_set &error) { - Terminal::get().print("Error loading plugin `" + module_name + "`:\n" + error.what() + "\n"); - } - } - if(interpreter.error()) - std::cerr << py::error_already_set().what() << std::endl; - } - } - + Plugins(); + void load(); private: py::detail::embedded_module jucipp_module; Python::Interpreter interpreter; diff --git a/tests/python_interpreter_test.cc b/tests/python_interpreter_test.cc index ed877ae..3773e29 100644 --- a/tests/python_interpreter_test.cc +++ b/tests/python_interpreter_test.cc @@ -3,6 +3,8 @@ #include "python_interpreter.h" #include "python_module.h" #include "terminal.h" +#include "config.h" +#include "python_module.h" class __attribute__((visibility("default")))