-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Handled overloaded methods correctly #178
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a1b1477
Make custom documenter class which handles SIP overloaded methods
nyalldawson 267c306
Not very nice hack to force default documenter when generating table …
nyalldawson 01e31ef
Fix building some classes
nyalldawson 7cfc064
Gracefully handle missing class_map yml files
nyalldawson fc27a88
Remove old class map download code
nyalldawson 09c1acc
Move documenter to seperate file
nyalldawson 73a9fa3
fixup
nyalldawson e3de843
Simplify logic
nyalldawson cb1078a
Regex should not match functions in examples
nyalldawson 13a038b
Gross hack no longer required
nyalldawson 7bb58e9
Revert "Remove old class map download code"
nyalldawson 8f99381
Download class map files to user-writable location
nyalldawson 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 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 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 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,85 @@ | ||
import inspect | ||
import re | ||
|
||
from sphinx.ext.autodoc import MethodDocumenter | ||
|
||
|
||
class OverloadedPythonMethodDocumenter(MethodDocumenter): | ||
""" | ||
A method documenter which handles overloaded methods, via processing | ||
the docstrings generated by SIP | ||
""" | ||
|
||
objtype = "method" | ||
priority = MethodDocumenter.priority | ||
|
||
@classmethod | ||
def can_document_member(cls, member, membername, isattr, parent): | ||
return MethodDocumenter.can_document_member(member, membername, isattr, parent) | ||
|
||
def parse_signature_blocks(self, docstring): | ||
""" | ||
Extracts each signature from a sip generated docstring, and | ||
returns each signature in a tuple with the docs for just | ||
that signature. | ||
""" | ||
res = [] | ||
current_sig = "" | ||
current_desc = "" | ||
for line in docstring.split("\n"): | ||
match = re.match(r"^\w+(\([^)]*\)(?:\s*->\s*[^:\n]+)?)\s*((?:(?!\w+\().)*)\s*$", line) | ||
if match: | ||
if current_sig: | ||
res.append((current_sig, current_desc)) | ||
current_sig = match.group(1) | ||
current_desc = match.group(2) | ||
if current_desc: | ||
current_desc += "\n" | ||
else: | ||
current_desc += line + "\n" | ||
|
||
if current_sig: | ||
res.append((current_sig, current_desc)) | ||
|
||
return res | ||
|
||
def add_content(self, more_content): | ||
""" | ||
Parse the docstring to get all signatures and their descriptions | ||
""" | ||
sourcename = self.get_sourcename() | ||
docstring = inspect.getdoc(self.object) | ||
if docstring: | ||
# does this method have multiple overrides? | ||
signature_blocks = self.parse_signature_blocks(docstring) | ||
|
||
if len(signature_blocks) <= 1: | ||
# nope, just use standard formatter then! | ||
super().add_content(more_content) | ||
return | ||
|
||
# add a method output for EVERY override | ||
for i, (signature, description) in enumerate(signature_blocks): | ||
if i > 0: | ||
self.add_line("", sourcename) | ||
|
||
# this pattern is used in the autodoc source! | ||
old_indent = self.indent | ||
new_indent = ( | ||
" " | ||
* len(self.content_indent) | ||
* (len(self.indent) // len(self.content_indent) - 1) | ||
) | ||
# skip the signature for the first overload. This will already | ||
# have been included by the base class Documenter logic! | ||
if i > 0: | ||
self.indent = new_indent | ||
self.add_directive_header(signature) | ||
self.indent = old_indent | ||
# we can only index the first signature! | ||
self.add_line(":no-index:", sourcename) | ||
self.add_line("", sourcename) | ||
|
||
doc_for_this_override = self.object_name + signature + "\n" + description | ||
for line in self.process_doc([doc_for_this_override.split("\n")]): | ||
self.add_line(line, sourcename) |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will be silently skipped, should we at least raise a warning?
I'm a bit concerned that for whatever reason, there is no more class_map file from QGIS and the docs are built without error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I've reverted that bit, but have revised the class map download to use a user-writable location instead.