Debian Codegraph @

docutils/readers/__init__.py

raw symbols SCIP
1 # $Id: __init__.py 10077 2025-04-09 08:55:54Z milde $
2 # Authors: David Goodger <[email protected]>; Ueli Schlaepfer
3 # Copyright: This module has been placed in the public domain.
4
5 """
6 This package contains Docutils Reader modules.
7 """
8
9 from __future__ import annotations
10
11 __docformat__ = 'reStructuredText'
12
13 import importlib
14 import warnings
15
16 from docutils import utils, parsers, Component
17 from docutils.transforms import universal
18
19 TYPE_CHECKING = False
20 if TYPE_CHECKING:
21 from typing import Final
22
23 from docutils import nodes
24 from docutils.io import Input
25 from docutils.parsers import Parser
26 from docutils.transforms import Transform
27
28
29 class Reader(Component):
30
31 """
32 Abstract base class for docutils Readers.
33
34 Each reader module or package must export a subclass also called 'Reader'.
35
36 The two steps of a Reader's responsibility are to read data from the
37 source Input object and parse the data with the Parser object.
38 Call `read()` to process a document.
39 """
40
41 component_type: Final = 'reader'
42 config_section: Final = 'readers'
43
44 def get_transforms(self) -> list[type[Transform]]:
45 return super().get_transforms() + [universal.Decorations,
46 universal.ExposeInternals,
47 universal.StripComments]
48
49 def __init__(self,
50 parser: Parser | str | None = None,
51 parser_name: str | None = None
52 ) -> None:
53 """
54 Initialize the Reader instance.
55
56 :parser: A parser instance or name (an instance will be created).
57 :parser_name: deprecated, use "parser".
58
59 Several instance attributes are defined with dummy initial values.
60 Subclasses may use these attributes as they wish.
61 """
62
63 self.parser: Parser | None = parser
64 """A `parsers.Parser` instance shared by all doctrees. May be left
65 unspecified if the document source determines the parser."""
66
67 if isinstance(parser, str):
68 self.set_parser(parser)
69 if parser_name is not None:
70 warnings.warn('Argument "parser_name" will be removed '
71 'in Docutils 2.0.\n'
72 ' Specify parser name in the "parser" argument.',
73 PendingDeprecationWarning, stacklevel=2)
74 if self.parser is None:
75 self.set_parser(parser_name)
76
77 self.source: Input | None = None
78 """`docutils.io` IO object, source of input data."""
79
80 self.input: str | None = None
81 """Raw text input; either a single string or, for more complex cases,
82 a collection of strings."""
83
84 def set_parser(self, parser_name: str) -> None:
85 """Set `self.parser` by name."""
86 parser_class = parsers.get_parser_class(parser_name)
87 self.parser = parser_class()
88
89 def read(self, source, parser, settings):
90 self.source = source
91 if not self.parser:
92 self.parser = parser
93 self.settings = settings
94 self.input = self.source.read()
95 self.parse()
96 return self.document
97
98 def parse(self) -> None:
99 """Parse `self.input` into a document tree."""
100 document = self.new_document()
101 self.parser.parse(self.input, document)
102 document.current_source = document.current_line = None
103 self.document: nodes.document = document
104
105 def new_document(self) -> nodes.document:
106 """Create and return a new empty document tree (root node)."""
107 return utils.new_document(self.source.source_path, self.settings)
108
109
110 class ReReader(Reader):
111
112 """
113 A reader which rereads an existing document tree (e.g. a
114 deserializer).
115
116 Often used in conjunction with `writers.UnfilteredWriter`.
117 """
118
119 def get_transforms(self) -> list[type[Transform]]:
120 # Do not add any transforms. They have already been applied
121 # by the reader which originally created the document.
122 return Component.get_transforms(self)
123
124
125 def get_reader_class(reader_name: str) -> type[Reader]:
126 """Return the Reader class from the `reader_name` module."""
127 name = reader_name.lower()
128 try:
129 module = importlib.import_module('docutils.readers.'+name)
130 except ImportError:
131 try:
132 module = importlib.import_module(name)
133 except ImportError as err:
134 raise ImportError(f'Reader "{reader_name}" not found.') from err
135 return module.Reader