File: gen_commands.py

package info (click to toggle)
python-mne 1.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 131,492 kB
  • sloc: python: 213,302; javascript: 12,910; sh: 447; makefile: 144
file content (113 lines) | stat: -rw-r--r-- 3,059 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.

import glob
from importlib import import_module
from pathlib import Path

from mne.utils import ArgvSetter, _replace_md5


def setup(app):
    app.connect("builder-inited", generate_commands_rst)


def setup_module():
    # HACK: Stop nosetests running setup() above
    pass


# Header markings go:
# 1. =/= : Page title
# 2. =   : Command name
# 3. -/- : Command description
# 4. -   : Command sections (Examples, Notes)

header = """\
:orphan:

.. _python_commands:

===============================
Command line tools using Python
===============================

"""

command_rst = """

.. _{0}:

{0}
{1}

.. rst-class:: callout

{2}

"""


def generate_commands_rst(app=None):
    try:
        from sphinx.util.display import status_iterator
    except Exception:
        from sphinx.util import status_iterator
    root = Path(__file__).parents[2]
    out_dir = root / "doc" / "generated"
    out_dir.mkdir(exist_ok=True)
    out_fname = out_dir / "commands.rst.new"

    command_path = root / "mne" / "commands"
    fnames = sorted(
        Path(fname).name for fname in glob.glob(str(command_path / "mne_*.py"))
    )
    assert len(fnames)
    iterator = status_iterator(
        fnames, "generating MNE command help ... ", length=len(fnames)
    )
    with open(out_fname, "w", encoding="utf8") as f:
        f.write(header)
        for fname in iterator:
            cmd_name = fname[:-3]
            module = import_module("." + cmd_name, "mne.commands")
            with ArgvSetter(("mne", cmd_name, "--help")) as out:
                try:
                    module.run()
                except SystemExit:  # this is how these terminate
                    pass
            output = out.stdout.getvalue().splitlines()

            # Swap usage and title lines
            output[0], output[2] = output[2], output[0]

            # Add header marking
            for idx in (1, 0):
                output.insert(idx, "-" * len(output[0]))

            # Add code styling for the "Usage: " line
            for li, line in enumerate(output):
                if line.startswith("Usage: mne "):
                    output[li] = f"Usage: ``{line[7:]}``"
                    break

            # Turn "Options:" into field list
            if "Options:" in output:
                ii = output.index("Options:")
                output[ii] = "Options"
                output.insert(ii + 1, "-------")
                output.insert(ii + 2, "")
                output.insert(ii + 3, ".. rst-class:: field-list cmd-list")
                output.insert(ii + 4, "")
            output = "\n".join(output)
            cmd_name_space = cmd_name.replace("mne_", "mne ")
            f.write(
                command_rst.format(cmd_name_space, "=" * len(cmd_name_space), output)
            )
    _replace_md5(str(out_fname))


# This is useful for testing/iterating to see what the result looks like
if __name__ == "__main__":
    generate_commands_rst()