From ee7ceafc3ce05806f85ae51385e8f2c699549bb2 Mon Sep 17 00:00:00 2001 From: milde Date: Tue, 19 Oct 2021 17:03:02 +0000 Subject: [PATCH] docutils-cli.py: Read settings from standard configuration files. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@8858 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/HISTORY.txt | 4 ++++ docutils/docs/user/config.txt | 29 ++++++++++++++++++++++++++ docutils/docutils/frontend.py | 6 +++--- docutils/tools/docutils-cli.py | 46 ++++++++++++++++++++++++++++++++---------- 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt index 629a1543d..3ea17f379 100644 --- a/docutils/HISTORY.txt +++ b/docutils/HISTORY.txt @@ -45,6 +45,10 @@ Will be merged on release. - New option "image_loading". Support "lazy" loading of images. Obsoletes "embed_images". +* tools/docutils-cli.py + + - Read settings from standard configuration files. + Release 0.18b1 (2021-10-05) =========================== diff --git a/docutils/docs/user/config.txt b/docutils/docs/user/config.txt index 815692ad5..230c8d573 100644 --- a/docutils/docs/user/config.txt +++ b/docutils/docs/user/config.txt @@ -2126,6 +2126,35 @@ New in 0.17. Obsoletes the ``html_writer`` option. .. _HTML writer: html.html +[docutils-cli application] +-------------------------- + +New in 0.18. + +reader +~~~~~~ +Reader component name. One of "standalone" or "pep". + +Default: "standalone". +Option: ``--reader`` + +parser +~~~~~~ +Parser component name. One of "markdown", "recommonmark", or "rst" + +Default: "rst". +Option: ``--parser`` + +.. _writer [docutils-cli application]: + +writer +~~~~~~ +Writer component name. One of the valid writer names or aliases. + +Default: "html5". +Option: ``--writer`` + + Other Settings ============== diff --git a/docutils/docutils/frontend.py b/docutils/docutils/frontend.py index 1c64340a8..1391dd156 100644 --- a/docutils/docutils/frontend.py +++ b/docutils/docutils/frontend.py @@ -332,7 +332,7 @@ class Values(optparse.Values): def copy(self): """Return a shallow copy of `self`.""" return self.__class__(defaults=self.__dict__) - + def setdefault(self, name, default): """V.setdefault(n[,d]) -> getattr(V,n,d), also set D.n=d if n not in D or None. """ @@ -683,7 +683,7 @@ class OptionParser(optparse.OptionParser, docutils.SettingsSpec): parser.read(config_file, self) self.config_files.extend(parser._files) base_path = os.path.dirname(config_file) - applied = {} + applied = set() settings = Values() for component in self.components: if not component: @@ -692,7 +692,7 @@ class OptionParser(optparse.OptionParser, docutils.SettingsSpec): + (component.config_section,)): if section in applied: continue - applied[section] = 1 + applied.add(section) settings.update(parser.get_section(section), self) make_paths_absolute( settings.__dict__, self.relative_path_settings, base_path) diff --git a/docutils/tools/docutils-cli.py b/docutils/tools/docutils-cli.py index 7ecacb726..06ea307b7 100755 --- a/docutils/tools/docutils-cli.py +++ b/docutils/tools/docutils-cli.py @@ -28,31 +28,54 @@ import sys from docutils.core import publish_cmdline, default_description from docutils.utils import SystemMessage +from docutils.frontend import ConfigParser, OptionParser + +config_section = 'docutils-cli application' + +def config_settings_from_files(): + """Return dict with default settings for the docutils-cli front end. + + Read default values for the three docutils-cli options from the + [docutils-cli application] section in standard configuration files. + + Command-line options are defined separately, using the "argparse" module + to allow two-stage parsing of the command line. + """ + settings = {'reader': 'standalone', + 'parser': 'rst', + 'writer': 'html5' + } + option_parser = OptionParser() + config_parser = ConfigParser() + config_files = option_parser.get_standard_config_files() + config_parser.read(config_files, option_parser) + settings.update(config_parser.get_section(config_section)) + return settings + +default_settings = config_settings_from_files() description = u'Generate documents from reStructuredText or Markdown sources.' - -epilog = (u'Currently, the component selection cannot be specified in the ' - u'configuration file. ' - u'The availability of some options depends on the selected ' + +epilog = (u'The availability of some options depends on the selected ' u'components, the list below adapts to your selection.' ) -parser = argparse.ArgumentParser(add_help=False, +parser = argparse.ArgumentParser(add_help=False, description=description, epilog=epilog, formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--reader', choices=('standalone', 'pep'), help=u'reader name', - default='standalone') -parser.add_argument('--parser', choices=('markdown', 'rst'), + default=default_settings['reader']) +parser.add_argument('--parser', choices=('markdown', 'recommonmark', 'rst'), help=u'parser name', - default='rst') -parser.add_argument('--writer', + default=default_settings['parser']) +parser.add_argument('--writer', # choices=('html', 'html4', 'html5', 'latex', 'xelatex', # 'odt', 'xml', 'pseudoxml', 'manpage', # 'pep_html', 's5_html'), help=u'writer name', - default='html5') + default=default_settings['writer']) (args, remainder) = parser.parse_known_args() @@ -64,7 +87,8 @@ if '-h' in sys.argv or '--help' in sys.argv: try: publish_cmdline(reader_name=args.reader, parser_name=args.parser, - writer_name=args.writer, + writer_name=args.writer, + config_section=config_section, description=default_description, argv=remainder) except ImportError as error: -- 2.11.4.GIT