From 23d836574b3c580c799b1fb1cec73713766d8639 Mon Sep 17 00:00:00 2001 From: milde Date: Fri, 7 Jul 2023 06:50:26 +0000 Subject: [PATCH] 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``. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@9428 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/HISTORY.txt | 4 +++ docutils/docutils/parsers/rst/directives/misc.py | 38 ++++++++++------------ docutils/docutils/parsers/rst/directives/tables.py | 30 ++++++++--------- docutils/docutils/parsers/rst/languages/de.py | 4 +-- 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt index 42863986d..1011da5e3 100644 --- a/docutils/HISTORY.txt +++ b/docutils/HISTORY.txt @@ -30,6 +30,8 @@ Changes since 0.20.1 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 @@ Changes since 0.20.1 - 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. diff --git a/docutils/docutils/parsers/rst/directives/misc.py b/docutils/docutils/parsers/rst/directives/misc.py index 10619dac5..85032d8e2 100644 --- a/docutils/docutils/parsers/rst/directives/misc.py +++ b/docutils/docutils/parsers/rst/directives/misc.py @@ -58,8 +58,10 @@ class Include(Directive): 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 @@ class Include(Directive): 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 @@ class Include(Directive): 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 @@ class Include(Directive): 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 @@ class Include(Directive): 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 @@ class Raw(Directive): 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,14 +251,14 @@ class Raw(Directive): 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)}.') 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 @@ class Raw(Directive): 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: diff --git a/docutils/docutils/parsers/rst/directives/tables.py b/docutils/docutils/parsers/rst/directives/tables.py index ce8acb82f..0e974a2bd 100644 --- a/docutils/docutils/parsers/rst/directives/tables.py +++ b/docutils/docutils/parsers/rst/directives/tables.py @@ -11,14 +11,15 @@ __docformat__ = 'reStructuredText' 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 @@ class CSVTable(Table): 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 @@ class CSVTable(Table): _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 @@ class CSVTable(Table): 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 @@ class CSVTable(Table): 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( diff --git a/docutils/docutils/parsers/rst/languages/de.py b/docutils/docutils/parsers/rst/languages/de.py index 621986bda..6b12f7528 100644 --- a/docutils/docutils/parsers/rst/languages/de.py +++ b/docutils/docutils/parsers/rst/languages/de.py @@ -17,6 +17,8 @@ __docformat__ = 'reStructuredText' directives = { + 'warnhinweis': 'admonition', # or, more generally, 'anmerkung'? + 'ermahnung': 'admonition', # sic! kept for backwards compatibiltity 'achtung': 'attention', 'vorsicht': 'caution', 'code': 'code', @@ -27,8 +29,6 @@ directives = { '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', -- 2.11.4.GIT