Debian Codegraph @

docutils/__main__.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # :Copyright: © 2020, 2022 Günter Milde.
3 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
4 #
5 # Copying and distribution of this file, with or without modification,
6 # are permitted in any medium without royalty provided the copyright
7 # notice and this notice are preserved.
8 # This file is offered as-is, without any warranty.
9 #
10 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
11 #
12 # Revision: $Revision: 10136 $
13 # Date: $Date: 2025-05-20 17:48:27 +0200 (Di, 20. Mai 2025) $
14
15 """Generic command line interface for the `docutils` package.
16
17 See also
18 https://docs.python.org/3/library/__main__.html#main-py-in-python-packages
19 """
20
21 from __future__ import annotations
22
23 __docformat__ = 'reStructuredText'
24
25 import argparse
26 import locale
27 import sys
28
29 import docutils
30 from docutils.core import Publisher, publish_cmdline, default_description
31
32
33 class CliSettingsSpec(docutils.SettingsSpec):
34 """Runtime settings & command-line options for the generic CLI.
35
36 Configurable reader, parser, and writer components.
37
38 The "--writer" default will change to 'html' in Docutils 2.0
39 when 'html' becomes an alias for the current value 'html5'.
40 """
41
42 settings_spec = (
43 'Docutils Application Options',
44 'Reader, writer, and parser settings influence the available options. '
45 ' Example: use `--help --writer=latex` to see LaTeX writer options. ',
46 # options: ('help text', [<option strings>], {<keyword arguments>})
47 (('Reader name (currently: "%default").',
48 ['--reader'], {'default': 'standalone', 'metavar': '<reader>'}),
49 ('Parser name (currently: "%default").',
50 ['--parser'], {'default': 'rst', 'metavar': '<parser>'}),
51 ('Writer name (currently: "%default").',
52 ['--writer'], {'default': 'html5', 'metavar': '<writer>'}),
53 )
54 )
55 config_section = 'docutils application'
56 config_section_dependencies = ('docutils-cli application', # back-compat
57 'applications')
58
59
60 def main() -> None:
61 """Generic command line interface for the Docutils Publisher.
62 """
63 try:
64 locale.setlocale(locale.LC_ALL, '')
65 except locale.Error as e:
66 sys.stderr.write(f'WARNING: Cannot set the default locale: {e}.\n')
67
68 description = ('Convert documents into useful formats. '
69 + default_description)
70
71 # Update component selection from config file(s)
72 components = Publisher().get_settings(settings_spec=CliSettingsSpec)
73
74 # Update component selection from command-line
75 argparser = argparse.ArgumentParser(add_help=False, allow_abbrev=False)
76 argparser.add_argument('--reader', default=components.reader)
77 argparser.add_argument('--parser', default=components.parser)
78 argparser.add_argument('--writer', default=components.writer)
79 # other options are parsed in a second pass via `publish_cmdline()`
80 (args, remainder) = argparser.parse_known_args()
81 # Ensure the current component selections are shown in help:
82 CliSettingsSpec.settings_default_overrides = args.__dict__
83
84 try:
85 publish_cmdline(reader=args.reader,
86 parser=args.parser,
87 writer=args.writer,
88 settings_spec=CliSettingsSpec,
89 description=description,
90 argv=remainder)
91 except ImportError as error:
92 print('%s.' % error, file=sys.stderr)
93 if '--traceback' in remainder:
94 raise
95 else:
96 print('Use "--traceback" to show details.')
97
98
99 if __name__ == '__main__':
100 if sys.argv[0].endswith('__main__.py'):
101 # fix "usage" message
102 sys.argv[0] = '%s -m docutils' % sys.executable
103 main()