如何从boost::python docstrings创建doxygen文档?

2024-05-20 22:23:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我为Pothon绑定创建了一个C++代码的大体,使用Boosi::Python。python绑定有如下文档:

BOOST_PYTHON_MODULE(mymodule)
{
    using namespace boost::python;
    def("foo1", foo1, arg("i"), "foo1 doc");
}

项目的其余部分用doxygen记录。我想知道是否有一种方法可以从python绑定的docstring生成doxygen文档。在

在我看来,我有两个选择:

  • 使用一个神奇的工具导入python文件并输出文档。Sphinx在一定程度上可以工作,因为它的autodoc工具实际上加载python模块并扫描docstring。然而,它并没有产生doxygen可以使用的输出格式(我想?)。在
  • 编写一个转换过程来导入BOOST_PYTHON_模块。呼叫帮助(mymodule)。解析输出以生成骨架python文件。像平常一样把它们喂给强生。在

有更好的方法吗?在


Tags: 模块文件工具方法代码文档docstringmodule
1条回答
网友
1楼 · 发布于 2024-05-20 22:23:36

this topic,可以执行以下操作:

1-编写doxygen文档:

// DocString: foo
/**
 * @brief Foo doc
 * @param i an integer
 * @return something
 *
 */
int foo(int i);

2-更新绑定文档:

^{pr2}$

3-使用如下脚本配置文件(在构建链中):

import re
import sys

def parse_doc_string(istr):
    pattern = re.compile(r'@(\w+)\s+(.*)')
    docstring = list()
    for line in map(lambda s : s.strip(), istr):
        if line == '/**':
            continue
        if line == '*/':
            return docstring
        line = line.lstrip('* ')
        match = pattern.match(line)
        if match:
            docstring.append((match.group(1), match.group(2)))

def extract(istr, docstrings):
    pattern = re.compile(r'^//\s*DocString:\s*(\w+)$')
    for line in map(lambda s : s.strip(), istr):
        match = pattern.match(line)
        if match:
            token = match.group(1)
            docstrings[token] = parse_doc_string(istr)

def format_doc_string(docstring):
    return '\n'.join('{}: {}'.format(k, v) for (k, v) in docstring)

def escape(string):
    return string.replace('\n', r'\n')

def substitute(istr, ostr, docstrings):
    pattern = re.compile(r'@DocString\((\w+)\)')
    for line in map(lambda s : s.rstrip(), istr):
        for match in pattern.finditer(line):
            token = match.group(1)
            docstring = format_doc_string(docstrings[token])
            line = line.replace(match.group(0), escape(docstring))
        print(line, file=ostr)

if __name__ == '__main__':
    sourcefile = sys.argv[1]
    docstrings = dict()
    with open(sourcefile) as istr:
        extract(istr, docstrings)
    with open(sourcefile) as istr:
        with sys.stdout as ostr:
            substitute(istr, ostr, docstrings)

它将替换您的绑定:

def("foo1", foo1, arg("i"), "brief: Foo doc\nparam: i an integer\nreturn: something");

相关问题 更多 >