Skip to content
Snippets Groups Projects
Commit 96edc645 authored by florent's avatar florent
Browse files

When building python Entity classes, populate methods with docstrings.

     * src/dynamic-graph-py.cc,
     * src/dynamic_graph/entity.py,
     * src/entity-py.cc.
parent 8148ad42
No related branches found
No related tags found
No related merge requests found
......@@ -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,
......
......@@ -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):
......
......@@ -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());
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment