Docutils Runtime Settings¶
| Author: | David Goodger |
|---|---|
| Contact: | docutils-develop@lists.sourceforge.net |
| Date: | $Date: 2012-01-03 19:23:53 +0000 (Tue, 03 Jan 2012) $ |
| Revision: | $Revision: 7302 $ |
| Copyright: | このドキュメントは、パブリック ドメインで公開されています。 |
Contents
Introduction¶
Docutils runtime settings are assembled from several sources: component settings specifications, application settings specifications, configuration files, and command-line options. Docutils overlays default and explicitly specified values from these sources such that settings behave the way we want and expect them to behave.
To understand how Docutils deals with runtime settings, the attributes
and parameters involved must first be understood. Begin with the the
docstrings of the attributes of the docutils.SettingsSpec base
class (in the docutils/__init__.py module):
settings_specsettings_defaultssettings_default_overridesrelative_path_settingsconfig_sectionconfig_section_dependencies
Next, several convenience function parameters are also significant
(described in the docutils.core.publish_programmatically function
docstring):
- The
settingsparameter is a runtime settings (docutils.frontend.Values) object which, if present, is assumed to be complete (it must include all runtime settings). Also, if thesettingsparameter is present, no further runtime settings processing is done. In other words, the other parameters, described below, will have no effect. settings_spec, a docutils.SettingsSpec subclass or object, is treated like a fourth component (after the Parser, Reader, and Writer). In other words, it's the settings specification for the "Application" itself.settings_overridesis a dictionary which will override the defaults of the components (from their settings specs).config_sectionspecifies the name of an application-specific configuration file section.
Runtime Settings Processing for Command-Line Tools¶
Following along with the actual code is recommended. The
docutils/__init__.py, docutils/core.py, and
docutils.frontend.py modules are described.
A command-line front-end tool imports and calls
docutils.core.publish_cmdline. The relevant convenience function parameters are described above.docutils.core.publish_cmdlineinitializes adocutils.core.Publisherobject, then calls itspublishmethod.The
docutils.core.Publisherobject'spublishmethod checks itssettingsattribute to see if it's defined. If it is, no further runtime settings processing is done.If
settingsis not defined,self.process_command_lineis called with the following relevant arguments:settings_specconfig_sectionsettings_overrides(in the form of excess keyword arguments, collected in thedefaultsparameter)
self.process_command_linecallsself.setup_option_parser, passingsettings_spec,config_section, anddefaults.self.setup_option_parserchecks itsconfig_sectionparameter; if defined, it adds that config file section tosettings_spec(or to a new, emptydocutils.SettingsSpecobject), replacing anything defined earlier. (See Docutils Configuration Files for details.) Then it instantiates a newdocutils.frontend.OptionParserobject, passing the following relevant arguments:components: A tuple ofdocutils.SettingsSpecobjects,(self.parser, self.reader, self.writer, settings_spec)defaults(originally fromsettings_overrides)
The
docutils.frontend.OptionParserobject's__init__method callsself.populate_from_componentswithself.components, which consists ofselfprepended to thecomponentstuple it received.self(docutils.frontend.OptionParser) defines general Docutils settings.In
self.populate_from_components, for each component passed,component.settings_specis processed andcomponent.settings_defaultsis applied. Then, for each component,component.settings_default_overridesis applied. This two-loop process ensures thatcomponent.settings_default_overridescan override the default settings of any other component.Back in
docutils.frontend.OptionParser.__init__, thedefaultsparameter (derived from thesettings_overridesparameter ofdocutils.core.Publisher.publish) is overlaid overself.defaults. Sosettings_overrideshas priority over allSettingsSpecdata.Next,
docutils.frontend.OptionParser.__init__checks if configuration files are enabled (itsread_config_filesparameter is true, andself.defaults['_disable_config']is false). If they are enabled (and normally, they are),self.get_standard_config_settingsis called. This reads the docutils configuration files, and returns a dictionary of settings. This is then overlaid onself.defaults. So configuration file settings have priority over all software-defined defaults.Back in the
docutils.core.Publisherobject,self.setup_option_parserreturns theoption_parserobject to its caller,self.process_command_line.self.process_command_linecallsoption_parser.parse_args, which parses all command line options and returns adocutils.frontend.Valuesobject. This is assigned to thedocutils.core.Publisherobject'sself.settings. So command-line options have priority over configuration file settings.When
option_parser.parse_argsis called, the source and destination command-line arguments are also parsed, and assigned to the_sourceand_destinationattributes of what becomes thedocutils.core.Publisherobject'sself.settings.From
docutils.core.Publisher.publish,self.set_iois called with no arguments. If eitherself.sourceorself.destinationare not set, the correspondingself.set_sourceandself.set_destinationare called, effectively with no arguments.self.set_sourcechecks for asource_pathparameter, and if there is none (which is the case for command-line use), it is taken fromself.settings._source.self.sourceis set by instantiating aself.source_classobject. For command-line front-end tools, the defaultself.source_classis used,docutils.io.FileInput.self.set_destinationdoes the same job for the destination thatself.set_sourcedoes for the source (the defaultself.destination_classisdocutils.io.FileOutput).
Runtime Settings Processing From Applications¶
Applications process runtime settings in a different way than
command-line tools do. Instead of calling publish_cmdline, the
application calls one of publish_file, publish_string, or
publish_parts. These in turn call publish_programmatically,
which implements a generic programmatic interface. Although an
application may also call publish_programmatically directly, it is
not recommended (if it does seem to be necessary, please write to the
Docutils-develop mailing list).
publish_programmatically accepts the same convenience function
parameters as publish_cmdline. Where things differ is that
programmatic use does no command-line processing. Instead of calling
docutils.Publisher.process_command_line (as publish_cmdline
does, via docutils.Publisher.publish),
docutils.Publisher.process_programmatic_settings is called to set
up the runtime settings.