如何从Shinx编译中获取警告列表 [英] How to get a list of warnings from sphinx compilation

查看:19
本文介绍了如何从Shinx编译中获取警告列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于狮身人面像的协作写作工具。用户可以访问Web应用程序(用Python/Flask语言开发)来用狮身人面像编写一本书,并将其编译成pdf。

我已经了解到,为了从python中编译一个sphinx文档,我应该使用

import sphinx
result = sphinx.build_main(['-c', 'path/to/conf',
                            'path/to/source/', 'path/to/out'])

到目前为止一切顺利。

现在我的用户希望应用程序向他们显示他们的语法错误。但输出(上例中的result)只给出了退出代码。

那么,如何从生成过程中获取警告列表?

也许我的野心太大了,但由于Shinx是一个python工具,我希望该工具有一个很好的pythonic接口。例如,sphinx.build_main的输出可能是一个非常丰富的对象,带有警告、行号...

注意,sphinx.build_main方法的参数看起来就像命令行界面的包装。

推荐答案

sphinx.build_main()调用sphinx.cmdline.main(),进而创建一个sphinx.application.Sphinx对象。您可以直接创建这样的对象(而不是"在python中进行系统调用")。使用类似以下内容:

import os
from sphinx.application import Sphinx

# Main arguments 
srcdir = "/path/to/source"
confdir = srcdir
builddir = os.path.join(srcdir, "_build")
doctreedir = os.path.join(builddir, "doctrees")
builder = "html"

# Write warning messages to a file (instead of stderr)
warning = open("/path/to/warnings.txt", "w")

# Create the Sphinx application object
app = Sphinx(srcdir, confdir, builddir, doctreedir, builder, 
             warning=warning)

# Run the build
app.build()

这篇关于如何从Shinx编译中获取警告列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆