Skip to content

Commit 9822063

Browse files
committed
WIP: Add MessageHandler context manager class
1 parent 297e43e commit 9822063

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

bindings/Sofa/src/SofaPython3/Sofa/Helper/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ set(HEADER_FILES
44
${CMAKE_CURRENT_SOURCE_DIR}/Submodule_Helper.h
55
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Vector.h
66
${CMAKE_CURRENT_SOURCE_DIR}/System/Submodule_System.h
7+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MessageHandler.h
78
${CMAKE_CURRENT_SOURCE_DIR}/System/Binding_FileRepository.h
89
)
910

1011
set(SOURCE_FILES
1112
${CMAKE_CURRENT_SOURCE_DIR}/Submodule_Helper.cpp
13+
${CMAKE_CURRENT_SOURCE_DIR}/Binding_MessageHandler.cpp
1214
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Vector.cpp
1315
${CMAKE_CURRENT_SOURCE_DIR}/System/Submodule_System.cpp
1416
${CMAKE_CURRENT_SOURCE_DIR}/System/Binding_FileRepository.cpp
@@ -23,4 +25,4 @@ SP3_add_python_module(
2325
SOURCES ${SOURCE_FILES}
2426
HEADERS ${HEADER_FILES}
2527
DEPENDS SofaCore SofaPython3::Plugin
26-
)
28+
)

bindings/Sofa/src/SofaPython3/Sofa/Helper/Submodule_Helper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ along with sofaqtquick. If not, see <http://www.gnu.org/licenses/>.
3131
#include <sofa/core/objectmodel/Base.h>
3232
#include "System/Submodule_System.h"
3333
#include "Submodule_Helper.h"
34+
#include "Binding_MessageHandler.h"
3435
#include "Binding_Vector.h"
3536

3637
namespace sofapython3
@@ -151,6 +152,7 @@ PYBIND11_MODULE(Helper, helper)
151152
helper.def("msg_fatal", [](py::args args) { MESSAGE_DISPATCH(msg_fatal); },
152153
R"(Emit a fatal error message from python.)");
153154

155+
moduleAddMessageHandler(helper);
154156
moduleAddVector(helper);
155157
moduleAddSystem(helper);
156158
}

bindings/Sofa/tests/Helper/Message.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
import Sofa.Helper
55
import unittest
66

7+
class MsgHandler(Sofa.Helper.MessageHandler):
8+
def __init__(self):
9+
Sofa.Helper.MessageHandler.__init__(self)
10+
11+
def process(msg):
12+
print ("GOT MESSAGE!")
13+
print("type: ")
14+
print(msg.type)
15+
print("sender: ")
16+
print(msg.sender)
17+
print("component: ")
18+
print(msg.component.typeName())
19+
print(msg.component.getName())
20+
print("message:")
21+
print(msg.message)
22+
23+
724
class Test(unittest.TestCase):
825
def test_messages(self):
926
"""Test that the message are correctly sended and does not generates exceptions"""
@@ -15,3 +32,12 @@ def test_messages(self):
1532
f(Sofa.Core.Node("node"), "Simple message to an object")
1633
f(Sofa.Core.Node("node"), "Simple message to an object with attached source info", "sourcefile.py", 10)
1734

35+
36+
37+
def test_messageHandler(self):
38+
with MsgHandler():
39+
Sofa.Helper.msg_info("plop")
40+
Sofa.Helper.msg_error("pouet")
41+
Sofa.Helper.msg_warning("coucou")
42+
43+

0 commit comments

Comments
 (0)