|
From: <mi...@us...> - 2022-01-29 10:48:27
|
Revision: 8991
http://sourceforge.net/p/docutils/code/8991
Author: milde
Date: 2022-01-29 10:48:24 +0000 (Sat, 29 Jan 2022)
Log Message:
-----------
Add `reporter` to `parsers.rst.Directive` class attributes.
Allows `Directive` subclass methods to call "reporter" methods as
`self.reporter.error()` etc. instead of the lengthy
`self.statemachine.reporter.error()`.
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docutils/parsers/rst/__init__.py
trunk/docutils/docutils/parsers/rst/directives/images.py
trunk/docutils/docutils/parsers/rst/directives/misc.py
trunk/docutils/docutils/parsers/rst/directives/tables.py
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2022-01-29 10:48:14 UTC (rev 8990)
+++ trunk/docutils/HISTORY.txt 2022-01-29 10:48:24 UTC (rev 8991)
@@ -39,6 +39,7 @@
* docutils/parsers/rst/__init__.py
- use "https:" scheme in PEP and RFC base link defaults.
+ - Add `reporter` to `Directive` class attributes.
* docutils/parsers/rst/directives/__init__.py
Modified: trunk/docutils/docutils/parsers/rst/__init__.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/__init__.py 2022-01-29 10:48:14 UTC (rev 8990)
+++ trunk/docutils/docutils/parsers/rst/__init__.py 2022-01-29 10:48:24 UTC (rev 8991)
@@ -272,6 +272,8 @@
- ``state_machine`` is the state machine which controls the state which called
the directive function.
+
+ - ``reporter`` is the state machine's `reporter` instance.
Directive functions return a list of nodes which will be inserted
into the document tree at the point where the directive was
@@ -286,7 +288,7 @@
substitution definition context, typically using code like this::
if not isinstance(state, states.SubstitutionDef):
- error = state_machine.reporter.error(
+ error = self.reporter.error(
'Invalid context: the "%s" directive can only be used '
'within a substitution definition.' % (name),
nodes.literal_block(block_text, block_text), line=lineno)
@@ -323,6 +325,7 @@
self.block_text = block_text
self.state = state
self.state_machine = state_machine
+ self.reporter = state_machine.reporter
def run(self):
raise NotImplementedError('Must override run() in subclass.')
Modified: trunk/docutils/docutils/parsers/rst/directives/images.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/images.py 2022-01-29 10:48:14 UTC (rev 8990)
+++ trunk/docutils/docutils/parsers/rst/directives/images.py 2022-01-29 10:48:24 UTC (rev 8991)
@@ -153,7 +153,7 @@
figure_node += caption
elif not (isinstance(first_node, nodes.comment)
and len(first_node) == 0):
- error = self.state_machine.reporter.error(
+ error = self.reporter.error(
'Figure caption must be a paragraph or empty comment.',
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
Modified: trunk/docutils/docutils/parsers/rst/directives/misc.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/misc.py 2022-01-29 10:48:14 UTC (rev 8990)
+++ trunk/docutils/docutils/parsers/rst/directives/misc.py 2022-01-29 10:48:24 UTC (rev 8991)
@@ -318,7 +318,7 @@
messages.append(elem)
else:
return [
- self.state_machine.reporter.error(
+ self.reporter.error(
'Error in "%s" directive: may contain a single paragraph '
'only.' % self.name, line=self.lineno)]
if node:
@@ -449,7 +449,7 @@
self.content[1:], self.content_offset, converted_role,
option_presets={}))
except states.MarkupError as detail:
- error = self.state_machine.reporter.error(
+ error = self.reporter.error(
'Error in "%s" directive:\n%s.' % (self.name, detail),
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
@@ -458,10 +458,10 @@
try:
options['class'] = directives.class_option(new_role_name)
except ValueError as detail:
- error = self.state_machine.reporter.error(
- 'Invalid argument for "%s" directive:\n%s.'
- % (self.name, detail), nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ error = self.reporter.error('Invalid argument '
+ 'for "%s" directive:\n%s.' % (self.name, detail),
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
return messages + [error]
role = roles.CustomRole(new_role_name, base_role, options, content)
roles.register_local_role(new_role_name, role)
@@ -561,7 +561,7 @@
state_machine_kwargs=self.SMkwargs)
if (new_line_offset - self.content_offset) != len(self.content):
# incomplete parse of block?
- error = self.state_machine.reporter.error(
+ error = self.reporter.error(
'Invalid meta directive.',
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
@@ -615,12 +615,12 @@
def run(self):
if self.content:
text = '\n'.join(self.content)
- info = self.state_machine.reporter.info(
+ info = self.reporter.info(
'Directive processed. Type="%s", arguments=%r, options=%r, '
'content:' % (self.name, self.arguments, self.options),
nodes.literal_block(text, text), line=self.lineno)
else:
- info = self.state_machine.reporter.info(
+ info = self.reporter.info(
'Directive processed. Type="%s", arguments=%r, options=%r, '
'content: None' % (self.name, self.arguments, self.options),
line=self.lineno)
@@ -633,12 +633,12 @@
# """This directive is useful only for testing purposes."""
# if content:
# text = '\n'.join(content)
-# info = state_machine.reporter.info(
+# info = reporter.info(
# 'Directive processed. Type="%s", arguments=%r, options=%r, '
# 'content:' % (name, arguments, options),
# nodes.literal_block(text, text), line=lineno)
# else:
-# info = state_machine.reporter.info(
+# info = reporter.info(
# 'Directive processed. Type="%s", arguments=%r, options=%r, '
# 'content: None' % (name, arguments, options), line=lineno)
# return [info]
Modified: trunk/docutils/docutils/parsers/rst/directives/tables.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/tables.py 2022-01-29 10:48:14 UTC (rev 8990)
+++ trunk/docutils/docutils/parsers/rst/directives/tables.py 2022-01-29 10:48:24 UTC (rev 8991)
@@ -65,33 +65,33 @@
def check_table_dimensions(self, rows, header_rows, stub_columns):
if len(rows) < header_rows:
- error = self.state_machine.reporter.error(
- '%s header row(s) specified but only %s row(s) of data '
- 'supplied ("%s" directive).'
- % (header_rows, len(rows), self.name), nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ error = self.reporter.error('%s header row(s) specified but '
+ 'only %s row(s) of data supplied ("%s" directive).'
+ % (header_rows, len(rows), self.name),
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
raise SystemMessagePropagation(error)
if len(rows) == header_rows > 0:
- error = self.state_machine.reporter.error(
- 'Insufficient data supplied (%s row(s)); no data remaining '
- 'for table body, required by "%s" directive.'
- % (len(rows), self.name), nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ error = self.reporter.error('Insufficient data supplied (%s row(s)); '
+ 'no data remaining for table body, required by "%s" directive.'
+ % (len(rows), self.name),
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
raise SystemMessagePropagation(error)
for row in rows:
if len(row) < stub_columns:
- error = self.state_machine.reporter.error(
- '%s stub column(s) specified but only %s columns(s) of '
- 'data supplied ("%s" directive).' %
- (stub_columns, len(row), self.name), nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ error = self.reporter.error('%s stub column(s) specified but '
+ 'only %s columns(s) of data supplied ("%s" directive).'
+ % (stub_columns, len(row), self.name),
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
raise SystemMessagePropagation(error)
if len(row) == stub_columns > 0:
- error = self.state_machine.reporter.error(
- 'Insufficient data supplied (%s columns(s)); no data remaining '
- 'for table body, required by "%s" directive.'
- % (len(row), self.name), nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ error = self.reporter.error('Insufficient data supplied '
+ '(%s columns(s)); no data remaining for table body, required '
+ 'by "%s" directive.' % (len(row), self.name),
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
raise SystemMessagePropagation(error)
def set_table_width(self, table_node):
@@ -106,18 +106,18 @@
if isinstance(self.widths, list):
if len(self.widths) != n_cols:
# TODO: use last value for missing columns?
- error = self.state_machine.reporter.error(
- '"%s" widths do not match the number of columns in table '
- '(%s).' % (self.name, n_cols), nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ error = self.reporter.error('"%s" widths do not match the '
+ 'number of columns in table (%s).' % (self.name, n_cols),
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
raise SystemMessagePropagation(error)
col_widths = self.widths
elif n_cols:
col_widths = [100 // n_cols] * n_cols
else:
- error = self.state_machine.reporter.error(
- 'No table data detected in CSV file.', nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ error = self.reporter.error('No table data detected in CSV file.',
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
raise SystemMessagePropagation(error)
return col_widths
@@ -132,19 +132,19 @@
def run(self):
if not self.content:
- warning = self.state_machine.reporter.warning(
- 'Content block expected for the "%s" directive; none found.'
- % self.name, nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ warning = self.reporter.warning('Content block expected '
+ 'for the "%s" directive; none found.' % self.name,
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
return [warning]
title, messages = self.make_title()
node = nodes.Element() # anonymous container for parsing
self.state.nested_parse(self.content, self.content_offset, node)
if len(node) != 1 or not isinstance(node[0], nodes.table):
- error = self.state_machine.reporter.error(
- 'Error parsing content block for the "%s" directive: exactly '
- 'one table expected.' % self.name, nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ error = self.reporter.error('Error parsing content block for the '
+ '"%s" directive: exactly one table expected.' % self.name,
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
return [error]
table_node = node[0]
table_node['classes'] += self.options.get('class', [])
@@ -240,10 +240,10 @@
if (not self.state.document.settings.file_insertion_enabled
and ('file' in self.options
or 'url' in self.options)):
- warning = self.state_machine.reporter.warning(
- 'File and URL access deactivated; ignoring "%s" '
- 'directive.' % self.name, nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ warning = self.reporter.warning('File and URL access '
+ 'deactivated; ignoring "%s" directive.' % self.name,
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
return [warning]
self.check_requirements()
title, messages = self.make_title()
@@ -264,10 +264,10 @@
return [detail.args[0]]
except csv.Error as detail:
message = str(detail)
- error = self.state_machine.reporter.error(
- 'Error with CSV data in "%s" directive:\n%s'
- % (self.name, message), nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ error = self.reporter.error('Error with CSV data'
+ ' in "%s" directive:\n%s' % (self.name, message),
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
return [error]
table = (col_widths, table_head, table_body)
table_node = self.state.build_table(table, self.content_offset,
@@ -292,10 +292,10 @@
if self.content:
# CSV data is from directive content.
if 'file' in self.options or 'url' in self.options:
- error = self.state_machine.reporter.error(
- '"%s" directive may not both specify an external file and'
- ' have content.' % self.name, nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ error = self.reporter.error('"%s" directive may not both '
+ 'specify an external file and have content.' % self.name,
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
raise SystemMessagePropagation(error)
source = self.content.source(0)
csv_data = self.content
@@ -302,11 +302,11 @@
elif 'file' in self.options:
# CSV data is from an external file.
if 'url' in self.options:
- error = self.state_machine.reporter.error(
- 'The "file" and "url" options may not be simultaneously'
- ' specified for the "%s" directive.' % self.name,
- nodes.literal_block(self.block_text, self.block_text),
- line=self.lineno)
+ error = self.reporter.error('The "file" and "url" options '
+ 'may not be simultaneously specified '
+ 'for the "%s" directive.' % self.name,
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
raise SystemMessagePropagation(error)
source_dir = os.path.dirname(
os.path.abspath(self.state.document.current_source))
@@ -319,7 +319,7 @@
error_handler=error_handler)
csv_data = csv_file.read().splitlines()
except OSError as error:
- severe = self.state_machine.reporter.severe(
+ severe = self.reporter.severe(
'Problems with "%s" directive path:\n%s.'
% (self.name, error),
nodes.literal_block(self.block_text, self.block_text),
@@ -339,7 +339,7 @@
try:
csv_text = urlopen(source).read()
except (URLError, OSError, ValueError) as error:
- severe = self.state_machine.reporter.severe(
+ severe = self.reporter.severe(
'Problems with "%s" directive URL "%s":\n%s.'
% (self.name, self.options['url'], error),
nodes.literal_block(self.block_text, self.block_text),
@@ -351,10 +351,11 @@
input_encoding_error_handler))
csv_data = csv_file.read().splitlines()
else:
- error = self.state_machine.reporter.warning(
+ error = self.reporter.warning(
'The "%s" directive requires content; none supplied.'
- % self.name, nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ % self.name,
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
raise SystemMessagePropagation(error)
return csv_data, source
@@ -409,8 +410,8 @@
def run(self):
if not self.content:
- error = self.state_machine.reporter.error(
- 'The "%s" directive is empty; content required.' % self.name,
+ error = self.reporter.error('The "%s" directive is empty; '
+ 'content required.' % self.name,
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
return [error]
@@ -439,7 +440,7 @@
def check_list_content(self, node):
if len(node) != 1 or not isinstance(node[0], nodes.bullet_list):
- error = self.state_machine.reporter.error(
+ error = self.reporter.error(
'Error parsing content block for the "%s" directive: '
'exactly one bullet list expected.' % self.name,
nodes.literal_block(self.block_text, self.block_text),
@@ -451,16 +452,17 @@
for item_index in range(len(list_node)):
item = list_node[item_index]
if len(item) != 1 or not isinstance(item[0], nodes.bullet_list):
- error = self.state_machine.reporter.error(
+ error = self.reporter.error(
'Error parsing content block for the "%s" directive: '
'two-level bullet list expected, but row %s does not '
'contain a second-level bullet list.'
- % (self.name, item_index + 1), nodes.literal_block(
- self.block_text, self.block_text), line=self.lineno)
+ % (self.name, item_index + 1),
+ nodes.literal_block(self.block_text, self.block_text),
+ line=self.lineno)
raise SystemMessagePropagation(error)
elif item_index:
if len(item[0]) != num_cols:
- error = self.state_machine.reporter.error(
+ error = self.reporter.error(
'Error parsing content block for the "%s" directive: '
'uniform two-level bullet list expected, but row %s '
'does not contain the same number of items as row 1 '
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|