Debian Codegraph @

docs/dev/runtime-settings-processing.rst

raw
1 .. include:: ../header.rst
2
3 =============================
4 Runtime Settings Processing
5 =============================
6
7 :Author: David Goodger, Günter Milde
8 :Contact: [email protected]
9 :Date: $Date: 2024-08-15 10:43:38 +0200 (Do, 15. Aug 2024) $
10 :Revision: $Revision: 9906 $
11 :Copyright: This document has been placed in the public domain.
12
13 :Abstract: A detailled description of Docutil's settings processing
14 framework.
15
16 .. contents::
17
18
19 The ``docutils/__init__.py``, ``docutils/core.py``, and
20 ``docutils.frontend.py`` modules are described.
21 Following along with the actual code is recommended.
22
23 See `Docutils Runtime Settings`_ for a high-level description of the core
24 API and `Docutils Configuration`_ for a description of the individual
25 settings.
26
27 .. note::
28 This document is informal.
29 It describes the state in Docutils 0.18.1.
30 Implementation details will change with the move to replace the
31 deprecated optparse_ module with argparse_.
32
33
34 Settings priority
35 =================
36
37 Docutils overlays default and explicitly specified values from various
38 sources such that settings behave the way we want and expect them to
39 behave.
40
41 The souces are overlaid in the following order (later sources
42 overwrite earlier ones):
43
44 1. Defaults specified in `settings_spec`__ and
45 `settings_defaults`__ attributes for each component_.
46 (details__)
47
48 __ ../api/runtime-settings.html#settingsspec-settings-spec
49 __ ../api/runtime-settings.html#settingsspec-settings-defaults
50 __ `OptionParser.populate_from_components()`_
51
52 2. Defaults specified in `settings_default_overrides`__ attribute for
53 each component_.
54 (details__)
55
56 __ ../api/runtime-settings.html#settingsspec-settings-default-overrides
57 __ component.settings_default_overrides_
58
59 3. Settings specified in the `"settings_overrides" argument`_ of the
60 `Publisher convenience functions`_ rsp. the `settings_overrides`
61 attribute of a `core.Publisher` instance.
62 (details__)
63
64 __ OptionParser.defaults_
65
66 4. If enabled, settings specified in `active sections`_ of the
67 `configuration files`_ in the order described in
68 `Configuration File Sections & Entries`_. (details__)
69
70 See also `Configuration File Sections & Entries`_.
71
72 __ `OptionParser.get_standard_config_settings()`_
73
74 5. If enabled, command line arguments (details__).
75
76 __ `Publisher.process_command_line()`_
77
78
79 Settings assigned to the `"settings" argument`_ of the
80 `convenience functions`_ or the ``Publisher.settings`` attribute
81 are used **instead of** the above sources
82 (see below for details for `command-line tools`__ and
83 `other applications`__).
84
85 __ `publisher.publish()`_
86 __ `publisher.process_programmatic_settings()`_
87
88 .. _command-line tools:
89
90 Runtime settings processing for command-line tools
91 ==================================================
92
93 The command-line `front-end tools`_ usually import and call
94 the Publisher convenience function `publish_cmdline()`_.
95
96 1. ``docutils.core.publish_cmdline()`` creates a `Publisher`_ instance::
97
98 publisher = core.Publisher(…, settings)
99
100 eventually sets the components_ from the respective names, and calls ::
101
102 publisher.publish(argv, …, settings_spec,
103 settings_overrides, config_section, …)
104
105 .. _publisher.publish():
106
107 2. If `publisher.settings` is None, ``publisher.publish()`` calls::
108
109 publisher.process_command_line(…,
110 settings_spec, config_section, **defaults)
111
112 with `defaults` taken from `publisher.settings_overrides`.
113
114 If `publisher.settings` is defined, steps 3 to 5 are skipped.
115
116 3. ``publisher.process_command_line()`` calls::
117
118 optpar = publisher.setup_option_parser(…,
119 settings_spec, config_section, **defaults)
120
121 .. _publisher.setup_option_parser():
122
123 4. ``publisher.setup_option_parser()``
124
125 - merges the value of the `"config_section" argument`_ into
126 `settings_spec` and
127
128 - creates an `OptionParser` instance ::
129
130 optpar = docutils.frontend.OptionParser(components, defaults)
131
132 with `components` the tuple of the `SettingsSpec`_ instances
133 ``(publisher.parser, publisher.reader, publisher.writer, settings_spec)``
134
135 .. _OptionParser.populate_from_components():
136
137 5. The `OptionParser` instance prepends itself to the `components` tuple
138 and calls ``self.populate_from_components(components)``, which updates
139 the attribute ``self.defaults`` in two steps:
140
141 a) For each component passed, ``component.settings_spec`` is processed
142 and ``component.settings_defaults`` is applied.
143
144 .. _component.settings_default_overrides:
145
146 b) In a second loop, for each component
147 ``component.settings_default_overrides`` is applied. This way,
148 ``component.settings_default_overrides`` can override the default
149 settings of any other component.
150
151 .. _OptionParser.defaults:
152
153 6. Back in ``OptionParser.__init__()``, ``self.defaults`` is updated with
154 the `defaults` argument passed to ``OptionParser(…)`` in step 5.
155
156 This means that the `settings_overrides` argument of the
157 `convenience functions`_ has priority over all
158 ``SettingsSpec.settings_spec`` defaults.
159
160 .. _OptionParser.get_standard_config_settings():
161
162 7. If configuration files are enabled,
163 ``self.get_standard_config_settings()`` is called.
164
165 This reads the Docutils `configuration files`_, and returns a
166 dictionary of settings in `active sections`_ which is used to update
167 ``optpar.defaults``. So configuration file settings have priority over
168 all software-defined defaults.
169
170 .. _Publisher.process_command_line():
171
172 8. ``publisher.process_command_line()`` calls ``optpar.parse_args()``.
173 The OptionParser parses all command line options and returns a
174 `docutils.frontend.Values` object.
175 This is assigned to ``publisher.settings``.
176 So command-line options have priority over configuration file
177 settings.
178
179 9. The `<source>` and `<destination>` command-line arguments
180 are also parsed, and assigned to ``publisher.settings._source``
181 and ``publisher.settings._destination`` respectively.
182
183 10. ``publisher.publish()`` calls ``publisher.set_io()`` with no arguments.
184 If either ``publisher.source`` or ``publisher.destination`` are not
185 set, the corresponding ``publisher.set_source()`` and
186 ``publisher.set_destination()`` are called:
187
188 ``publisher.set_source()``
189 checks for a ``source_path`` argument, and if there is none (which
190 is the case for command-line use), it is taken from
191 ``publisher.settings._source``. ``publisher.source`` is set by
192 instantiating a ``publisher.source_class`` object.
193 For command-line front-end tools, the default
194 ``publisher.source_class`` (i.e. ``docutils.io.FileInput``)
195 is used.
196
197 ``publisher.set_destination()``
198 does the same job for the destination. (the default
199 ``publisher.destination_class`` is ``docutils.io.FileOutput``).
200
201 .. _accessing the runtime settings:
202
203 11. ``publisher.publish()`` passes ``publisher.settings`` to the reader_
204 component's ``read()`` method.
205
206 12. The reader component creates a new `document root node`__.
207 ``nodes.document.__init__()`` adds the settings to the internal
208 attributes.
209
210 __ ../ref/doctree.html#document
211
212 All components acting on the Document Tree are handed the
213 ``document`` root node and can access the runtime settings as
214 ``document.settings``.
215
216
217 Runtime settings processing for other applications
218 ==================================================
219
220 The `convenience functions`_ , ``core.publish_file()``,
221 ``core.publish_string()``, or ``core.publish_parts()`` do not parse the
222 command line for settings.
223
224 1. The convenience functions call the generic programmatic interface
225 function ``core.publish_programmatically()`` that creates a
226 `core.Publisher` instance ::
227
228 pub = core.Publisher(…, settings)
229
230 eventually sets the components_ from the respective names, and calls ::
231
232 publisher.process_programmatic_settings(
233 settings_spec, settings_overrides, config_section)
234
235 .. _publisher.process_programmatic_settings():
236
237 2. If `publisher.settings` is None,
238 ``publisher.process_programmatic_settings()`` calls::
239
240 publisher.get_settings(settings_spec, config_section, **defaults)
241
242 with `defaults` taken from `publisher.settings_overrides`.
243
244 If `publisher.settings` is defined, the following steps are skipped.
245
246 3. ``publisher.get_settings()`` calls::
247
248 optpar = publisher.setup_option_parser(…,
249 settings_spec, config_section, **defaults)
250
251 4. The OptionParser instance determines setting defaults
252 as described in `steps 4 to 7`__ in the previous section.
253
254 __ `publisher.setup_option_parser()`_
255
256 5. Back in ``publisher.get_settings()``, the ``frontend.Values`` instance
257 returned by ``optpar.get_default_values()`` is stored in
258 ``publisher.settings``.
259
260 6. ``publish_programmatically()`` continues with setting
261 ``publisher.source`` and ``publisher.destination``.
262
263 7. Finally, ``publisher.publish()`` is called. As ``publisher.settings``
264 is not None, no further command line processing takes place.
265
266 8. All components acting on the Document Tree are handed the
267 ``document`` root node and can access the runtime settings as
268 ``document.settings`` (cf. `steps 11 and 12`__ in the previous section).
269
270 __ `accessing the runtime settings`_
271
272
273 .. References:
274
275 .. _optparse: https://docs.python.org/dev/library/optparse.html
276 .. _argparse: https://docs.python.org/dev/library/argparse.html
277
278 .. _Docutils Runtime Settings:
279 ../api/runtime-settings.html
280 .. _active sections: ../api/runtime-settings.html#active-sections
281 .. _SettingsSpec: ../api/runtime-settings.html#settingsspec
282 .. _component:
283 .. _components: ../api/runtime-settings.html#components
284
285 .. _convenience functions:
286 .. _Publisher convenience functions:
287 ../api/publisher.html#publisher-convenience-functions
288 .. _publish_cmdline(): ../api/publisher.html#publish-cmdline
289 .. _"settings" argument: ../api/publisher.html#settings
290 .. _"settings_overrides" argument: ../api/publisher.html#settings-overrides
291 .. _"config_section" argument: ../api/publisher.html#config-section
292
293 .. _front-end tools: ../user/tools.html
294 .. _configuration file:
295 .. _configuration files:
296 .. _Docutils Configuration: ../user/config.html#configuration-files
297 .. _Configuration File Sections & Entries:
298 ../user/config.html#configuration-file-sections-entries
299 .. _Docutils Project Model: ../peps/pep-0258.html#docutils-project-model
300 .. _Publisher: ../peps/pep-0258.html#publisher
301 .. _Reader: ../peps/pep-0258.html#reader