|
1 | 1 | """Generate the code reference pages.""" |
2 | | - |
| 2 | +import ast |
3 | 3 | import os |
4 | 4 | from pathlib import Path |
| 5 | +from typing import Union |
5 | 6 |
|
6 | 7 | import mkdocs_gen_files |
7 | 8 |
|
|
10 | 11 | "inference.enterprise.parallel.celeryconfig", |
11 | 12 | ] |
12 | 13 |
|
| 14 | + |
| 15 | +def module_has_docstrings(path: str) -> bool: |
| 16 | + with open(path, "r", encoding="utf-8") as f: |
| 17 | + try: |
| 18 | + tree = ast.parse(f.read(), filename=path) |
| 19 | + except SyntaxError: |
| 20 | + return False # skip broken files |
| 21 | + |
| 22 | + if has_docstring(tree): |
| 23 | + return True |
| 24 | + |
| 25 | + for node in tree.body: |
| 26 | + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)): |
| 27 | + if has_docstring(node): |
| 28 | + return True |
| 29 | + return False |
| 30 | + |
| 31 | + |
| 32 | +def has_docstring(node: Union[ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Module]): |
| 33 | + return ast.get_docstring(node) is not None |
| 34 | + |
| 35 | + |
13 | 36 | if not os.environ.get("SKIP_CODEGEN"): |
14 | 37 | for package in ["inference", "inference_sdk", "inference_cli"]: |
15 | 38 | nav = mkdocs_gen_files.Nav() |
16 | 39 | src = Path(__file__).parent.parent.parent / package |
17 | 40 |
|
18 | 41 | for path in sorted(p for p in src.rglob("*.py") if "landing" not in p.parts): |
| 42 | + if not module_has_docstrings(path=path.as_posix()): |
| 43 | + continue |
19 | 44 | module_path = path.relative_to(src.parent).with_suffix("") |
20 | 45 | doc_path = path.relative_to(src.parent).with_suffix(".md") |
21 | 46 | full_doc_path = Path("reference", doc_path) |
|
41 | 66 | # print("GENERATING NAVIGATION") |
42 | 67 | # print("\n".join(lines)) |
43 | 68 | nav_file.writelines(lines) |
| 69 | + |
| 70 | + |
0 commit comments