|
| 1 | +import os |
| 2 | +import textwrap |
| 3 | +from typing import Dict, List, Optional |
| 4 | + |
| 5 | + |
| 6 | +def _write_doc_folder( |
| 7 | + folder: str, |
| 8 | + pyfiles: List[str], |
| 9 | + hidden: bool = False, |
| 10 | + prefix: str = "", |
| 11 | + subfolders: Optional[List[str]] = None, |
| 12 | +) -> Dict[str, str]: |
| 13 | + """ |
| 14 | + Creates all the file in a dictionary. |
| 15 | + """ |
| 16 | + template = textwrap.dedent( |
| 17 | + """ |
| 18 | + <full_module_name> |
| 19 | + <line> |
| 20 | +
|
| 21 | + .. automodule:: <full_module_name> |
| 22 | + :members: |
| 23 | + :no-undoc-members: |
| 24 | + """ |
| 25 | + ) |
| 26 | + |
| 27 | + index = textwrap.dedent( |
| 28 | + """ |
| 29 | + <full_module_name> |
| 30 | + <line> |
| 31 | + """ |
| 32 | + ) |
| 33 | + |
| 34 | + submodule = ".".join(os.path.splitext(folder)[0].replace("\\", "/").split("/")) |
| 35 | + fullsubmodule = f"{prefix}.{submodule}" if prefix else submodule |
| 36 | + rows = [ |
| 37 | + index.replace("<submodule>", submodule) |
| 38 | + .replace("<full_module_name>", fullsubmodule) |
| 39 | + .replace("<line>", "=" * len(fullsubmodule)), |
| 40 | + ] |
| 41 | + if subfolders: |
| 42 | + rows.append( |
| 43 | + textwrap.dedent( |
| 44 | + """ |
| 45 | + .. toctree:: |
| 46 | + :maxdepth: 1 |
| 47 | + :caption: submodules |
| 48 | + |
| 49 | + """ |
| 50 | + ) |
| 51 | + ) |
| 52 | + for sub in subfolders: |
| 53 | + rows.append(f" {sub}/index") |
| 54 | + res = {} |
| 55 | + has_module = False |
| 56 | + for name in sorted(pyfiles): |
| 57 | + if not name: |
| 58 | + continue |
| 59 | + module_name = ".".join(os.path.splitext(name)[0].replace("\\", "/").split("/")) |
| 60 | + last = module_name.split(".")[-1] |
| 61 | + if not hidden and last[0] == "_" and last != "__init__": |
| 62 | + continue |
| 63 | + if not module_name or module_name in ("__main__", "__init__"): |
| 64 | + continue |
| 65 | + key = f"{module_name}.rst" |
| 66 | + if module_name.endswith("__init__"): |
| 67 | + module_name = ".".join(module_name.split(".")[:-1]) |
| 68 | + full_module_name = f"{submodule}.{module_name}" |
| 69 | + line = "=" * len(full_module_name) |
| 70 | + text = ( |
| 71 | + template.replace("<module_name>", module_name) |
| 72 | + .replace("<full_module_name>", full_module_name) |
| 73 | + .replace("<line>", line) |
| 74 | + ) |
| 75 | + res[key] = text |
| 76 | + if not has_module: |
| 77 | + has_module = True |
| 78 | + rows.append( |
| 79 | + textwrap.dedent( |
| 80 | + """ |
| 81 | +
|
| 82 | + .. toctree:: |
| 83 | + :maxdepth: 1 |
| 84 | + :caption: modules |
| 85 | +
|
| 86 | + """ |
| 87 | + ) |
| 88 | + ) |
| 89 | + rows.append(f" {last}") |
| 90 | + |
| 91 | + rows.append( |
| 92 | + textwrap.dedent( |
| 93 | + f""" |
| 94 | +
|
| 95 | + .. automodule:: {submodule} |
| 96 | + :members: |
| 97 | + :no-undoc-members: |
| 98 | + """ |
| 99 | + ) |
| 100 | + ) |
| 101 | + res["index.rst"] = "\n".join(rows) |
| 102 | + return res |
| 103 | + |
| 104 | + |
| 105 | +def sphinx_api( |
| 106 | + folder: str, |
| 107 | + output_folder: Optional[str] = None, |
| 108 | + simulate: bool = False, |
| 109 | + hidden: bool = False, |
| 110 | + verbose: int = 0, |
| 111 | +): |
| 112 | + """ |
| 113 | + Creates simple pages to document a package. |
| 114 | + Relies on :epkg:`automodule`. |
| 115 | +
|
| 116 | + :param folder: folder to document |
| 117 | + :param output_folder: where to write the result |
| 118 | + :param simulate: prints out what the function will do |
| 119 | + :param hidden: document file starting with `_` |
| 120 | + :param verbose: verbosity |
| 121 | + :return: list of written file |
| 122 | + """ |
| 123 | + folder = folder.rstrip("/\\") |
| 124 | + root, package_name = os.path.split(folder) |
| 125 | + files = [] |
| 126 | + if verbose: |
| 127 | + print(f"[sphinx_api] start creating API for {folder!r}") |
| 128 | + |
| 129 | + for racine, dossiers, fichiers in os.walk(folder): |
| 130 | + pyfiles = [f for f in fichiers if f.endswith(".py")] |
| 131 | + if not pyfiles: |
| 132 | + continue |
| 133 | + mname = racine[len(root) + 1 :] if root else racine |
| 134 | + selected = [ |
| 135 | + d |
| 136 | + for d in dossiers |
| 137 | + if os.path.exists(os.path.join(racine, d, "__init__.py")) |
| 138 | + ] |
| 139 | + if verbose: |
| 140 | + print(f"[sphinx_api] open {mname!r}") |
| 141 | + if selected: |
| 142 | + print(f"[sphinx_api] submodules {selected!r}") |
| 143 | + content = _write_doc_folder( |
| 144 | + mname, pyfiles, hidden=hidden, prefix="", subfolders=selected |
| 145 | + ) |
| 146 | + if verbose: |
| 147 | + print(f"[sphinx_api] close {mname!r}") |
| 148 | + if simulate: |
| 149 | + print(f"--+ {mname}") |
| 150 | + for k, v in content.items(): |
| 151 | + print(f" | {k}") |
| 152 | + else: |
| 153 | + assert output_folder, "output_folder is empty" |
| 154 | + subfolder = os.path.join(output_folder, *mname.split("/")[1:]) |
| 155 | + if verbose: |
| 156 | + print(f"[sphinx_api] create {subfolder!r}") |
| 157 | + if not os.path.exists(subfolder): |
| 158 | + os.makedirs(subfolder) |
| 159 | + for k, v in content.items(): |
| 160 | + n = os.path.join(subfolder, k) |
| 161 | + if verbose: |
| 162 | + print(f"[sphinx_api] write {n!r}") |
| 163 | + with open(n, "w") as f: |
| 164 | + f.write(v) |
| 165 | + files.append(n) |
| 166 | + return files |
0 commit comments