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
|
2025-06-06 11:36:55 +00:00
|
|
|
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 (obj.__module__ == module_name and
|
|
|
|
any(fnmatch.fnmatch(name, pat) for pat in patterns if pat not in exclude) and
|
|
|
|
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-06-06 07:04:42 +00:00
|
|
|
with open(f'docs/source/_autogenerated/{cls}.md', 'w') as f2:
|
|
|
|
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 = [
|
|
|
|
name for name, obj in inspect.getmembers(module, inspect.isfunction)
|
|
|
|
if (obj.__module__ == module_name and
|
|
|
|
any(fnmatch.fnmatch(name, pat) for pat in patterns if pat not in exclude))
|
|
|
|
]
|
|
|
|
|
|
|
|
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-06-06 07:04:42 +00:00
|
|
|
with open(f'docs/source/_autogenerated/{func}.md', 'w') as f2:
|
|
|
|
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__":
|
2025-06-06 11:36:55 +00:00
|
|
|
# Ensure the output directory exists
|
|
|
|
os.makedirs('docs/source/_autogenerated', exist_ok=True)
|
|
|
|
|
2025-06-06 07:04:42 +00:00
|
|
|
with open('docs/source/_autogenerated/index.md', 'w') as f:
|
|
|
|
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')
|