-
Notifications
You must be signed in to change notification settings - Fork 8
Add supported-table directive that validates whether all different targets we support are documented #116
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
Add supported-table directive that validates whether all different targets we support are documented #116
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1d2f8ed
Add supported-table directive that validates whether all different ta…
Miauwkeru cb2c307
Rename supported_table to dissect_supported_table
Miauwkeru 26c529c
Add "[Dissect]" prefix to log messages
Miauwkeru d255ba9
Give the directive the `dissect-` prefix
Miauwkeru fa8b489
Resolve comments
Miauwkeru f49db0e
Revert quotes on glob
Miauwkeru 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import Any | ||
| from typing_extensions import override | ||
|
|
||
| from sphinx.application import Sphinx | ||
| from sphinx.util.logging import getLogger | ||
| from sphinx.util.console import colorize | ||
| from sphinx.addnodes import pending_xref | ||
| from docutils.parsers.rst.directives.tables import ListTable | ||
| from docutils.parsers.rst import directives | ||
| from docutils import nodes | ||
|
|
||
|
|
||
| LOGGER = getLogger(__name__) | ||
| DISSECT_PREFIX = colorize("bold", "[Dissect] ") | ||
|
|
||
|
|
||
| class SupportedTargetTable(ListTable): | ||
| """Extended list-table directive that validates if we have all our supported target modules documented.""" | ||
|
|
||
| option_spec = ListTable.option_spec.copy() | ||
| option_spec["source-path"] = directives.unchanged_required | ||
| option_spec["blacklist"] = directives.unchanged | ||
| option_spec["glob-pattern"] = directives.unchanged | ||
|
|
||
| @override | ||
| def run(self): | ||
| # Call the parent directive to get the table node | ||
| result = super().run() | ||
|
|
||
| module_references = [] | ||
| table_name = "" | ||
| # Perform custom checks on the table | ||
| if result and isinstance(result[0], nodes.table): | ||
| table_node = result[0] | ||
| table_name = table_node[0].astext() | ||
| LOGGER.info( | ||
| DISSECT_PREFIX | ||
| + colorize("darkgreen", "Gathering references from table '%s'"), | ||
| table_name, | ||
| ) | ||
| module_references = self._gather_table_references(table_node) | ||
|
|
||
| if module_references: | ||
| self.validate_references(table_name, module_references) | ||
|
|
||
| return result | ||
|
|
||
| def _gather_table_references(self, table_node: nodes.table) -> list[str]: | ||
| """Gather all the references inside the table.""" | ||
| references = [] | ||
| for ref in table_node.findall(pending_xref): | ||
| target = ref.get("reftarget") | ||
| split_names = target.rsplit(".", 2) | ||
| if target.endswith("._os"): | ||
| modname = split_names[-2] | ||
| else: | ||
| modname = split_names[-1] | ||
| references.append(modname) | ||
|
|
||
| return references | ||
|
|
||
| def validate_references(self, table_name: str, module_references: list[str]): | ||
| LOGGER.info( | ||
| DISSECT_PREFIX | ||
| + colorize("darkgreen", "Validating module references from table '%s'"), | ||
| table_name, | ||
| ) | ||
| check_path: str = self.options.get("source-path") | ||
| # Get the environment for the sphinx app | ||
| environment = self.state.document.settings.env | ||
| dissect_projects_dir = environment.config.dissect_projects_path | ||
|
|
||
| search_path = dissect_projects_dir / check_path | ||
|
|
||
| black_list = set() | ||
| black_list.update(["__init__"]) | ||
| black_list.update( | ||
| module for module in self.options.get("blacklist", "").split(",") if module | ||
| ) | ||
|
|
||
| glob = self.options.get("glob-pattern", "*.py") | ||
| for file in search_path.glob(glob): | ||
| if file.name == "_os.py": | ||
| file = file.parent | ||
|
|
||
| relative_file = file.relative_to(dissect_projects_dir) | ||
|
|
||
| if file.stem in black_list: | ||
| LOGGER.debug( | ||
| DISSECT_PREFIX + colorize("darkgrey", "Skipping %s"), relative_file | ||
| ) | ||
| continue | ||
|
Miauwkeru marked this conversation as resolved.
|
||
|
|
||
| if file.stem not in module_references: | ||
| LOGGER.warning( | ||
| DISSECT_PREFIX | ||
| + colorize( | ||
| "darkred", "Missing documentation entry for %s in table '%s'" | ||
| ), | ||
| relative_file, | ||
| table_name, | ||
| ) | ||
|
|
||
| LOGGER.info( | ||
| DISSECT_PREFIX + colorize("darkgreen", "Done validating table '%s'"), | ||
| table_name, | ||
| ) | ||
|
|
||
|
|
||
| def setup(app: Sphinx) -> dict[str, Any]: | ||
| app.add_config_value( | ||
| "dissect_projects_path", Path(__file__).parent.parent.parent.parent, "html" | ||
| ) | ||
| app.add_directive("dissect-supported-table", SupportedTargetTable) | ||
| return { | ||
| "version": "0.1", | ||
| "parallel_read_safe": True, | ||
| "parallel_write_safe": True, | ||
| } | ||
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.