diff --git a/src/dynamic-graph-py.cc b/src/dynamic-graph-py.cc index a6235bc4fefbf91a0d0a2f93ff5b9f8dd9b38639..ced6b0f9b84b9754dd294c782609acaf1ffa5947 100644 --- a/src/dynamic-graph-py.cc +++ b/src/dynamic-graph-py.cc @@ -30,6 +30,7 @@ namespace dynamicgraph { extern PyObject* displaySignals(PyObject* self, PyObject* args); extern PyObject* executeCommand(PyObject* self, PyObject* args); extern PyObject* listCommands(PyObject* self, PyObject* args); + extern PyObject* getCommandDocstring(PyObject* self, PyObject* args); } namespace factory { @@ -137,6 +138,10 @@ static PyMethodDef dynamicGraphMethods[] = { dynamicgraph::python::entity::listCommands, METH_VARARGS, "list the commands of an entity"}, + {"entity_get_command_docstring", + dynamicgraph::python::entity::getCommandDocstring, + METH_VARARGS, + "get the docstring of an entity command"}, {"factory_get_entity_class_list", dynamicgraph::python::factory::getEntityClassList, METH_VARARGS, diff --git a/src/dynamic_graph/entity.py b/src/dynamic_graph/entity.py index e570914924157da88fe01b34d0813fb2bfb14574..3571a81068d21dcb2e8090e69fb8cbe37a617e5b 100644 --- a/src/dynamic_graph/entity.py +++ b/src/dynamic_graph/entity.py @@ -7,9 +7,10 @@ import wrap, signal_base entityClassNameList = [] -def commandMethod(name) : +def commandMethod(name, docstring) : def method(self, *arg): return wrap.entity_execute_command(self.object, name, arg) + method.__doc__ = docstring return method def initEntity(self, name): @@ -22,7 +23,8 @@ def initEntity(self, name): commands = wrap.entity_list_commands(self.object) # for each command, add a method with the name of the command for command in commands: - setattr(self.__class__, command, commandMethod(command)) + docstring = wrap.entity_get_command_docstring(self.object, command) + setattr(self.__class__, command, commandMethod(command, docstring)) self.__class__.commandCreated = True def updateEntityClasses(dictionary): diff --git a/src/entity-py.cc b/src/entity-py.cc index 7f0b6040599703d737464e646bf98afa3df4d125..6ca8110e2b6b676c49a471d785f25dea0475ab22 100644 --- a/src/entity-py.cc +++ b/src/entity-py.cc @@ -458,6 +458,32 @@ namespace dynamicgraph { } return result; } + PyObject* getCommandDocstring(PyObject* self, PyObject* args) + { + PyObject* object = NULL; + char* commandName; + if (!PyArg_ParseTuple(args, "Os", &object, &commandName)) { + return NULL; + } + + // Retrieve the entity instance + if (!PyCObject_Check(object)) { + PyErr_SetString(error, "first argument is not an object"); + return NULL; + } + void* pointer = PyCObject_AsVoidPtr(object); + Entity* entity = (Entity*)pointer; + typedef std::map<const std::string, command::Command*> CommandMap; + CommandMap map = entity->getNewStyleCommandMap(); + command::Command* command = NULL; + try { + command = map[commandName]; + } catch (const std::exception& exc) { + PyErr_SetString(error, exc.what()); + } + std::string docstring = command->getDocstring(); + return Py_BuildValue("s", docstring.c_str()); + } } } }