From c4b85ea287d9a5b0ab51f5d58cabdaee64d324bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20L=C3=BCke?= Date: Wed, 1 Jan 2025 20:09:27 +0100 Subject: [PATCH] mana python plugin: pass filename and restructure a bit --- src/CMakeLists.txt | 7 +++- src/mana_plugin_python.cc | 58 +++++++++++++++++++++------- src/mana_python_plugin_test_numpy.py | 18 +++++++++ 3 files changed, 67 insertions(+), 16 deletions(-) create mode 100644 src/mana_python_plugin_test_numpy.py diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c1c808b..164d471 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -44,9 +44,9 @@ target_link_libraries(mana-plugin-c-test PRIVATE rxi-logc) add_library(mana-plugin-cpp-test SHARED mana_plugin_cpp_test.cc) target_link_libraries(mana-plugin-cpp-test PRIVATE rxi-logc) -find_package(ROOT COMPONENTS Hist) +find_package(ROOT COMPONENTS Hist QUIET) if (ROOT_FOUND) - message("-- Using ROOT installation from ${ROOT_USE_FILE}") + message(STATUS "mnode: Using ROOT installation from ${ROOT_USE_FILE}") include(${ROOT_USE_FILE}) add_library(mana-plugin-root-histogram SHARED mana_plugin_root_histogram.cc) @@ -54,12 +54,15 @@ if (ROOT_FOUND) add_library(mana-plugin-root-rntuple-writer SHARED mana_plugin_root_rntuple_writer.cc) target_link_libraries(mana-plugin-root-rntuple-writer PRIVATE mana mesytec-mnode rxi-logc ${ROOT_LIBRARIES}) +else() + message(STATUS "mnode: ROOT not found, ROOT plugins will not be built") endif() if (pybind11_FOUND) add_library(mana-plugin-python SHARED mana_plugin_python.cc) target_link_libraries(mana-plugin-python PRIVATE mana mesytec-mnode rxi-logc pybind11::embed) file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/mana_python_test_plugin.py ${CMAKE_BINARY_DIR}/mana_python_test_plugin.py COPY_ON_ERROR SYMBOLIC) + file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/mana_python_plugin_test_numpy.py ${CMAKE_BINARY_DIR}/mana_python_plugin_test_numpy.py COPY_ON_ERROR SYMBOLIC) endif() add_library(mana-plugin-nng-server SHARED mana_plugin_nng_server.cc) diff --git a/src/mana_plugin_python.cc b/src/mana_plugin_python.cc index 5c4f67b..fc6f9ab 100644 --- a/src/mana_plugin_python.cc +++ b/src/mana_plugin_python.cc @@ -1,12 +1,11 @@ -#include "internal/mana_lib.hpp" #include -extern "C" -{ -#include "internal/rxi/log.h" -} #include #include +#include "internal/argh.h" +#include "internal/mana_lib.hpp" +#include "internal/rxi/log.hpp" + using namespace mesytec; using namespace mesytec::mnode; namespace py = pybind11; @@ -26,6 +25,17 @@ struct Context py::object py_end_run; std::vector> eventBuffers; }; + +void reload_py(Context *ctx) +{ + ctx->usercode.reload(); + // TODO: check if the retrieved attributes are callable + ctx->py_begin_run = ctx->usercode.attr("begin_run"); + ctx->py_process_event = ctx->usercode.attr("process_event"); + ctx->py_process_system_event = ctx->usercode.attr("process_system_event"); + ctx->py_end_run = ctx->usercode.attr("end_run"); +} + } // namespace MANA_DEFINE_PLUGIN_INIT(init) @@ -34,9 +44,34 @@ MANA_DEFINE_PLUGIN_INIT(init) static Context g_ctx; auto ctx = &g_ctx; log_debug("init: ctx=%p", ctx); - // TODO: pass args to init() - // TODO: catch exceptions - ctx->usercode = py::module::import("mana_python_test_plugin"); + try + { + argh::parser parser; + parser.parse(plugin_argc, plugin_argv); + if (parser.size() != 1) + { + log_error("missing argument"); + return nullptr; + } + auto filename = parser[0]; + ctx->usercode = py::module_::import(filename.c_str()); + try + { + // py::exec(R"(import debugpy; debugpy.listen(5678))"); + // py::exec(R"(import numpy)"); + // py::module::import("numpy"); + } + catch (const std::exception &e) + { + log_warn("warning: startup code failed: %s", e.what()); + } + reload_py(ctx); + } + catch (const std::exception &e) + { + log_error("error: %s", e.what()); + return nullptr; + } return ctx; } @@ -47,12 +82,7 @@ MANA_DEFINE_PLUGIN_BEGIN_RUN(begin_run) log_info("begin_run: ctx=%p", context); auto jRun = nlohmann::json::parse(descriptor_json); auto ctx = reinterpret_cast(context); - ctx->usercode.reload(); - // TODO: check if the retrieved attributes are callable - ctx->py_begin_run = ctx->usercode.attr("begin_run"); - ctx->py_process_event = ctx->usercode.attr("process_event"); - ctx->py_process_system_event = ctx->usercode.attr("process_system_event"); - ctx->py_end_run = ctx->usercode.attr("end_run"); + reload_py(ctx); ctx->py_begin_run(descriptor_json); ctx->eventBuffers.clear(); ctx->eventBuffers.resize(jRun["events"].size()); diff --git a/src/mana_python_plugin_test_numpy.py b/src/mana_python_plugin_test_numpy.py new file mode 100644 index 0000000..71642fe --- /dev/null +++ b/src/mana_python_plugin_test_numpy.py @@ -0,0 +1,18 @@ + +import numpy +import sys + +def begin_run(runDescription: str): + print(f"python: begin_run - python version: {sys.version}") + print(f"numpy: {numpy.__version__}") + +def end_run(runDescription: str): + print(f"python: end_run") + +def process_event(eventIndex: int, dataArrays): + return + print(f"event[{eventIndex}]: {dataArrays}") + +def process_system_event(data): + return + print(f"python: system_event: size={len(data)}")