From 96edc6454b0f094e94e189fdfd57f6ae37086edf Mon Sep 17 00:00:00 2001 From: florent <florent@laas.fr> Date: Thu, 25 Nov 2010 08:53:11 +0100 Subject: [PATCH] When building python Entity classes, populate methods with docstrings. * src/dynamic-graph-py.cc, * src/dynamic_graph/entity.py, * src/entity-py.cc. --- src/dynamic-graph-py.cc | 5 +++++ src/dynamic_graph/entity.py | 6 ++++-- src/entity-py.cc | 26 ++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/dynamic-graph-py.cc b/src/dynamic-graph-py.cc index a6235bc..ced6b0f 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 e570914..3571a81 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 7f0b604..6ca8110 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()); + } } } } -- GitLab