Skip to content

Commit a9ffee5

Browse files
committed
Working MessageHandler with context
1 parent bfafe80 commit a9ffee5

File tree

5 files changed

+37
-32
lines changed

5 files changed

+37
-32
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,21 @@ namespace sofapython3
8787
default:
8888
return "Other";
8989
}
90-
};
90+
}();
9191

92-
auto component = [&m]() {
92+
Base* component = [&m]() -> Base* {
9393
sofa::helper::logging::SofaComponentInfo* nfo = dynamic_cast<sofa::helper::logging::SofaComponentInfo*>(m.componentInfo().get());
9494
if( nfo != nullptr )
95-
return py::cast(nfo->m_component);
96-
return py::object();
97-
};
98-
99-
py::dict msg("type"_a=typeStr,
100-
"isEmpty"_a=m.empty(),
101-
"sender"_a=m.sender(),
102-
"message"_a=m.messageAsString(),
103-
"component"_a=component
104-
);
105-
95+
return const_cast<Base*>(nfo->m_component);
96+
return nullptr;
97+
}();
98+
99+
py::dict msg;
100+
msg["type"] = py::str(typeStr);
101+
msg["isEmpty"]=py::cast(m.empty());
102+
msg["sender"]=py::str(m.sender());
103+
msg["message"]=py::str(m.messageAsString());
104+
msg["component"]=component ? PythonFactory::toPython(component) : py::none();
106105
py::object fct = self.attr("process")(msg);
107106
}
108107
}
@@ -121,11 +120,14 @@ namespace sofapython3
121120
}));
122121

123122
f.def("process", &PyMessageHandler::process);
124-
f.def("__enter__", [](PyMessageHandler* self){
123+
f.def("__enter__", [](PyMessageHandler* self) -> PyMessageHandler*
124+
{
125125
PythonEnvironment::gil acquire {"MessageHandler"};
126126
sofa::helper::logging::MessageDispatcher::addHandler(self);
127+
return self;
127128
});
128-
f.def("__exit__", [](PyMessageHandler* self){
129+
f.def("__exit__", [](PyMessageHandler* self, py::object /*exc_type*/, py::object /*exc_value*/, py::object /*traceback*/)
130+
{
129131
PythonEnvironment::gil acquire {"MessageHandler"};
130132
sofa::helper::logging::MessageDispatcher::rmHandler(self);
131133
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ SP3_add_python_module(
1818
MODULE_NAME Simulation
1919
SOURCES ${SOURCE_FILES}
2020
HEADERS ${HEADER_FILES}
21-
DEPENDS SofaCore SofaSimulationCore SofaSimulationGraph SofaBaseVisual
22-
)
21+
DEPENDS SofaHelper SofaCore SofaSimulationCore SofaSimulationGraph SofaBaseVisual
22+
)

bindings/Sofa/src/SofaPython3/Sofa/Simulation/Submodule_Simulation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ using sofa::simulation::Simulation;
4141

4242
#include <sofa/core/visual/VisualParams.h>
4343
#include <sofa/helper/logging/MessageDispatcher.h>
44+
using sofa::helper::logging::MessageDispatcher;
4445
#include <sofa/helper/logging/ConsoleMessageHandler.h>
4546
#include "Submodule_Simulation_doc.h"
4647

bindings/Sofa/tests/Helper/Message.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,11 @@ class MsgHandler(Sofa.Helper.MessageHandler):
88
def __init__(self):
99
Sofa.Helper.MessageHandler.__init__(self)
1010

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)
11+
def process(self, msg):
12+
self.type = msg["type"]
13+
self.sender = msg["sender"]
14+
self.component = msg["component"]
15+
self.message = msg["message"]
2216

2317

2418
class Test(unittest.TestCase):
@@ -35,9 +29,17 @@ def test_messages(self):
3529

3630

3731
def test_messageHandler(self):
38-
with MsgHandler():
32+
with MsgHandler() as handler:
3933
Sofa.Helper.msg_info("plop")
40-
Sofa.Helper.msg_error("pouet")
41-
Sofa.Helper.msg_warning("coucou")
34+
self.assertEqual(handler.type, "Info")
35+
self.assertEqual(handler.sender, "PythonScript")
36+
self.assertEqual(handler.component, None)
37+
self.assertEqual(handler.message, "plop")
38+
39+
Sofa.Helper.msg_warning("coucou")
40+
self.assertEqual(handler.type, "Warning")
41+
self.assertEqual(handler.sender, "PythonScript")
42+
self.assertEqual(handler.component, None)
43+
self.assertEqual(handler.message, "coucou")
4244

4345

bindings/Sofa/tests/Simulation/Node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_getItem(self):
157157

158158
self.assertEqual(root["node1.node2.object2.name"], root.node1.node2.object2.name)
159159

160-
def test_getRoot(self):
160+
def test_getRoot(self):
161161
root = Sofa.Core.Node("root")
162162
node = root.addChild("node")
163163
self.assertEqual(node.getRoot(), root)

0 commit comments

Comments
 (0)