22
33Implements the Distutils 'check' command.
44"""
5+ import contextlib
6+
57from distutils .core import Command
68from distutils .errors import DistutilsSetupError
79
8- try :
9- # docutils is installed
10- from docutils .utils import Reporter
11- from docutils .parsers .rst import Parser
12- from docutils import frontend
13- from docutils import nodes
10+ with contextlib .suppress (ImportError ):
11+ import docutils .utils
12+ import docutils .parsers .rst
13+ import docutils .frontend
14+ import docutils .nodes
1415
15- class SilentReporter (Reporter ):
16+ class SilentReporter (docutils . utils . Reporter ):
1617 def __init__ (
1718 self ,
1819 source ,
@@ -30,16 +31,10 @@ def __init__(
3031
3132 def system_message (self , level , message , * children , ** kwargs ):
3233 self .messages .append ((level , message , children , kwargs ))
33- return nodes .system_message (
34+ return docutils . nodes .system_message (
3435 message , level = level , type = self .levels [level ], * children , ** kwargs
3536 )
3637
37- HAS_DOCUTILS = True
38- except Exception :
39- # Catch all exceptions because exceptions besides ImportError probably
40- # indicate that docutils is not ported to Py3k.
41- HAS_DOCUTILS = False
42-
4338
4439class check (Command ):
4540 """This command checks the meta-data of the package."""
@@ -81,8 +76,11 @@ def run(self):
8176 if self .metadata :
8277 self .check_metadata ()
8378 if self .restructuredtext :
84- if HAS_DOCUTILS :
85- self .check_restructuredtext ()
79+ if 'docutils' in globals ():
80+ try :
81+ self .check_restructuredtext ()
82+ except TypeError as exc :
83+ raise DistutilsSetupError (str (exc ))
8684 elif self .strict :
8785 raise DistutilsSetupError ('The docutils package is needed.' )
8886
@@ -124,8 +122,10 @@ def _check_rst_data(self, data):
124122 """Returns warnings when the provided data doesn't compile."""
125123 # the include and csv_table directives need this to be a path
126124 source_path = self .distribution .script_name or 'setup.py'
127- parser = Parser ()
128- settings = frontend .OptionParser (components = (Parser ,)).get_default_values ()
125+ parser = docutils .parsers .rst .Parser ()
126+ settings = docutils .frontend .OptionParser (
127+ components = (docutils .parsers .rst .Parser ,)
128+ ).get_default_values ()
129129 settings .tab_width = 4
130130 settings .pep_references = None
131131 settings .rfc_references = None
@@ -139,7 +139,7 @@ def _check_rst_data(self, data):
139139 error_handler = settings .error_encoding_error_handler ,
140140 )
141141
142- document = nodes .document (settings , reporter , source = source_path )
142+ document = docutils . nodes .document (settings , reporter , source = source_path )
143143 document .note_source (source_path , - 1 )
144144 try :
145145 parser .parse (data , document )
0 commit comments