-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: Add basic PythonPlugin example
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
add_executable(python_plugin_manager python_plugin_manager.cpp) | ||
target_link_libraries(python_plugin_manager PRIVATE ${LIBRARY_TARGET}) | ||
|
||
add_custom_command( | ||
TARGET python_plugin_manager | ||
POST_BUILD | ||
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/plugin.py | ||
$<TARGET_FILE_DIR:python_plugin_manager>/plugin_1.py | ||
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/plugin.py | ||
$<TARGET_FILE_DIR:python_plugin_manager>/plugin_2.py) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import time | ||
|
||
|
||
def initialize(something): | ||
print("initialize: ", something) | ||
|
||
|
||
def loop(something): | ||
# this only works because of the I/O and sleep; | ||
# a loop without any "non-python" operations may block all threads | ||
# wanting to execute python plugins; | ||
# if python 3.12 or newer is used, multiple threads can execute | ||
# python code at the same time | ||
while True: | ||
print("loop: ", something) | ||
time.sleep(1) | ||
|
||
|
||
print("loading") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "plugin_manager.h" | ||
#include "python/plugin.h" | ||
|
||
#include <exception> | ||
#include <filesystem> | ||
#include <iostream> | ||
#include <thread> | ||
#include <tuple> | ||
#include <utility> | ||
#include <vector> | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
try { | ||
if (argc < 1) { | ||
return -1; | ||
} | ||
// path to plugins can be passed via command line; | ||
// if no path was specified, the location of the executable is used instead | ||
std::filesystem::path plugin_dir; | ||
if (argc < 2) { | ||
plugin_dir = std::filesystem::path { argv[0] }.parent_path(); | ||
} else { | ||
plugin_dir = std::filesystem::path { argv[1] }; | ||
} | ||
ppplugin::GenericPluginManager<ppplugin::PythonPlugin> manager; | ||
std::vector<std::thread> threads; | ||
int plugin_number {}; | ||
|
||
// recursively traverse filesystem to find scripts | ||
const std::filesystem::recursive_directory_iterator dir_iterator { plugin_dir }; | ||
for (const auto& entry : dir_iterator) { | ||
if (!entry.is_regular_file()) { | ||
continue; | ||
} | ||
const auto& path = entry.path(); | ||
// only load files ending with ".py" and execute in separate thread | ||
if (path.extension() == ".py") { | ||
auto plugin = manager.loadPythonPlugin(path); | ||
if (plugin) { | ||
threads.emplace_back([plugin = std::move(*plugin), plugin_number = plugin_number++]() mutable { | ||
// ignore calling errors | ||
std::ignore = plugin.call<void>("initialize", plugin_number); | ||
std::ignore = plugin.call<void>("loop", std::to_string(plugin_number)); | ||
}); | ||
} else { | ||
std::cerr << "Failed to load " << path << '\n'; | ||
} | ||
} | ||
} | ||
|
||
for (auto& thread : threads) { | ||
thread.join(); | ||
} | ||
} catch (const std::exception& exception) { | ||
std::cerr << "A fatal error occurred: '" << exception.what() << "'\n"; | ||
return 1; | ||
} catch (...) { | ||
std::cerr << "An unknown fatal error occurred!"; | ||
return 1; | ||
} | ||
return 0; | ||
} |