File: show_reno_config.py

package info (click to toggle)
python-reno 4.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 776 kB
  • sloc: python: 5,263; makefile: 21; sh: 10
file content (73 lines) | stat: -rw-r--r-- 2,212 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from docutils import nodes
from docutils.parsers import rst
from docutils.statemachine import ViewList
from sphinx.util import logging
from sphinx.util.nodes import nested_parse_with_titles

from reno import config

LOG = logging.getLogger(__name__)


def _multi_line_string(s, indent=''):
    output_lines = s.splitlines()
    if not output_lines[0].strip():
        output_lines = output_lines[1:]
    for l in output_lines:
        yield indent + l


def _format_option_help(options):
    "Produce RST lines for the configuration options."
    for opt in sorted(options, key=lambda opt: opt.name):
        yield '``{}``'.format(opt.name)
        for l in _multi_line_string(opt.help, '  '):
            yield l
        yield ''
        if isinstance(opt.default, str) and '\n' in opt.default:
            # Multi-line string
            yield '  Defaults to'
            yield ''
            yield '  ::'
            yield ''
            for l in _multi_line_string(opt.default, '    '):
                yield l
        else:
            yield '  Defaults to ``{!r}``'.format(opt.default)
        yield ''


class ShowConfigDirective(rst.Directive):

    option_spec = {}

    has_content = True

    def run(self):
        result = ViewList()
        source_name = '<' + __name__ + '>'
        for line in _format_option_help(config._OPTIONS):
            LOG.info(line)
            result.append(line, source_name)

        node = nodes.section()
        node.document = self.state.document
        nested_parse_with_titles(self.state, result, node)

        return node.children


def setup(app):
    app.add_directive('show-reno-config', ShowConfigDirective)