如何抑制或禁用 reSTructuredText 中的警告?

发布于 2024-09-30 14:44:26 字数 720 浏览 6 评论 0原文

我正在用Python开发一个CMS,它使用reStructuredText(通过docutils)来格式化内容。我的很多内容都是从其他来源导入的,通常以未格式化的文本文档的形式出现。 reST 对此非常有用,因为默认情况下它使一切看起来都很正常。

然而,我遇到的一个问题是,我收到的警告转储到我的网络服务器上的 stderr注入到我的页面内容中。例如,我在网页上收到如下警告:

系统消息:WARNING/2(第 296 行);反向链接

是:如何抑制、禁用或以其他方式重定向这些警告?

理想情况下,我希望将这些警告写入日志文件,但是如果有人可以告诉我如何关闭警告注入到我的内容中,那就完美了。

负责将 reST 解析为 HTML 的代码:

from docutils import core
import reSTpygments

def reST2HTML( str ):
    parts = core.publish_parts(
                          source = str,
                          writer_name = 'html')
    return parts['body_pre_docinfo'] + parts['fragment']

I'm working on a CMS in Python that uses reStructuredText (via docutils) to format content. Alot of my content is imported from other sources and usually comes in the form of unformatted text documents. reST works great for this because it makes everything look pretty sane by default.

One problem I am having, however, is that I get warnings dumped to stderr on my webserver and injected into my page content. For example, I get warnings like the following on my web page:

System Message: WARNING/2 (, line 296); backlink

My question is: How do I suppress, disable, or otherwise re-direct these warnings?

Ideally, I'd love to write these out to a log file, but if someone can just tell me how to turn off the warnings from being injected into my content then that would be perfect.

The code that's responsible for parsing the reST into HTML:

from docutils import core
import reSTpygments

def reST2HTML( str ):
    parts = core.publish_parts(
                          source = str,
                          writer_name = 'html')
    return parts['body_pre_docinfo'] + parts['fragment']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

圈圈圆圆圈圈 2024-10-07 14:44:26
def reST2HTML( str ):
    parts = core.publish_parts(
    source = str,
    writer_name = 'html',
    settings_overrides={'report_level':'quiet'},
    )
    return parts['body_pre_docinfo'] + parts['fragment']
def reST2HTML( str ):
    parts = core.publish_parts(
    source = str,
    writer_name = 'html',
    settings_overrides={'report_level':'quiet'},
    )
    return parts['body_pre_docinfo'] + parts['fragment']
过期以后 2024-10-07 14:44:26

看来 report_level 接受字符串是旧版本。现在,以下内容对我有用。

import docutils.core
import docutils.utils
from pathlib import Path

shut_up_level = docutils.utils.Reporter.SEVERE_LEVEL + 1
docutils.core.publish_file(
    source_path=Path(...), destination_path=Path(...),
    settings_overrides={'report_level': shut_up_level},
    writer_name='html')

关于level

# docutils.utils.__init__.py
class Reporter(object):
    # system message level constants:
    (DEBUG_LEVEL,
     INFO_LEVEL,
     WARNING_LEVEL,
     ERROR_LEVEL,
     SEVERE_LEVEL) = range(5)

    ...

    def system_message(self, level, message, *children, **kwargs):
        ...
        if self.stream and (level >= self.report_level  # self.report_level was set by you. (for example, shut_up_level)
                    or self.debug_flag and level == self.DEBUG_LEVEL
                    or level >= self.halt_level):
            self.stream.write(msg.astext() + '\n')
        ...
        return msg

根据上面的代码,你知道可以分配self.report_level(即settings_overrides={'report_level': ...})让警告不展示。

我将其设置为SERVER_LEVEL+1,因此它不会显示任何错误。 (您可以根据您的需求进行设置。

It seems the report_level accept string is an old version. Now, the below is work for me.

import docutils.core
import docutils.utils
from pathlib import Path

shut_up_level = docutils.utils.Reporter.SEVERE_LEVEL + 1
docutils.core.publish_file(
    source_path=Path(...), destination_path=Path(...),
    settings_overrides={'report_level': shut_up_level},
    writer_name='html')

about level

# docutils.utils.__init__.py
class Reporter(object):
    # system message level constants:
    (DEBUG_LEVEL,
     INFO_LEVEL,
     WARNING_LEVEL,
     ERROR_LEVEL,
     SEVERE_LEVEL) = range(5)

    ...

    def system_message(self, level, message, *children, **kwargs):
        ...
        if self.stream and (level >= self.report_level  # self.report_level was set by you. (for example, shut_up_level)
                    or self.debug_flag and level == self.DEBUG_LEVEL
                    or level >= self.halt_level):
            self.stream.write(msg.astext() + '\n')
        ...
        return msg

According to the above code, you know that you can assign the self.report_level (i.e. settings_overrides={'report_level': ...}) let the warning not show.

and I set it to SERVER_LEVEL+1, so it will not show any error. (you can set it according to your demand.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文