|
| 1 | +"""Export important utility functions with docstrings to a markdown reference file. |
| 2 | +
|
| 3 | +Scans specified Python source files for public functions with docstrings and |
| 4 | +writes a categorized markdown reference to an output file. Only functions |
| 5 | +with a module-level docstring are included; undocumented functions are skipped. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import ast |
| 12 | +import sys |
| 13 | +from dataclasses import dataclass, field |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class _Entry: |
| 19 | + module: str |
| 20 | + file: str |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class _Section: |
| 25 | + heading: str |
| 26 | + entries: list[_Entry] = field(default_factory=list) |
| 27 | + |
| 28 | + |
| 29 | +# The categories of utility functions to include in the reference. |
| 30 | +# Each section maps a heading to a list of (module, file) entries. |
| 31 | +# Only public functions with a docstring in these files are included. |
| 32 | +SECTIONS: list[_Section] = [ |
| 33 | + _Section( |
| 34 | + heading="### Dependency Management", |
| 35 | + entries=[ |
| 36 | + _Entry(module="usethis._deps", file="src/usethis/_deps.py"), |
| 37 | + ], |
| 38 | + ), |
| 39 | + _Section( |
| 40 | + heading="### Console Output", |
| 41 | + entries=[ |
| 42 | + _Entry(module="usethis._console", file="src/usethis/_console.py"), |
| 43 | + ], |
| 44 | + ), |
| 45 | + _Section( |
| 46 | + heading="### Tool and Feature Detection", |
| 47 | + entries=[ |
| 48 | + _Entry( |
| 49 | + module="usethis._detect.pre_commit", |
| 50 | + file="src/usethis/_detect/pre_commit.py", |
| 51 | + ), |
| 52 | + _Entry( |
| 53 | + module="usethis._detect.readme", |
| 54 | + file="src/usethis/_detect/readme.py", |
| 55 | + ), |
| 56 | + _Entry( |
| 57 | + module="usethis._integrations.project.build", |
| 58 | + file="src/usethis/_integrations/project/build.py", |
| 59 | + ), |
| 60 | + ], |
| 61 | + ), |
| 62 | + _Section( |
| 63 | + heading="### Project Metadata", |
| 64 | + entries=[ |
| 65 | + _Entry( |
| 66 | + module="usethis._integrations.project.name", |
| 67 | + file="src/usethis/_integrations/project/name.py", |
| 68 | + ), |
| 69 | + _Entry( |
| 70 | + module="usethis._integrations.project.packages", |
| 71 | + file="src/usethis/_integrations/project/packages.py", |
| 72 | + ), |
| 73 | + _Entry( |
| 74 | + module="usethis._integrations.project.layout", |
| 75 | + file="src/usethis/_integrations/project/layout.py", |
| 76 | + ), |
| 77 | + _Entry( |
| 78 | + module="usethis._file.pyproject_toml.requires_python", |
| 79 | + file="src/usethis/_file/pyproject_toml/requires_python.py", |
| 80 | + ), |
| 81 | + _Entry( |
| 82 | + module="usethis._file.pyproject_toml.name", |
| 83 | + file="src/usethis/_file/pyproject_toml/name.py", |
| 84 | + ), |
| 85 | + ], |
| 86 | + ), |
| 87 | + _Section( |
| 88 | + heading="### Backend Dispatch", |
| 89 | + entries=[ |
| 90 | + _Entry( |
| 91 | + module="usethis._backend.dispatch", |
| 92 | + file="src/usethis/_backend/dispatch.py", |
| 93 | + ), |
| 94 | + ], |
| 95 | + ), |
| 96 | +] |
| 97 | + |
| 98 | + |
| 99 | +def _get_public_functions(path: Path) -> list[tuple[str, str]]: |
| 100 | + """Return (name, first_docstring_line) for each public function in the file. |
| 101 | +
|
| 102 | + Functions without a docstring are excluded. |
| 103 | + """ |
| 104 | + try: |
| 105 | + source = path.read_text(encoding="utf-8") |
| 106 | + except (OSError, UnicodeDecodeError) as exc: |
| 107 | + print(f"ERROR: Cannot read {path}: {exc}", file=sys.stderr) |
| 108 | + return [] |
| 109 | + |
| 110 | + try: |
| 111 | + tree = ast.parse(source) |
| 112 | + except SyntaxError as exc: |
| 113 | + print(f"ERROR: Cannot parse {path}: {exc}", file=sys.stderr) |
| 114 | + return [] |
| 115 | + |
| 116 | + results: list[tuple[str, str]] = [] |
| 117 | + for node in ast.walk(tree): |
| 118 | + if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): |
| 119 | + continue |
| 120 | + if node.name.startswith("_"): |
| 121 | + continue |
| 122 | + docstring = ast.get_docstring(node) |
| 123 | + if docstring is None: |
| 124 | + continue |
| 125 | + first_line = docstring.split("\n")[0].strip() |
| 126 | + if first_line: |
| 127 | + results.append((node.name, first_line)) |
| 128 | + |
| 129 | + return results |
| 130 | + |
| 131 | + |
| 132 | +def main() -> int: |
| 133 | + parser = argparse.ArgumentParser( |
| 134 | + description="Export utility function reference to a markdown file.", |
| 135 | + ) |
| 136 | + parser.add_argument( |
| 137 | + "--output-file", |
| 138 | + required=True, |
| 139 | + help="Path to the output markdown file to write.", |
| 140 | + ) |
| 141 | + args = parser.parse_args() |
| 142 | + |
| 143 | + output_file = Path(args.output_file) |
| 144 | + |
| 145 | + sections_output: list[str] = [] |
| 146 | + failed = False |
| 147 | + |
| 148 | + for section in SECTIONS: |
| 149 | + bullets: list[str] = [] |
| 150 | + |
| 151 | + for entry in section.entries: |
| 152 | + source_path = Path(entry.file) |
| 153 | + if not source_path.is_file(): |
| 154 | + print( |
| 155 | + f"ERROR: Source file {source_path} not found.", |
| 156 | + file=sys.stderr, |
| 157 | + ) |
| 158 | + failed = True |
| 159 | + continue |
| 160 | + |
| 161 | + for func_name, first_line in _get_public_functions(source_path): |
| 162 | + bullets.append(f"- `{func_name}()` (`{entry.module}`) — {first_line}") |
| 163 | + |
| 164 | + if bullets: |
| 165 | + if sections_output: |
| 166 | + sections_output.append("") |
| 167 | + sections_output.append(section.heading) |
| 168 | + sections_output.append("") |
| 169 | + sections_output.extend(bullets) |
| 170 | + |
| 171 | + content = "\n".join(sections_output) + "\n" |
| 172 | + |
| 173 | + output_file.parent.mkdir(parents=True, exist_ok=True) |
| 174 | + output_file.write_text(content, encoding="utf-8") |
| 175 | + |
| 176 | + print(f"Function reference written to {output_file}.") |
| 177 | + |
| 178 | + if failed: |
| 179 | + return 1 |
| 180 | + |
| 181 | + return 0 |
| 182 | + |
| 183 | + |
| 184 | +if __name__ == "__main__": |
| 185 | + raise SystemExit(main()) |
0 commit comments