Fix: 3598893 --stylesheet and --stylesheet-path options broken, patch partially applied
[docutils.git] / docutils / frontend.py
blobebecff19b6872f1c78f51b9ffaa35194fe572827
1 # $Id$
2 # Author: David Goodger <goodger@python.org>
3 # Copyright: This module has been placed in the public domain.
5 """
6 Command-line and common processing for Docutils front-end tools.
8 Exports the following classes:
10 * `OptionParser`: Standard Docutils command-line processing.
11 * `Option`: Customized version of `optparse.Option`; validation support.
12 * `Values`: Runtime settings; objects are simple structs
13 (``object.attribute``). Supports cumulative list settings (attributes).
14 * `ConfigParser`: Standard Docutils config file processing.
16 Also exports the following functions:
18 * Option callbacks: `store_multiple`, `read_config_file`.
19 * Setting validators: `validate_encoding`,
20 `validate_encoding_error_handler`,
21 `validate_encoding_and_error_handler`,
22 `validate_boolean`, `validate_ternary`, `validate_threshold`,
23 `validate_colon_separated_string_list`,
24 `validate_comma_separated_string_list`,
25 `validate_dependency_file`.
26 * `make_paths_absolute`.
27 * SettingSpec manipulation: `filter_settings_spec`.
28 """
30 __docformat__ = 'reStructuredText'
32 import os
33 import os.path
34 import sys
35 import warnings
36 import ConfigParser as CP
37 import codecs
38 import optparse
39 from optparse import SUPPRESS_HELP
40 import docutils
41 import docutils.utils
42 import docutils.nodes
43 from docutils.utils.error_reporting import locale_encoding, ErrorOutput, ErrorString
46 def store_multiple(option, opt, value, parser, *args, **kwargs):
47 """
48 Store multiple values in `parser.values`. (Option callback.)
50 Store `None` for each attribute named in `args`, and store the value for
51 each key (attribute name) in `kwargs`.
52 """
53 for attribute in args:
54 setattr(parser.values, attribute, None)
55 for key, value in kwargs.items():
56 setattr(parser.values, key, value)
58 def read_config_file(option, opt, value, parser):
59 """
60 Read a configuration file during option processing. (Option callback.)
61 """
62 try:
63 new_settings = parser.get_config_file_settings(value)
64 except ValueError, error:
65 parser.error(error)
66 parser.values.update(new_settings, parser)
68 def validate_encoding(setting, value, option_parser,
69 config_parser=None, config_section=None):
70 try:
71 codecs.lookup(value)
72 except LookupError:
73 raise (LookupError('setting "%s": unknown encoding: "%s"'
74 % (setting, value)),
75 None, sys.exc_info()[2])
76 return value
78 def validate_encoding_error_handler(setting, value, option_parser,
79 config_parser=None, config_section=None):
80 try:
81 codecs.lookup_error(value)
82 except LookupError:
83 raise (LookupError(
84 'unknown encoding error handler: "%s" (choices: '
85 '"strict", "ignore", "replace", "backslashreplace", '
86 '"xmlcharrefreplace", and possibly others; see documentation for '
87 'the Python ``codecs`` module)' % value),
88 None, sys.exc_info()[2])
89 return value
91 def validate_encoding_and_error_handler(
92 setting, value, option_parser, config_parser=None, config_section=None):
93 """
94 Side-effect: if an error handler is included in the value, it is inserted
95 into the appropriate place as if it was a separate setting/option.
96 """
97 if ':' in value:
98 encoding, handler = value.split(':')
99 validate_encoding_error_handler(
100 setting + '_error_handler', handler, option_parser,
101 config_parser, config_section)
102 if config_parser:
103 config_parser.set(config_section, setting + '_error_handler',
104 handler)
105 else:
106 setattr(option_parser.values, setting + '_error_handler', handler)
107 else:
108 encoding = value
109 validate_encoding(setting, encoding, option_parser,
110 config_parser, config_section)
111 return encoding
113 def validate_boolean(setting, value, option_parser,
114 config_parser=None, config_section=None):
115 """Check/normalize boolean settings:
116 True: '1', 'on', 'yes', 'true'
117 False: '0', 'off', 'no','false', ''
119 if isinstance(value, bool):
120 return value
121 try:
122 return option_parser.booleans[value.strip().lower()]
123 except KeyError:
124 raise (LookupError('unknown boolean value: "%s"' % value),
125 None, sys.exc_info()[2])
127 def validate_ternary(setting, value, option_parser,
128 config_parser=None, config_section=None):
129 """Check/normalize three-value settings:
130 True: '1', 'on', 'yes', 'true'
131 False: '0', 'off', 'no','false', ''
132 any other value: returned as-is.
134 if isinstance(value, bool) or value is None:
135 return value
136 try:
137 return option_parser.booleans[value.strip().lower()]
138 except KeyError:
139 return value
141 def validate_nonnegative_int(setting, value, option_parser,
142 config_parser=None, config_section=None):
143 value = int(value)
144 if value < 0:
145 raise ValueError('negative value; must be positive or zero')
146 return value
148 def validate_threshold(setting, value, option_parser,
149 config_parser=None, config_section=None):
150 try:
151 return int(value)
152 except ValueError:
153 try:
154 return option_parser.thresholds[value.lower()]
155 except (KeyError, AttributeError):
156 raise (LookupError('unknown threshold: %r.' % value),
157 None, sys.exc_info[2])
159 def validate_colon_separated_string_list(
160 setting, value, option_parser, config_parser=None, config_section=None):
161 if isinstance(value, unicode):
162 value = value.split(':')
163 else:
164 last = value.pop()
165 value.extend(last.split(':'))
166 return value
168 def validate_comma_separated_list(setting, value, option_parser,
169 config_parser=None, config_section=None):
170 """Check/normalize list arguments (split at "," and strip whitespace).
172 # `value` is already a list when given as command line option
173 # and "action" is "append"
174 # in python2 buildhtml.py calls this once with str after several
175 # times with unicode. MAYBE fix somewhere else.
176 #if isinstance(value, unicode): #py3
177 #if isinstance(value, basestr): # py3 and py2.7
178 if not hasattr(value, 'pop'):
179 value = [value]
180 # this function is called for every option added to `value`
181 # -> split the last item and append the result:
182 last = value.pop()
183 classes = [cls.strip(u' \t\n') for cls in last.split(',')
184 if cls.strip(u' \t\n')]
185 value.extend(classes)
186 return value
188 def validate_url_trailing_slash(
189 setting, value, option_parser, config_parser=None, config_section=None):
190 if not value:
191 return './'
192 elif value.endswith('/'):
193 return value
194 else:
195 return value + '/'
197 def validate_dependency_file(setting, value, option_parser,
198 config_parser=None, config_section=None):
199 try:
200 return docutils.utils.DependencyList(value)
201 except IOError:
202 return docutils.utils.DependencyList(None)
204 def validate_strip_class(setting, value, option_parser,
205 config_parser=None, config_section=None):
206 # value is a comma separated string list:
207 value = validate_comma_separated_list(setting, value, option_parser,
208 config_parser, config_section)
209 # validate list elements:
210 for cls in value:
211 normalized = docutils.nodes.make_id(cls)
212 if cls != normalized:
213 raise ValueError('invalid class value %r (perhaps %r?)'
214 % (cls, normalized))
215 return value
217 def make_paths_absolute(pathdict, keys, base_path=None):
219 Interpret filesystem path settings relative to the `base_path` given.
221 Paths are values in `pathdict` whose keys are in `keys`. Get `keys` from
222 `OptionParser.relative_path_settings`.
224 if base_path is None:
225 base_path = os.getcwdu() # type(base_path) == unicode
226 # to allow combining non-ASCII cwd with unicode values in `pathdict`
227 for key in keys:
228 if key in pathdict:
229 value = pathdict[key]
230 if isinstance(value, list):
231 value = [make_one_path_absolute(base_path, path)
232 for path in value]
233 elif value:
234 value = make_one_path_absolute(base_path, value)
235 pathdict[key] = value
237 def make_one_path_absolute(base_path, path):
238 return os.path.abspath(os.path.join(base_path, path))
240 def filter_settings_spec(settings_spec, *exclude, **replace):
241 """Return a copy of `settings_spec` excluding/replacing some settings.
243 `settings_spec` is a tuple of configuration settings with a structure
244 described for docutils.SettingsSpec.settings_spec.
246 Optional positional arguments are names of to-be-excluded settings.
247 Keyword arguments are option specification replacements.
248 (See the html4strict writer for an example.)
250 settings = list(settings_spec)
251 # every third item is a sequence of option tuples
252 for i in range(2, len(settings), 3):
253 newopts = []
254 for opt_spec in settings[i]:
255 # opt_spec is ("<help>", [<option strings>], {<keyword args>})
256 opt_name = [opt_string[2:].replace('-', '_')
257 for opt_string in opt_spec[1]
258 if opt_string.startswith('--')
259 ][0]
260 if opt_name in exclude:
261 continue
262 if opt_name in replace.keys():
263 newopts.append(replace[opt_name])
264 else:
265 newopts.append(opt_spec)
266 settings[i] = tuple(newopts)
267 return tuple(settings)
270 class Values(optparse.Values):
273 Updates list attributes by extension rather than by replacement.
274 Works in conjunction with the `OptionParser.lists` instance attribute.
277 def __init__(self, *args, **kwargs):
278 optparse.Values.__init__(self, *args, **kwargs)
279 if (not hasattr(self, 'record_dependencies')
280 or self.record_dependencies is None):
281 # Set up dependency list, in case it is needed.
282 self.record_dependencies = docutils.utils.DependencyList()
284 def update(self, other_dict, option_parser):
285 if isinstance(other_dict, Values):
286 other_dict = other_dict.__dict__
287 other_dict = other_dict.copy()
288 for setting in option_parser.lists.keys():
289 if (hasattr(self, setting) and setting in other_dict):
290 value = getattr(self, setting)
291 if value:
292 value += other_dict[setting]
293 del other_dict[setting]
294 self._update_loose(other_dict)
296 def copy(self):
297 """Return a shallow copy of `self`."""
298 return self.__class__(defaults=self.__dict__)
301 class Option(optparse.Option):
303 ATTRS = optparse.Option.ATTRS + ['validator', 'overrides']
305 def process(self, opt, value, values, parser):
307 Call the validator function on applicable settings and
308 evaluate the 'overrides' option.
309 Extends `optparse.Option.process`.
311 result = optparse.Option.process(self, opt, value, values, parser)
312 setting = self.dest
313 if setting:
314 if self.validator:
315 value = getattr(values, setting)
316 try:
317 new_value = self.validator(setting, value, parser)
318 except Exception, error:
319 raise (optparse.OptionValueError(
320 'Error in option "%s":\n %s'
321 % (opt, ErrorString(error))),
322 None, sys.exc_info()[2])
323 setattr(values, setting, new_value)
324 if self.overrides:
325 setattr(values, self.overrides, None)
326 return result
329 class OptionParser(optparse.OptionParser, docutils.SettingsSpec):
332 Parser for command-line and library use. The `settings_spec`
333 specification here and in other Docutils components are merged to build
334 the set of command-line options and runtime settings for this process.
336 Common settings (defined below) and component-specific settings must not
337 conflict. Short options are reserved for common settings, and components
338 are restrict to using long options.
341 standard_config_files = [
342 '/etc/docutils.conf', # system-wide
343 './docutils.conf', # project-specific
344 '~/.docutils'] # user-specific
345 """Docutils configuration files, using ConfigParser syntax. Filenames
346 will be tilde-expanded later. Later files override earlier ones."""
348 threshold_choices = 'info 1 warning 2 error 3 severe 4 none 5'.split()
349 """Possible inputs for for --report and --halt threshold values."""
351 thresholds = {'info': 1, 'warning': 2, 'error': 3, 'severe': 4, 'none': 5}
352 """Lookup table for --report and --halt threshold values."""
354 booleans={'1': True, 'on': True, 'yes': True, 'true': True,
355 '0': False, 'off': False, 'no': False, 'false': False, '': False}
356 """Lookup table for boolean configuration file settings."""
358 default_error_encoding = getattr(sys.stderr, 'encoding',
359 None) or locale_encoding or 'ascii'
361 default_error_encoding_error_handler = 'backslashreplace'
363 settings_spec = (
364 'General Docutils Options',
365 None,
366 (('Specify the document title as metadata.',
367 ['--title'], {}),
368 ('Include a "Generated by Docutils" credit and link.',
369 ['--generator', '-g'], {'action': 'store_true',
370 'validator': validate_boolean}),
371 ('Do not include a generator credit.',
372 ['--no-generator'], {'action': 'store_false', 'dest': 'generator'}),
373 ('Include the date at the end of the document (UTC).',
374 ['--date', '-d'], {'action': 'store_const', 'const': '%Y-%m-%d',
375 'dest': 'datestamp'}),
376 ('Include the time & date (UTC).',
377 ['--time', '-t'], {'action': 'store_const',
378 'const': '%Y-%m-%d %H:%M UTC',
379 'dest': 'datestamp'}),
380 ('Do not include a datestamp of any kind.',
381 ['--no-datestamp'], {'action': 'store_const', 'const': None,
382 'dest': 'datestamp'}),
383 ('Include a "View document source" link.',
384 ['--source-link', '-s'], {'action': 'store_true',
385 'validator': validate_boolean}),
386 ('Use <URL> for a source link; implies --source-link.',
387 ['--source-url'], {'metavar': '<URL>'}),
388 ('Do not include a "View document source" link.',
389 ['--no-source-link'],
390 {'action': 'callback', 'callback': store_multiple,
391 'callback_args': ('source_link', 'source_url')}),
392 ('Link from section headers to TOC entries. (default)',
393 ['--toc-entry-backlinks'],
394 {'dest': 'toc_backlinks', 'action': 'store_const', 'const': 'entry',
395 'default': 'entry'}),
396 ('Link from section headers to the top of the TOC.',
397 ['--toc-top-backlinks'],
398 {'dest': 'toc_backlinks', 'action': 'store_const', 'const': 'top'}),
399 ('Disable backlinks to the table of contents.',
400 ['--no-toc-backlinks'],
401 {'dest': 'toc_backlinks', 'action': 'store_false'}),
402 ('Link from footnotes/citations to references. (default)',
403 ['--footnote-backlinks'],
404 {'action': 'store_true', 'default': 1,
405 'validator': validate_boolean}),
406 ('Disable backlinks from footnotes and citations.',
407 ['--no-footnote-backlinks'],
408 {'dest': 'footnote_backlinks', 'action': 'store_false'}),
409 ('Enable section numbering by Docutils. (default)',
410 ['--section-numbering'],
411 {'action': 'store_true', 'dest': 'sectnum_xform',
412 'default': 1, 'validator': validate_boolean}),
413 ('Disable section numbering by Docutils.',
414 ['--no-section-numbering'],
415 {'action': 'store_false', 'dest': 'sectnum_xform'}),
416 ('Remove comment elements from the document tree.',
417 ['--strip-comments'],
418 {'action': 'store_true', 'validator': validate_boolean}),
419 ('Leave comment elements in the document tree. (default)',
420 ['--leave-comments'],
421 {'action': 'store_false', 'dest': 'strip_comments'}),
422 ('Remove all elements with classes="<class>" from the document tree. '
423 'Warning: potentially dangerous; use with caution. '
424 '(Multiple-use option.)',
425 ['--strip-elements-with-class'],
426 {'action': 'append', 'dest': 'strip_elements_with_classes',
427 'metavar': '<class>', 'validator': validate_strip_class}),
428 ('Remove all classes="<class>" attributes from elements in the '
429 'document tree. Warning: potentially dangerous; use with caution. '
430 '(Multiple-use option.)',
431 ['--strip-class'],
432 {'action': 'append', 'dest': 'strip_classes',
433 'metavar': '<class>', 'validator': validate_strip_class}),
434 ('Report system messages at or higher than <level>: "info" or "1", '
435 '"warning"/"2" (default), "error"/"3", "severe"/"4", "none"/"5"',
436 ['--report', '-r'], {'choices': threshold_choices, 'default': 2,
437 'dest': 'report_level', 'metavar': '<level>',
438 'validator': validate_threshold}),
439 ('Report all system messages. (Same as "--report=1".)',
440 ['--verbose', '-v'], {'action': 'store_const', 'const': 1,
441 'dest': 'report_level'}),
442 ('Report no system messages. (Same as "--report=5".)',
443 ['--quiet', '-q'], {'action': 'store_const', 'const': 5,
444 'dest': 'report_level'}),
445 ('Halt execution at system messages at or above <level>. '
446 'Levels as in --report. Default: 4 (severe).',
447 ['--halt'], {'choices': threshold_choices, 'dest': 'halt_level',
448 'default': 4, 'metavar': '<level>',
449 'validator': validate_threshold}),
450 ('Halt at the slightest problem. Same as "--halt=info".',
451 ['--strict'], {'action': 'store_const', 'const': 1,
452 'dest': 'halt_level'}),
453 ('Enable a non-zero exit status for non-halting system messages at '
454 'or above <level>. Default: 5 (disabled).',
455 ['--exit-status'], {'choices': threshold_choices,
456 'dest': 'exit_status_level',
457 'default': 5, 'metavar': '<level>',
458 'validator': validate_threshold}),
459 ('Enable debug-level system messages and diagnostics.',
460 ['--debug'], {'action': 'store_true', 'validator': validate_boolean}),
461 ('Disable debug output. (default)',
462 ['--no-debug'], {'action': 'store_false', 'dest': 'debug'}),
463 ('Send the output of system messages to <file>.',
464 ['--warnings'], {'dest': 'warning_stream', 'metavar': '<file>'}),
465 ('Enable Python tracebacks when Docutils is halted.',
466 ['--traceback'], {'action': 'store_true', 'default': None,
467 'validator': validate_boolean}),
468 ('Disable Python tracebacks. (default)',
469 ['--no-traceback'], {'dest': 'traceback', 'action': 'store_false'}),
470 ('Specify the encoding and optionally the '
471 'error handler of input text. Default: <locale-dependent>:strict.',
472 ['--input-encoding', '-i'],
473 {'metavar': '<name[:handler]>',
474 'validator': validate_encoding_and_error_handler}),
475 ('Specify the error handler for undecodable characters. '
476 'Choices: "strict" (default), "ignore", and "replace".',
477 ['--input-encoding-error-handler'],
478 {'default': 'strict', 'validator': validate_encoding_error_handler}),
479 ('Specify the text encoding and optionally the error handler for '
480 'output. Default: UTF-8:strict.',
481 ['--output-encoding', '-o'],
482 {'metavar': '<name[:handler]>', 'default': 'utf-8',
483 'validator': validate_encoding_and_error_handler}),
484 ('Specify error handler for unencodable output characters; '
485 '"strict" (default), "ignore", "replace", '
486 '"xmlcharrefreplace", "backslashreplace".',
487 ['--output-encoding-error-handler'],
488 {'default': 'strict', 'validator': validate_encoding_error_handler}),
489 ('Specify text encoding and error handler for error output. '
490 'Default: %s:%s.'
491 % (default_error_encoding, default_error_encoding_error_handler),
492 ['--error-encoding', '-e'],
493 {'metavar': '<name[:handler]>', 'default': default_error_encoding,
494 'validator': validate_encoding_and_error_handler}),
495 ('Specify the error handler for unencodable characters in '
496 'error output. Default: %s.'
497 % default_error_encoding_error_handler,
498 ['--error-encoding-error-handler'],
499 {'default': default_error_encoding_error_handler,
500 'validator': validate_encoding_error_handler}),
501 ('Specify the language (as BCP 47 language tag). Default: en.',
502 ['--language', '-l'], {'dest': 'language_code', 'default': 'en',
503 'metavar': '<name>'}),
504 ('Write output file dependencies to <file>.',
505 ['--record-dependencies'],
506 {'metavar': '<file>', 'validator': validate_dependency_file,
507 'default': None}), # default set in Values class
508 ('Read configuration settings from <file>, if it exists.',
509 ['--config'], {'metavar': '<file>', 'type': 'string',
510 'action': 'callback', 'callback': read_config_file}),
511 ("Show this program's version number and exit.",
512 ['--version', '-V'], {'action': 'version'}),
513 ('Show this help message and exit.',
514 ['--help', '-h'], {'action': 'help'}),
515 # Typically not useful for non-programmatical use:
516 (SUPPRESS_HELP, ['--id-prefix'], {'default': ''}),
517 (SUPPRESS_HELP, ['--auto-id-prefix'], {'default': 'id'}),
518 # Hidden options, for development use only:
519 (SUPPRESS_HELP, ['--dump-settings'], {'action': 'store_true'}),
520 (SUPPRESS_HELP, ['--dump-internals'], {'action': 'store_true'}),
521 (SUPPRESS_HELP, ['--dump-transforms'], {'action': 'store_true'}),
522 (SUPPRESS_HELP, ['--dump-pseudo-xml'], {'action': 'store_true'}),
523 (SUPPRESS_HELP, ['--expose-internal-attribute'],
524 {'action': 'append', 'dest': 'expose_internals',
525 'validator': validate_colon_separated_string_list}),
526 (SUPPRESS_HELP, ['--strict-visitor'], {'action': 'store_true'}),
528 """Runtime settings and command-line options common to all Docutils front
529 ends. Setting specs specific to individual Docutils components are also
530 used (see `populate_from_components()`)."""
532 settings_defaults = {'_disable_config': None,
533 '_source': None,
534 '_destination': None,
535 '_config_files': None}
536 """Defaults for settings that don't have command-line option equivalents."""
538 relative_path_settings = ('warning_stream',)
540 config_section = 'general'
542 version_template = ('%%prog (Docutils %s [%s], Python %s, on %s)'
543 % (docutils.__version__, docutils.__version_details__,
544 sys.version.split()[0], sys.platform))
545 """Default version message."""
547 def __init__(self, components=(), defaults=None, read_config_files=None,
548 *args, **kwargs):
550 `components` is a list of Docutils components each containing a
551 ``.settings_spec`` attribute. `defaults` is a mapping of setting
552 default overrides.
555 self.lists = {}
556 """Set of list-type settings."""
558 self.config_files = []
559 """List of paths of applied configuration files."""
561 optparse.OptionParser.__init__(
562 self, option_class=Option, add_help_option=None,
563 formatter=optparse.TitledHelpFormatter(width=78),
564 *args, **kwargs)
565 if not self.version:
566 self.version = self.version_template
567 # Make an instance copy (it will be modified):
568 self.relative_path_settings = list(self.relative_path_settings)
569 self.components = (self,) + tuple(components)
570 self.populate_from_components(self.components)
571 self.set_defaults_from_dict(defaults or {})
572 if read_config_files and not self.defaults['_disable_config']:
573 try:
574 config_settings = self.get_standard_config_settings()
575 except ValueError, error:
576 self.error(error)
577 self.set_defaults_from_dict(config_settings.__dict__)
579 def populate_from_components(self, components):
581 For each component, first populate from the `SettingsSpec.settings_spec`
582 structure, then from the `SettingsSpec.settings_defaults` dictionary.
583 After all components have been processed, check for and populate from
584 each component's `SettingsSpec.settings_default_overrides` dictionary.
586 for component in components:
587 if component is None:
588 continue
589 settings_spec = component.settings_spec
590 self.relative_path_settings.extend(
591 component.relative_path_settings)
592 for i in range(0, len(settings_spec), 3):
593 title, description, option_spec = settings_spec[i:i+3]
594 if title:
595 group = optparse.OptionGroup(self, title, description)
596 self.add_option_group(group)
597 else:
598 group = self # single options
599 for (help_text, option_strings, kwargs) in option_spec:
600 option = group.add_option(help=help_text, *option_strings,
601 **kwargs)
602 if kwargs.get('action') == 'append':
603 self.lists[option.dest] = 1
604 if component.settings_defaults:
605 self.defaults.update(component.settings_defaults)
606 for component in components:
607 if component and component.settings_default_overrides:
608 self.defaults.update(component.settings_default_overrides)
610 def get_standard_config_files(self):
611 """Return list of config files, from environment or standard."""
612 try:
613 config_files = os.environ['DOCUTILSCONFIG'].split(os.pathsep)
614 except KeyError:
615 config_files = self.standard_config_files
617 # If 'HOME' is not set, expandvars() requires the 'pwd' module which is
618 # not available under certain environments, for example, within
619 # mod_python. The publisher ends up in here, and we need to publish
620 # from within mod_python. Therefore we need to avoid expanding when we
621 # are in those environments.
622 expand = os.path.expanduser
623 if 'HOME' not in os.environ:
624 try:
625 import pwd
626 except ImportError:
627 expand = lambda x: x
628 return [expand(f) for f in config_files if f.strip()]
630 def get_standard_config_settings(self):
631 settings = Values()
632 for filename in self.get_standard_config_files():
633 settings.update(self.get_config_file_settings(filename), self)
634 return settings
636 def get_config_file_settings(self, config_file):
637 """Returns a dictionary containing appropriate config file settings."""
638 parser = ConfigParser()
639 parser.read(config_file, self)
640 self.config_files.extend(parser._files)
641 base_path = os.path.dirname(config_file)
642 applied = {}
643 settings = Values()
644 for component in self.components:
645 if not component:
646 continue
647 for section in (tuple(component.config_section_dependencies or ())
648 + (component.config_section,)):
649 if section in applied:
650 continue
651 applied[section] = 1
652 settings.update(parser.get_section(section), self)
653 make_paths_absolute(
654 settings.__dict__, self.relative_path_settings, base_path)
655 return settings.__dict__
657 def check_values(self, values, args):
658 """Store positional arguments as runtime settings."""
659 values._source, values._destination = self.check_args(args)
660 make_paths_absolute(values.__dict__, self.relative_path_settings)
661 values._config_files = self.config_files
662 return values
664 def check_args(self, args):
665 source = destination = None
666 if args:
667 source = args.pop(0)
668 if source == '-': # means stdin
669 source = None
670 if args:
671 destination = args.pop(0)
672 if destination == '-': # means stdout
673 destination = None
674 if args:
675 self.error('Maximum 2 arguments allowed.')
676 if source and source == destination:
677 self.error('Do not specify the same file for both source and '
678 'destination. It will clobber the source file.')
679 return source, destination
681 def set_defaults_from_dict(self, defaults):
682 self.defaults.update(defaults)
684 def get_default_values(self):
685 """Needed to get custom `Values` instances."""
686 defaults = Values(self.defaults)
687 defaults._config_files = self.config_files
688 return defaults
690 def get_option_by_dest(self, dest):
692 Get an option by its dest.
694 If you're supplying a dest which is shared by several options,
695 it is undefined which option of those is returned.
697 A KeyError is raised if there is no option with the supplied
698 dest.
700 for group in self.option_groups + [self]:
701 for option in group.option_list:
702 if option.dest == dest:
703 return option
704 raise KeyError('No option with dest == %r.' % dest)
707 class ConfigParser(CP.RawConfigParser):
709 old_settings = {
710 'pep_stylesheet': ('pep_html writer', 'stylesheet'),
711 'pep_stylesheet_path': ('pep_html writer', 'stylesheet_path'),
712 'pep_template': ('pep_html writer', 'template')}
713 """{old setting: (new section, new setting)} mapping, used by
714 `handle_old_config`, to convert settings from the old [options] section."""
716 old_warning = """
717 The "[option]" section is deprecated. Support for old-format configuration
718 files may be removed in a future Docutils release. Please revise your
719 configuration files. See <http://docutils.sf.net/docs/user/config.html>,
720 section "Old-Format Configuration Files".
723 not_utf8_error = """\
724 Unable to read configuration file "%s": content not encoded as UTF-8.
725 Skipping "%s" configuration file.
728 def __init__(self, *args, **kwargs):
729 CP.RawConfigParser.__init__(self, *args, **kwargs)
731 self._files = []
732 """List of paths of configuration files read."""
734 self._stderr = ErrorOutput()
735 """Wrapper around sys.stderr catching en-/decoding errors"""
737 def read(self, filenames, option_parser):
738 if type(filenames) in (str, unicode):
739 filenames = [filenames]
740 for filename in filenames:
741 try:
742 # Config files must be UTF-8-encoded:
743 fp = codecs.open(filename, 'r', 'utf-8')
744 except IOError:
745 continue
746 try:
747 if sys.version_info < (3,2):
748 CP.RawConfigParser.readfp(self, fp, filename)
749 else:
750 CP.RawConfigParser.read_file(self, fp, filename)
751 except UnicodeDecodeError:
752 self._stderr.write(self.not_utf8_error % (filename, filename))
753 fp.close()
754 continue
755 fp.close()
756 self._files.append(filename)
757 if self.has_section('options'):
758 self.handle_old_config(filename)
759 self.validate_settings(filename, option_parser)
761 def handle_old_config(self, filename):
762 warnings.warn_explicit(self.old_warning, ConfigDeprecationWarning,
763 filename, 0)
764 options = self.get_section('options')
765 if not self.has_section('general'):
766 self.add_section('general')
767 for key, value in options.items():
768 if key in self.old_settings:
769 section, setting = self.old_settings[key]
770 if not self.has_section(section):
771 self.add_section(section)
772 else:
773 section = 'general'
774 setting = key
775 if not self.has_option(section, setting):
776 self.set(section, setting, value)
777 self.remove_section('options')
779 def validate_settings(self, filename, option_parser):
781 Call the validator function and implement overrides on all applicable
782 settings.
784 for section in self.sections():
785 for setting in self.options(section):
786 try:
787 option = option_parser.get_option_by_dest(setting)
788 except KeyError:
789 continue
790 if option.validator:
791 value = self.get(section, setting)
792 try:
793 new_value = option.validator(
794 setting, value, option_parser,
795 config_parser=self, config_section=section)
796 except Exception, error:
797 raise (ValueError(
798 'Error in config file "%s", section "[%s]":\n'
799 ' %s\n'
800 ' %s = %s'
801 % (filename, section, ErrorString(error),
802 setting, value)), None, sys.exc_info()[2])
803 self.set(section, setting, new_value)
804 if option.overrides:
805 self.set(section, option.overrides, None)
807 def optionxform(self, optionstr):
809 Transform '-' to '_' so the cmdline form of option names can be used.
811 return optionstr.lower().replace('-', '_')
813 def get_section(self, section):
815 Return a given section as a dictionary (empty if the section
816 doesn't exist).
818 section_dict = {}
819 if self.has_section(section):
820 for option in self.options(section):
821 section_dict[option] = self.get(section, option)
822 return section_dict
825 class ConfigDeprecationWarning(DeprecationWarning):
826 """Warning for deprecated configuration file features."""