I'm adding a python module in a c++ code, using boost::python. The c++ project is documented with doxygen. I want to create a documentation for the python module but I don't know how not to be redundant like this :
#include <boost/python.hpp>
using namespace boost::python;
/** @brief Sum two integers
* @param a an integer
* @param b another integer
* @return sum of integers
*/
int sum(int a, int b)
{
return a+b;
}
BOOST_PYTHON_MODULE(pymodule)
{
def("sum",&sum,args("a","b"),
"Sum two integers.\n\n:param a: an integer\n:param b: another integer\n:returns: sum of integers");
};
Here I say the same thing in docstring and doxygen comments. Any ideas ?
Edit: The c++ doc isn't public and the python interface is a subset of c++.
The 5gon12eder's idea is to extract doxygen comments and substitute them into python docstrings. He proposed a solution with a python script.
Here's another one with a CMake script because I'm using it to build my project. I hope it can help people with the same problem :
pymodule.cpp.in :
In this case, the script will generate pymodule.cpp with good docstring.
I'm a fan of code generation and I believe that this is a reasonable situation to deploy it.
If you're a bit disciplined in writing your Doxygen DocStrings and refrain from complicated markup in them, it is not that hard to write a small parser that extracts them and substitutes them back into the Python DocStrings.
Here is a small example. It won't be powerful enough to handle any realistic use-case but I believe that extending it would not be hard and worth the effort unless you only have a hand-full of functions to document.
Place a special comment before each Doxygen DocString that gives the following comment block a name. Here, I'm using the syntax
to associate the name
sum
with the following DocString.Then, place another special string inside the Python bindings that references that name. I'm using the following syntax here.
Now we need a tool to extract the Doxygen DocStrings and substitutes them into the Python bindings.
As I've said, this example is contrived but it should show the idea and demonstrate that it is not too hard to do.
Running this script over the source file produces the following output.
Add two hours of polishing to the script and you're good to go.
Since this is likely to be of interest to other people as well, I wouldn't be surprised if somebody had already written such a script. And if not, publishing yours as free software would certainly be welcomed by others.