gaspype/docs/source/generate_class_list.py

87 lines
2.9 KiB
Python
Raw Normal View History

2025-06-06 07:04:42 +00:00
# This script generates the source md-files for all classes and functions for the docs
2025-05-22 15:55:12 +00:00
import importlib
import inspect
import fnmatch
from io import TextIOWrapper
import os
2025-05-22 15:55:12 +00:00
2025-06-06 08:12:28 +00:00
def write_manual(f: TextIOWrapper, doc_files: list[str], title: str) -> None:
write_dochtree(f, title, doc_files)
2025-05-22 15:55:12 +00:00
2025-06-06 08:12:28 +00:00
def write_classes(f: TextIOWrapper, patterns: list[str], module_name: str, title: str, description: str = '', exclude: list[str] = []) -> None:
"""Write the classes to the file."""
2025-05-22 15:55:12 +00:00
module = importlib.import_module(module_name)
classes = [
name for name, obj in inspect.getmembers(module, inspect.isclass)
if (any(fnmatch.fnmatch(name, pat) for pat in patterns if pat not in exclude) and
2025-05-22 15:55:12 +00:00
obj.__doc__ and '(Automatic generated stub)' not in obj.__doc__)
]
if description:
f.write(f'{description}\n\n')
2025-06-06 07:04:42 +00:00
write_dochtree(f, title, classes)
2025-05-22 15:55:12 +00:00
for cls in classes:
2025-07-28 14:21:16 +00:00
with open(f'docs/source/api/{cls}.md', 'w') as f2:
2025-06-06 07:04:42 +00:00
f2.write(f'# {module_name}.{cls}\n')
f2.write('```{eval-rst}\n')
f2.write(f'.. autoclass:: {module_name}.{cls}\n')
f2.write(' :members:\n')
f2.write(' :undoc-members:\n')
f2.write(' :show-inheritance:\n')
f2.write(' :inherited-members:\n')
f2.write('```\n\n')
2025-05-22 15:55:12 +00:00
def write_functions(f: TextIOWrapper, patterns: list[str], module_name: str, title: str, description: str = '', exclude: list[str] = []) -> None:
2025-06-06 08:12:28 +00:00
"""Write the classes to the file."""
2025-05-22 15:55:12 +00:00
module = importlib.import_module(module_name)
functions = [
2025-07-28 14:21:16 +00:00
name for name, _ in inspect.getmembers(module, inspect.isfunction)
if (any(fnmatch.fnmatch(name, pat) for pat in patterns if pat not in exclude))
2025-05-22 15:55:12 +00:00
]
if description:
f.write(f'{description}\n\n')
2025-06-06 07:04:42 +00:00
write_dochtree(f, title, functions)
2025-05-22 15:55:12 +00:00
for func in functions:
if not func.startswith('_'):
2025-07-28 14:21:16 +00:00
with open(f'docs/source/api/{func}.md', 'w') as f2:
2025-06-06 07:04:42 +00:00
f2.write(f'# {module_name}.{func}\n')
f2.write('```{eval-rst}\n')
f2.write(f'.. autofunction:: {module_name}.{func}\n')
f2.write('```\n\n')
def write_dochtree(f: TextIOWrapper, title: str, items: list[str]):
f.write('```{toctree}\n')
f.write(':maxdepth: 1\n')
f.write(f':caption: {title}:\n')
#f.write(':hidden:\n')
for text in items:
if not text.startswith('_'):
f.write(f"{text}\n")
f.write('```\n\n')
2025-05-22 15:55:12 +00:00
2025-06-06 07:04:42 +00:00
if __name__ == "__main__":
# Ensure the output directory exists
2025-07-28 14:21:16 +00:00
os.makedirs('docs/source/api', exist_ok=True)
2025-07-28 14:21:16 +00:00
with open('docs/source/api/index.md', 'w') as f:
2025-06-06 07:04:42 +00:00
f.write('# Classes and functions\n\n')
2025-06-06 08:12:28 +00:00
2025-06-06 07:04:42 +00:00
write_classes(f, ['*'], 'gaspype', title='Classes')
2025-05-22 15:55:12 +00:00
2025-06-06 07:04:42 +00:00
write_functions(f, ['*'], 'gaspype', title='Functions')
2025-06-06 08:12:28 +00:00
write_manual(f, ['../ndfloat', '../floatarray'], title='Types')