Skip to content

Commit

Permalink
Refactor auto-generate api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lochhh committed Oct 24, 2024
1 parent 6ae5171 commit bbc2c48
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
39 changes: 16 additions & 23 deletions docs/make_api_index.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
"""Generate the API index page for all ``movement`` modules."""

import os
from pathlib import Path

# Modules to exclude from the API index
exclude_modules = ["cli_entrypoint"]

# Set the current working directory to the directory of this script
script_dir = os.path.dirname(os.path.abspath(__file__))
script_dir = Path(__file__).resolve().parent
os.chdir(script_dir)


def make_api_index():
"""Create a doctree of all ``movement`` modules."""
doctree = "\n"

for root, _, files in os.walk("../movement"):
# Remove leading "../"
root = root[3:]
for file in sorted(files):
if file.endswith(".py") and not file.startswith("_"):
# Convert file path to module name
module_name = os.path.join(root, file)
module_name = module_name[:-3].replace(os.sep, ".")
# Check if the module should be excluded
if not any(
file.startswith(exclude_module)
for exclude_module in exclude_modules
):
doctree += f" {module_name}\n"

doctree = ""
api_path = Path("../movement")
for path in sorted(api_path.rglob("*.py")):
if path.name.startswith("_"):
continue
# Convert file path to module name
rel_path = path.relative_to(api_path.parent)
module_name = str(rel_path.with_suffix("")).replace(os.sep, ".")
if rel_path.stem not in exclude_modules:
doctree += f" {module_name}\n"
# Get the header
with open("./source/_templates/api_index_head.rst") as f:
api_head = f.read()
api_head_path = Path("source") / "_templates" / "api_index_head.rst"
api_head = api_head_path.read_text()
# Write api_index.rst with header + doctree
with open("./source/api_index.rst", "w") as f:
f.write("..\n This file is auto-generated.\n\n")
output_path = Path("source") / "api_index.rst"
with output_path.open("w") as f:
f.write(api_head)
f.write(doctree)
print(os.path.abspath("./source/api_index.rst"))


if __name__ == "__main__":
Expand Down
3 changes: 3 additions & 0 deletions docs/source/_templates/api_index_head.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
..
This file is auto-generated.
.. _target-api:

API Reference
Expand Down

0 comments on commit bbc2c48

Please sign in to comment.