|
From: <mi...@us...> - 2023-07-07 06:50:29
|
Revision: 9428
http://sourceforge.net/p/docutils/code/9428
Author: milde
Date: 2023-07-07 06:50:26 +0000 (Fri, 07 Jul 2023)
Log Message:
-----------
Small code cleanup.
* Sort import statements.
* Do not overwrite standard `io` with `docutils.io`.
* Use auxiliary variable to avoid too long lines and
multiple dereferencing of ``self.state.document.settings``.
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docutils/parsers/rst/directives/misc.py
trunk/docutils/docutils/parsers/rst/directives/tables.py
trunk/docutils/docutils/parsers/rst/languages/de.py
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2023-07-07 06:50:09 UTC (rev 9427)
+++ trunk/docutils/HISTORY.txt 2023-07-07 06:50:26 UTC (rev 9428)
@@ -30,6 +30,8 @@
configuration setting is None, '', 'utf-8-sig', 'utf-16', or 'utf-32'.
Do not remove other ZWNBSPs.
+ - Auto-close `FileInput.source` in case of reading/decoding errors.
+
.. _UTF-8 mode: https://docs.python.org/3/library/os.html#utf8-mode
.. _input encoding: docs/api/publisher.html#encodings
@@ -65,6 +67,8 @@
- Use context manager for image reading operations.
Catch `URLError` when `urllib.request.urlopen()` fails.
+ - Convert image URI to path if accessing a local file. Fixes bug #153.
+
* docutils/writers/s5_html/__init__.py
- Warn if the S5 writer cannot copy the theme files.
Modified: trunk/docutils/docutils/parsers/rst/directives/misc.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/misc.py 2023-07-07 06:50:09 UTC (rev 9427)
+++ trunk/docutils/docutils/parsers/rst/directives/misc.py 2023-07-07 06:50:26 UTC (rev 9428)
@@ -58,8 +58,10 @@
Depending on the options, the file (or a clipping) is
converted to nodes and returned or inserted into the input stream.
"""
- if not self.state.document.settings.file_insertion_enabled:
+ settings = self.state.document.settings
+ if not settings.file_insertion_enabled:
raise self.warning('"%s" directive disabled.' % self.name)
+ tab_width = self.options.get('tab-width', settings.tab_width)
current_source = self.state.document.current_source
path = directives.path(self.arguments[0])
if path.startswith('<') and path.endswith('>'):
@@ -68,15 +70,12 @@
else:
_base = Path(current_source).parent
path = utils.relative_path(None, _base/path)
- encoding = self.options.get(
- 'encoding', self.state.document.settings.input_encoding)
- e_handler = self.state.document.settings.input_encoding_error_handler
- tab_width = self.options.get(
- 'tab-width', self.state.document.settings.tab_width)
+ encoding = self.options.get('encoding', settings.input_encoding)
+ error_handler = settings.input_encoding_error_handler
try:
include_file = io.FileInput(source_path=path,
encoding=encoding,
- error_handler=e_handler)
+ error_handler=error_handler)
except UnicodeEncodeError:
raise self.severe(f'Problems with "{self.name}" directive path:\n'
f'Cannot encode input file path "{path}" '
@@ -85,7 +84,7 @@
raise self.severe(f'Problems with "{self.name}" directive '
f'path:\n{io.error_string(error)}.')
else:
- self.state.document.settings.record_dependencies.add(path)
+ settings.record_dependencies.add(path)
# Get to-be-included content
startline = self.options.get('start-line', None)
@@ -121,7 +120,7 @@
include_lines = statemachine.string2lines(rawtext, tab_width,
convert_whitespace=True)
for i, line in enumerate(include_lines):
- if len(line) > self.state.document.settings.line_length_limit:
+ if len(line) > settings.line_length_limit:
raise self.warning('"%s": line %d exceeds the'
' line-length-limit.' % (path, i+1))
@@ -187,7 +186,7 @@
if 'parser' in self.options:
# parse into a dummy document and return created nodes
- document = utils.new_document(path, self.state.document.settings)
+ document = utils.new_document(path, settings)
document.include_log = include_log + [(path, clip_options)]
parser = self.options['parser']()
parser.parse('\n'.join(include_lines), document)
@@ -227,15 +226,14 @@
has_content = True
def run(self):
- if (not self.state.document.settings.raw_enabled
- or (not self.state.document.settings.file_insertion_enabled
- and ('file' in self.options
- or 'url' in self.options))):
+ settings = self.state.document.settings
+ if (not settings.raw_enabled
+ or (not settings.file_insertion_enabled
+ and ('file' in self.options or 'url' in self.options))):
raise self.warning('"%s" directive disabled.' % self.name)
attributes = {'format': ' '.join(self.arguments[0].lower().split())}
- encoding = self.options.get(
- 'encoding', self.state.document.settings.input_encoding)
- e_handler = self.state.document.settings.input_encoding_error_handler
+ encoding = self.options.get('encoding', settings.input_encoding)
+ error_handler = settings.input_encoding_error_handler
if self.content:
if 'file' in self.options or 'url' in self.options:
raise self.error(
@@ -253,7 +251,7 @@
try:
raw_file = io.FileInput(source_path=path,
encoding=encoding,
- error_handler=e_handler)
+ error_handler=error_handler)
except OSError as error:
raise self.severe(f'Problems with "{self.name}" directive '
f'path:\n{io.error_string(error)}.')
@@ -260,7 +258,7 @@
else:
# TODO: currently, raw input files are recorded as
# dependencies even if not used for the chosen output format.
- self.state.document.settings.record_dependencies.add(path)
+ settings.record_dependencies.add(path)
try:
text = raw_file.read()
except UnicodeError as error:
@@ -277,7 +275,7 @@
f'{io.error_string(error)}.')
raw_file = io.StringInput(source=raw_text, source_path=source,
encoding=encoding,
- error_handler=e_handler)
+ error_handler=error_handler)
try:
text = raw_file.read()
except UnicodeError as error:
Modified: trunk/docutils/docutils/parsers/rst/directives/tables.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/tables.py 2023-07-07 06:50:09 UTC (rev 9427)
+++ trunk/docutils/docutils/parsers/rst/directives/tables.py 2023-07-07 06:50:26 UTC (rev 9428)
@@ -11,14 +11,15 @@
import csv
from pathlib import Path
+from urllib.request import urlopen
+from urllib.error import URLError
import warnings
-from docutils import io, nodes, statemachine, utils
-from docutils.utils import SystemMessagePropagation
+from docutils import nodes, statemachine, utils
+from docutils.io import FileInput, StringInput
from docutils.parsers.rst import Directive
from docutils.parsers.rst import directives
-from urllib.request import urlopen
-from urllib.error import URLError
+from docutils.utils import SystemMessagePropagation
def align(argument):
@@ -321,9 +322,9 @@
Get CSV data from the directive content, from an external
file, or from a URL reference.
"""
- encoding = self.options.get(
- 'encoding', self.state.document.settings.input_encoding)
- error_handler = self.state.document.settings.input_encoding_error_handler # noqa:E501
+ settings = self.state.document.settings
+ encoding = self.options.get('encoding', settings.input_encoding)
+ error_handler = settings.input_encoding_error_handler
if self.content:
# CSV data is from directive content.
if 'file' in self.options or 'url' in self.options:
@@ -348,9 +349,9 @@
_base = Path(self.state.document.current_source).parent
source = utils.relative_path(None, _base/source)
try:
- csv_file = io.FileInput(source_path=source,
- encoding=encoding,
- error_handler=error_handler)
+ csv_file = FileInput(source_path=source,
+ encoding=encoding,
+ error_handler=error_handler)
csv_data = csv_file.read().splitlines()
except OSError as error:
severe = self.reporter.severe(
@@ -360,7 +361,7 @@
line=self.lineno)
raise SystemMessagePropagation(severe)
else:
- self.state.document.settings.record_dependencies.add(source)
+ settings.record_dependencies.add(source)
elif 'url' in self.options:
source = self.options['url']
try:
@@ -373,10 +374,9 @@
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
raise SystemMessagePropagation(severe)
- csv_file = io.StringInput(
- source=csv_text, source_path=source, encoding=encoding,
- error_handler=(self.state.document.settings.
- input_encoding_error_handler))
+ csv_file = StringInput(source=csv_text, source_path=source,
+ encoding=encoding,
+ error_handler=error_handler)
csv_data = csv_file.read().splitlines()
else:
error = self.reporter.warning(
Modified: trunk/docutils/docutils/parsers/rst/languages/de.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/languages/de.py 2023-07-07 06:50:09 UTC (rev 9427)
+++ trunk/docutils/docutils/parsers/rst/languages/de.py 2023-07-07 06:50:26 UTC (rev 9428)
@@ -17,6 +17,8 @@
directives = {
+ 'warnhinweis': 'admonition', # or, more generally, 'anmerkung'?
+ 'ermahnung': 'admonition', # sic! kept for backwards compatibiltity
'achtung': 'attention',
'vorsicht': 'caution',
'code': 'code',
@@ -27,8 +29,6 @@
'notiz': 'note',
'tipp': 'tip',
'warnung': 'warning',
- 'warnhinweis': 'admonition',
- 'ermahnung': 'admonition', # sic! kept for backwards compatibiltity
'kasten': 'sidebar',
'seitenkasten': 'sidebar', # kept for backwards compatibiltity
'seitenleiste': 'sidebar',
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|