|
| 1 | +""" |
| 2 | +Generate the code reference pages. |
| 3 | +Based off https://mkdocstrings.github.io/recipes/#automatic-code-reference-pages |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | + |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import mkdocs_gen_files |
| 11 | + |
| 12 | +TEST_SUITE = "test-suite/test-suite" |
| 13 | + |
| 14 | +if not os.path.isdir(TEST_SUITE): |
| 15 | + raise FileNotFoundError(f"Error: {TEST_SUITE} does not exist. Please clone the eessi/test-suite in a test-suite dir.") |
| 16 | + |
| 17 | +# build a navigation for the menu and a dictionary of navigations for each section |
| 18 | +nav = mkdocs_gen_files.Nav() |
| 19 | + |
| 20 | +# Loop through all python files in test-suite/eessi |
| 21 | +for path in sorted(Path(f"{TEST_SUITE}/eessi/").rglob("*.py")): |
| 22 | + # Get the relative filename, without .py suffix |
| 23 | + module_path = path.relative_to(TEST_SUITE).with_suffix("") |
| 24 | + |
| 25 | + # define the corresponding (relative) markdown filename |
| 26 | + doc_path = path.relative_to(TEST_SUITE).with_suffix(".md") |
| 27 | + |
| 28 | + # Specify the full corresponding markdown filename, including a testsuite_api subdir |
| 29 | + # so that we don't have these API docs scattered in the root of the EESSI docs repo |
| 30 | + full_doc_path = Path("testsuite_api/", doc_path) |
| 31 | + |
| 32 | + # If something is an __init__, use the directory name as the name of the python module instead of the filename |
| 33 | + parts = list(module_path.parts) |
| 34 | + if parts[-1] == "__init__": |
| 35 | + parts = parts[:-1] |
| 36 | + # Skip the __main__, if there is one. This is not part of the API |
| 37 | + elif parts[-1] == "__main__": |
| 38 | + continue |
| 39 | + |
| 40 | + # Add an entry for this module to the navigation |
| 41 | + nav[parts] = doc_path.as_posix() |
| 42 | + |
| 43 | + # Create the markdown file |
| 44 | + with mkdocs_gen_files.open(full_doc_path, "w") as fd: |
| 45 | + # identifier is something like eessi.foo.bar |
| 46 | + identifier = ".".join(parts) |
| 47 | + fd.write(f"::: {identifier}") |
| 48 | + |
| 49 | + # This maps the generated .md file to the source .py, so that you can have an "Edit on GitHub" button |
| 50 | + # that links to the Python code |
| 51 | + mkdocs_gen_files.set_edit_path(full_doc_path, path) |
| 52 | + |
| 53 | + # Create the api/summary.md file and write the full navigation tree into it so it can be used as a site sidebar |
| 54 | +with mkdocs_gen_files.open("testsuite_api/summary.md", "w") as nav_file: |
| 55 | + nav_file.writelines(nav.build_literate_nav()) |
0 commit comments