简体   繁体   English

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

[英]How Do I Suppress or Disable Warnings in reSTructuredText?

I'm working on a CMS in Python that uses reStructuredText (via docutils) to format content.我正在用 Python 开发一个 CMS,它使用 reStructuredText(通过 docutils)来格式化内容。 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. reST 对此非常有用,因为默认情况下它使一切看起来都非常理智。

One problem I am having, however, is that I get warnings dumped to stderr on my webserver and injected into my page content .然而,我遇到的一个问题是,我将警告转储到我的网络服务器上的 stderr注入到我的页面内容中 For example, I get warnings like the following on my web page:例如,我在我的网页上收到如下警告:

System Message: WARNING/2 (, line 296);系统消息:警告/2(,第 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:负责将 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']
def reST2HTML( str ):
    parts = core.publish_parts(
    source = str,
    writer_name = 'html',
    settings_overrides={'report_level':'quiet'},
    )
    return parts['body_pre_docinfo'] + parts['fragment']

It seems the report_level accept string is an old version.似乎report_level接受字符串是旧版本。 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 (ie settings_overrides={'report_level': ...} ) let the warning not show.根据上面的代码,你知道你可以分配self.report_level (即settings_overrides={'report_level': ...} )让警告不显示。

and I set it to SERVER_LEVEL+1 , so it will not show any error.我将它设置为SERVER_LEVEL+1 ,所以它不会显示任何错误。 ( you can set it according to your demand. ) 您可以根据您的需求进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM