Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Properly sort modules when building the API documentation #231

Merged
merged 7 commits into from
Jun 6, 2024
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Things to be included in the next release go here.
- Renamed some of the abstract base classes to separate them from the actual device driver classes.
- Updated the documentation templates to use the new jinja template style that `mkdocstrings-python` is switching to.

### Fixed

- Fixed the API documentation to properly sort all the modules so that the Table of Contents is readable.

---

## v1.4.2 (2024-05-09)
Expand Down
41 changes: 31 additions & 10 deletions docs/generate_api_pages.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Generate the code reference pages and navigation."""

from pathlib import Path
from typing import Tuple

import mkdocs_gen_files

Expand All @@ -11,7 +12,27 @@
root = Path(__file__).parent.parent
src = root / "src"

for path in src.rglob("*.py"):

def sort_paths(path_object: Path) -> Tuple[int, str]:
"""A helper function to provide a way to sort a list of Path objects.

This allows for sorting based on the name, but places modules above subpackages.

Args:
path_object: A Path object representing a Python module.

Returns:
The key to use in the sorting method/function.
"""
# Count the number of parts in the path minus 1 (to ignore the file itself)
# This effectively gives us the "depth" of the file
depth = len(path_object.parts) - 1
# Sort by depth first, then alphabetically
return depth, path_object.as_posix().lower()


file_list = sorted(src.rglob("*.py"), key=sort_paths)
for path in file_list:
module_path = path.relative_to(src).with_suffix("")
doc_path = path.relative_to(src).with_suffix(".md")
full_doc_path = Path("reference", doc_path)
Expand Down Expand Up @@ -42,24 +63,24 @@
("helpers", "enums"),
("helpers", "__init__"),
}:
fd.write(" options:\n show_if_no_docstring: true\n")
fd.write(" options:\n show_if_no_docstring: true\n")
if module_path.parts[-2:] == ("commands", "__init__"):
fd.write(
" options:\n"
" inherited_members: false\n"
" filters: ['!^_', '^__init__', '^gen_']\n"
" members_order: source\n"
" merge_init_into_class: false\n"
" inherited_members: false\n"
" filters: ['!^_', '^__init__', '^gen_']\n"
" members_order: source\n"
" merge_init_into_class: false\n"
)
elif module_path.parts[1] == "commands":
fd.write(
" options:\n"
" inherited_members: false\n"
" merge_init_into_class: false\n"
" filters: ['!^_']\n"
" inherited_members: false\n"
" merge_init_into_class: false\n"
" filters: ['!^_']\n"
)
if module_path.parts[-2:] == ("drivers", "__init__"):
fd.write(" options:\n members_order: source\n\n")
fd.write(" options:\n members_order: source\n\n")

mkdocs_gen_files.set_edit_path(full_doc_path, path.relative_to(root))

Expand Down
Loading