-
-
Notifications
You must be signed in to change notification settings - Fork 108
v2 doc and demo updates #384
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c84c157
docs
pavel-kirienko 5e10b4b
docs
pavel-kirienko e92777a
remap docs
pavel-kirienko bc541e4
Add Topic.evictions and Topic.subject_id()
pavel-kirienko eec5878
Add Node.transport
pavel-kirienko e9a6459
update the monitor
pavel-kirienko 55f1789
Expose examples in the docs
pavel-kirienko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,97 @@ | ||
| #!/usr/bin/env python | ||
| """Build API docs using pdoc. Invoked via ``nox -s docs``.""" | ||
|
|
||
| import ast | ||
| import shutil | ||
| from pathlib import Path | ||
| import pkgutil | ||
| import importlib | ||
| import sys | ||
|
|
||
| import pdoc | ||
| import pycyphal2 | ||
|
|
||
| # Discover and import all public submodules so pdoc can see them, | ||
| # then inject them into their parent's __all__ so pdoc lists them in the sidebar. | ||
| # Public modules are expected to be importable in the docs environment; failures are treated as hard errors. | ||
| for mi in pkgutil.walk_packages(pycyphal2.__path__, pycyphal2.__name__ + "."): | ||
| leaf = mi.name.rsplit(".", 1)[-1] | ||
| if leaf.startswith("_"): | ||
| continue | ||
| OUTPUT_DIRECTORY = Path("html_docs") | ||
| EXAMPLES_DIRECTORY = Path("examples") | ||
|
|
||
|
|
||
| def _discover_examples(directory: Path) -> list[Path]: | ||
| if not directory.is_dir(): | ||
| raise RuntimeError(f"Examples directory {directory!s} not found while building docs") | ||
| examples = sorted(path for path in directory.rglob("*.py") if path.is_file()) | ||
| if not examples: | ||
| raise RuntimeError(f"No example scripts found under {directory!s} while building docs") | ||
| return examples | ||
|
|
||
|
|
||
| def _load_summary(path: Path) -> str: | ||
| try: | ||
| importlib.import_module(mi.name) | ||
| except Exception as ex: | ||
| raise RuntimeError(f"Failed to import public module {mi.name!r} while building docs") from ex | ||
| parent = sys.modules[mi.name.rsplit(".", 1)[0]] | ||
| if hasattr(parent, "__all__") and leaf not in parent.__all__: | ||
| parent.__all__.append(leaf) | ||
| module = ast.parse(path.read_text(encoding="utf8"), filename=str(path)) | ||
| except SyntaxError as ex: | ||
| raise RuntimeError(f"Failed to parse example {path!s} while building docs") from ex | ||
| doc = ast.get_docstring(module, clean=True) | ||
| if not doc: | ||
| return "" | ||
| for line in doc.splitlines(): | ||
| text = line.strip() | ||
| if text and not text.startswith("Usage:"): | ||
| return text | ||
| return "" | ||
|
|
||
|
|
||
| def _make_examples_section(examples: list[Path]) -> str: | ||
| if not examples: | ||
| return "" | ||
| lines = ["## Examples", "", "Runnable examples:"] | ||
| for path in examples: | ||
| relative = path.relative_to(EXAMPLES_DIRECTORY).as_posix() | ||
| summary = _load_summary(path) | ||
| suffix = f" - {summary}" if summary else "" | ||
| lines.append(f"- [`examples/{relative}`](examples/{relative}){suffix}") | ||
| return "\n".join(lines) + "\n" | ||
|
|
||
|
|
||
| def _inject_examples_section(examples: list[Path]) -> None: | ||
| section = _make_examples_section(examples) | ||
| doc = pycyphal2.__doc__ or "" | ||
| pycyphal2.__doc__ = doc.rstrip() + f"\n\n{section}" | ||
|
|
||
|
|
||
| def _copy_examples(examples_source: Path, output_directory: Path, examples: list[Path]) -> None: | ||
| destination = output_directory / examples_source.name | ||
| shutil.rmtree(destination, ignore_errors=True) | ||
| if not examples: | ||
| return | ||
| for source in examples: | ||
| target = destination / source.relative_to(examples_source) | ||
| target.parent.mkdir(parents=True, exist_ok=True) | ||
| shutil.copy2(source, target) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| # Discover and import all public submodules so pdoc can see them, | ||
| # then inject them into their parent's __all__ so pdoc lists them in the sidebar. | ||
| # Public modules are expected to be importable in the docs environment; failures are treated as hard errors. | ||
| for mi in pkgutil.walk_packages(pycyphal2.__path__, pycyphal2.__name__ + "."): | ||
| leaf = mi.name.rsplit(".", 1)[-1] | ||
| if leaf.startswith("_"): | ||
| continue | ||
| try: | ||
| importlib.import_module(mi.name) | ||
| except Exception as ex: | ||
| raise RuntimeError(f"Failed to import public module {mi.name!r} while building docs") from ex | ||
| parent = sys.modules[mi.name.rsplit(".", 1)[0]] | ||
| if hasattr(parent, "__all__") and leaf not in parent.__all__: | ||
| parent.__all__.append(leaf) | ||
|
|
||
| # Customization is necessary to expose special members like __aiter__, __call__, etc. | ||
| # We also use it to tweak the colors. | ||
| pdoc.render.configure(template_directory=Path(__file__).resolve().with_name("pdoc")) | ||
| examples = _discover_examples(EXAMPLES_DIRECTORY) | ||
| _inject_examples_section(examples) | ||
| pdoc.pdoc("pycyphal2", output_directory=OUTPUT_DIRECTORY) | ||
| _copy_examples(EXAMPLES_DIRECTORY, OUTPUT_DIRECTORY, examples) | ||
|
|
||
| import pdoc | ||
|
|
||
| # Customization is necessary to expose special members like __aiter__, __call__, etc. | ||
| # We also use it to tweak the colors. | ||
| pdoc.render.configure(template_directory=Path(__file__).resolve().with_name("pdoc")) | ||
| pdoc.pdoc("pycyphal2", output_directory=Path("html_docs")) | ||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,7 @@ | |
| --def: #FC6D09; | ||
| --annotation: #7aa2ff; | ||
| } | ||
|
|
||
| main.pdoc .docstring h3 { | ||
| font-size: 1.2rem; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.