(app=None)
| 50 | |
| 51 | |
| 52 | def generate_commands_rst(app=None): |
| 53 | try: |
| 54 | from sphinx.util.display import status_iterator |
| 55 | except Exception: |
| 56 | from sphinx.util import status_iterator |
| 57 | root = Path(__file__).parents[2] |
| 58 | out_dir = root / "doc" / "generated" |
| 59 | out_dir.mkdir(exist_ok=True) |
| 60 | out_fname = out_dir / "commands.rst.new" |
| 61 | |
| 62 | command_path = root / "mne" / "commands" |
| 63 | fnames = sorted( |
| 64 | Path(fname).name for fname in glob.glob(str(command_path / "mne_*.py")) |
| 65 | ) |
| 66 | assert len(fnames) |
| 67 | iterator = status_iterator( |
| 68 | fnames, "generating MNE command help ... ", length=len(fnames) |
| 69 | ) |
| 70 | with open(out_fname, "w", encoding="utf8") as f: |
| 71 | f.write(header) |
| 72 | for fname in iterator: |
| 73 | cmd_name = fname[:-3] |
| 74 | module = import_module("." + cmd_name, "mne.commands") |
| 75 | with ArgvSetter(("mne", cmd_name, "--help")) as out: |
| 76 | try: |
| 77 | module.run() |
| 78 | except SystemExit: # this is how these terminate |
| 79 | pass |
| 80 | output = out.stdout.getvalue().splitlines() |
| 81 | |
| 82 | # Swap usage and title lines |
| 83 | output[0], output[2] = output[2], output[0] |
| 84 | |
| 85 | # Add header marking |
| 86 | for idx in (1, 0): |
| 87 | output.insert(idx, "-" * len(output[0])) |
| 88 | |
| 89 | # Add code styling for the "Usage: " line |
| 90 | for li, line in enumerate(output): |
| 91 | if line.startswith("Usage: mne "): |
| 92 | output[li] = f"Usage: ``{line[7:]}``" |
| 93 | break |
| 94 | |
| 95 | # Turn "Options:" into field list |
| 96 | if "Options:" in output: |
| 97 | ii = output.index("Options:") |
| 98 | output[ii] = "Options" |
| 99 | output.insert(ii + 1, "-------") |
| 100 | output.insert(ii + 2, "") |
| 101 | output.insert(ii + 3, ".. rst-class:: field-list cmd-list") |
| 102 | output.insert(ii + 4, "") |
| 103 | output = "\n".join(output) |
| 104 | cmd_name_space = cmd_name.replace("mne_", "mne ") |
| 105 | f.write( |
| 106 | command_rst.format(cmd_name_space, "=" * len(cmd_name_space), output) |
| 107 | ) |
| 108 | _replace_md5(str(out_fname)) |
| 109 |
no test coverage detected