mana python plugin: pass filename and restructure a bit

This commit is contained in:
Florian Lüke 2025-01-01 20:09:27 +01:00
parent dd939b4808
commit c4b85ea287
3 changed files with 67 additions and 16 deletions

View file

@ -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)

View file

@ -1,12 +1,11 @@
#include "internal/mana_lib.hpp"
#include <mesytec-mvlc/cpp_compat.h>
extern "C"
{
#include "internal/rxi/log.h"
}
#include <pybind11/embed.h>
#include <pybind11/stl.h>
#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<std::vector<py::object>> 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 <script.py> 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 *>(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());

View file

@ -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)}")