Debian Codegraph @

test/expensive_benchmarks/docutils_data/docs/dev/runtime-settings-processing.txt

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