|
1 | | -import shutil |
2 | 1 | import inspect |
3 | | -import sys |
4 | 2 | from logging import getLogger |
5 | 3 | from pathlib import Path |
6 | 4 |
|
7 | 5 | import webknossos |
8 | 6 |
|
9 | | -''' |
| 7 | +""" |
10 | 8 | This script generates a mapping of all classes in webknossos to their |
11 | 9 | corresponding MkDocs pages. It is used to generate the API reference. |
12 | | -''' |
| 10 | +""" |
13 | 11 |
|
14 | 12 | logger = getLogger(__name__) |
15 | 13 |
|
16 | 14 | OUT_PATH = Path("src/api") |
17 | 15 |
|
18 | | -for key, value in webknossos.__dict__.items(): |
19 | | - if getattr(value, "__module__", "").startswith("webknossos"): |
20 | | - |
21 | | - logger.debug("Processing module", key) |
22 | | - |
23 | | - module = value.__module__ |
24 | | - |
25 | | - module_parts = module.split('.') |
26 | | - module_name = module_parts[-1] |
27 | | - module_path = "/".join(module_parts[:-1]) |
28 | | - file_name = f"{key.lower()}.md" |
29 | | - |
30 | | - # Extract all classes from the module |
31 | | - classes = inspect.getmembers(sys.modules[module], inspect.isclass) |
32 | | - |
33 | | - # Only include classes that are in implemented in that module |
34 | | - classes = [c for c in classes if c[1].__module__ == module] |
35 | | - classes = [c for c in classes if not c[0].startswith("_")] |
36 | | - |
37 | | - # The file content uses a special syntax for MkDocs to render the |
38 | | - # docstrings as Markdown. The syntax is: |
39 | | - # ::: module.submodule.class |
40 | | - # See https://mkdocstrings.github.io/python/ |
41 | | - classes_string = "\n".join([f" - {c[0]}" for c in classes]) |
42 | | - file_content = f"""::: {module} |
| 16 | +# Initialize a dictionary to store submodules and their classes |
| 17 | +submodules_classes = {} |
| 18 | + |
| 19 | +# Iterate through all the members of the module |
| 20 | +for name, member in inspect.getmembers(webknossos): |
| 21 | + # Check if the member is a module (submodule) |
| 22 | + if inspect.ismodule(member) and member.__name__.startswith("webknossos"): |
| 23 | + submodule_name = member.__name__ |
| 24 | + |
| 25 | + # List classes in the submodule |
| 26 | + classes = inspect.getmembers(member, inspect.isclass) |
| 27 | + |
| 28 | + # Filter classes that are defined in this specific submodule |
| 29 | + defined_classes = [ |
| 30 | + cls for cls_name, cls in classes if cls.__module__ == submodule_name |
| 31 | + ] |
| 32 | + |
| 33 | + # If there are classes defined in this submodule, add them to the dictionary |
| 34 | + if defined_classes: |
| 35 | + submodules_classes[submodule_name] = defined_classes |
| 36 | + |
| 37 | +# Print the submodules and their classes |
| 38 | +for submodule, classes in submodules_classes.items(): |
| 39 | + # The file content uses a special syntax for MkDocs to render the |
| 40 | + # docstrings as Markdown. The syntax is: |
| 41 | + # ::: module.submodule.class |
| 42 | + # See https://mkdocstrings.github.io/python/ |
| 43 | + classes_string = "\n".join( |
| 44 | + [ |
| 45 | + f" - {c.__name__}" |
| 46 | + for c in classes |
| 47 | + if not c.__name__.startswith("_") |
| 48 | + ] |
| 49 | + ) |
| 50 | + file_content = f"""::: {submodule} |
43 | 51 | options: |
44 | 52 | members:\n{classes_string}\n""" |
45 | | - |
46 | | - out_path=OUT_PATH.joinpath(module_path) |
47 | | - out_path.mkdir(exist_ok=True, parents=True) |
48 | 53 |
|
49 | | - logger.debug(f"Writing API docs to {out_path.joinpath(file_name)}") |
50 | | - out_path.joinpath(file_name).write_text(file_content) |
| 54 | + submodule_parts = submodule.split(".") |
| 55 | + submodule_name = submodule_parts[-1] |
| 56 | + file_name = Path(f"{submodule_name}.md") |
| 57 | + file_path = "/".join(submodule_parts[:-1]) |
| 58 | + |
| 59 | + out_path = OUT_PATH.joinpath(file_path) |
| 60 | + out_path.mkdir(exist_ok=True, parents=True) |
| 61 | + |
| 62 | + logger.debug(f"Writing API docs to {out_path.joinpath(file_name)}") |
| 63 | + out_path.joinpath(file_name).write_text(file_content) |
0 commit comments