MCPcopy
hub / github.com/mne-tools/mne-python / generate_commands_rst

Function generate_commands_rst

doc/sphinxext/gen_commands.py:52–108  ·  view source on GitHub ↗
(app=None)

Source from the content-addressed store, hash-verified

50
51
52def 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

Callers 1

gen_commands.pyFile · 0.85

Calls 6

ArgvSetterClass · 0.90
_replace_md5Function · 0.90
writeMethod · 0.80
getvalueMethod · 0.80
insertMethod · 0.80
runMethod · 0.45

Tested by

no test coverage detected