|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +# Define the directory where the Python source files are located |
| 5 | +src_dir = 'src/bdkpython' |
| 6 | +package_root = 'bdkpython' |
| 7 | + |
| 8 | +# Define the output file for the API documentation |
| 9 | +output_file = 'docs/api.rst' |
| 10 | + |
| 11 | +# Regex pattern to match class definitions |
| 12 | +class_pattern = re.compile(r'^class ([A-Za-z][A-Za-z0-9_]*)') |
| 13 | + |
| 14 | +# Store classes with their full module path |
| 15 | +public_classes = {} |
| 16 | + |
| 17 | +for root, _, files in os.walk(src_dir): |
| 18 | + for file in files: |
| 19 | + if file.endswith('.py'): |
| 20 | + module_path = os.path.relpath(os.path.join(root, file), src_dir) |
| 21 | + module_path = module_path.replace(os.sep, '.').removesuffix('.py') |
| 22 | + full_module = f'{package_root}.{module_path}' |
| 23 | + with open(os.path.join(root, file), 'r') as f: |
| 24 | + for line in f: |
| 25 | + match = class_pattern.match(line) |
| 26 | + if match: |
| 27 | + class_name = match.group(1) |
| 28 | + if not class_name.startswith('_'): |
| 29 | + fqcn = f'{full_module}.{class_name}' |
| 30 | + public_classes[fqcn] = True |
| 31 | + |
| 32 | +# Generate the RST content |
| 33 | +rst_content = "API Reference\n============\n\n" |
| 34 | + |
| 35 | +for fqcn in sorted(public_classes.keys()): |
| 36 | + rst_content += f".. autoclass:: {fqcn}\n" |
| 37 | + rst_content += " :members:\n" |
| 38 | + rst_content += " :undoc-members:\n" |
| 39 | + rst_content += " :show-inheritance:\n\n" |
| 40 | + |
| 41 | +# Write the RST content to the output file |
| 42 | +with open(output_file, 'w') as f: |
| 43 | + f.write(rst_content) |
| 44 | + |
| 45 | +print(f"API documentation has been generated in {output_file}") |
0 commit comments