Charles Hooper
Charles Hooper

Reputation: 2779

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. 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']

Upvotes: 8

Views: 1434

Answers (2)

Carson
Carson

Reputation: 8018

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

Upvotes: 3

user221014
user221014

Reputation:

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

Upvotes: 12

Related Questions