How Do I Suppress or Disable Warnings in reSTructuredText?
Asked Answered
C

2

8

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']
Chaumont answered 31/10, 2010 at 3:48 Comment(0)
F
12
def reST2HTML( str ):
    parts = core.publish_parts(
    source = str,
    writer_name = 'html',
    settings_overrides={'report_level':'quiet'},
    )
    return parts['body_pre_docinfo'] + parts['fragment']
Fickle answered 31/10, 2010 at 4:0 Comment(0)
M
3

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.)

Meng answered 24/9, 2020 at 8:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.