File: cmdhelp.py

package info (click to toggle)
xonsh 0.22.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,628 kB
  • sloc: python: 49,591; sh: 185; makefile: 133; xml: 17
file content (73 lines) | stat: -rw-r--r-- 2,152 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
"""This module adds a reST directive to sphinx that generates alias
documentation. For example::

    .. command-help:: xonsh.aliases.source_foreign

    .. command-help:: xonsh.aliases.source_foreign -h

will create help for aliases.
"""
import importlib
import io
import textwrap

from docutils import nodes

try:
    from docutils.utils.error_reporting import ErrorString  # docutils <= 0.20
except ImportError:
    try:
        from docutils.error_reporting import ErrorString  # docutils old
    except Exception:
            # docutils >= 0.22.4
            def ErrorString(exc):
                return str(exc)

from docutils.parsers.rst import Directive
from docutils.statemachine import ViewList

from sphinx.util.nodes import nested_parse_with_titles

from xonsh.tools import redirect_stdout, redirect_stderr


class CommandHelp(Directive):
    """The command-help directive, which is based on constructing a list of
    of string lines of restructured text and then parsing it into its own node.
    Note that this will add the '--help' flag automatically.
    """

    required_arguments = 1
    optional_arguments = 1
    final_argument_whitespace = True
    option_spec = {}
    has_content = False

    def run(self):
        arguments = self.arguments
        lines = [".. code-block:: none", ""]
        m, f = arguments[0].rsplit(".", 1)
        mod = importlib.import_module(m)
        func = getattr(mod, f)
        args = ["--help"] if len(arguments) == 1 else arguments[1:]
        stdout = io.StringIO()
        stderr = io.StringIO()
        with redirect_stdout(stdout), redirect_stderr(stderr):
            try:
                func(args)
            except SystemExit:
                pass
        stdout.seek(0)
        s = stdout.read()
        lines += textwrap.indent(s, "    ").splitlines()

        # hook to docutils
        src, lineno = self.state_machine.get_source_and_line(self.lineno)
        vl = ViewList(lines, source=src)
        node = nodes.paragraph()
        nested_parse_with_titles(self.state, vl, node)
        return node.children


def setup(app):
    app.add_directive("command-help", CommandHelp)