-
-
Notifications
You must be signed in to change notification settings - Fork 990
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added code references to search results.
- Loading branch information
1 parent
8e40ddc
commit 34353c8
Showing
10 changed files
with
349 additions
and
35 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from functools import cached_property | ||
|
||
from sphinxcontrib.serializinghtml import JSONHTMLBuilder | ||
|
||
|
||
class PythonObjectsJSONHTMLBuilder(JSONHTMLBuilder): | ||
name = "pyjson" | ||
|
||
@cached_property | ||
def domain_objects(self): | ||
domain = self.env.get_domain("py") | ||
return [item for item in domain.get_objects() if item[2] != "module"] | ||
|
||
def get_doc_context(self, docname, body, metatags): | ||
out_dict = super().get_doc_context(docname, body, metatags) | ||
python_objects = self.get_python_objects(docname) | ||
out_dict["python_objects"] = python_objects | ||
out_dict["python_objects_search"] = " ".join( | ||
# Keeps the code suffix to improve the search results for terms such as | ||
# "select" for QuerySet.select_related. | ||
[key.split(".")[-1] for key in python_objects.keys()] | ||
) | ||
return out_dict | ||
|
||
def get_python_objects(self, docname): | ||
entries = {} | ||
for name, _, _, obj_docname, _, _ in self.domain_objects: | ||
if obj_docname == docname: | ||
code_path = name.split(".")[-2:] | ||
if code_path[0][0].isupper(): | ||
short_name = ".".join(code_path) | ||
else: | ||
short_name = code_path[-1] | ||
entries[short_name] = name | ||
return entries | ||
|
||
|
||
def setup(app): | ||
app.add_builder(PythonObjectsJSONHTMLBuilder) |
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
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,63 @@ | ||
from unittest.mock import Mock, patch | ||
|
||
from django.test import SimpleTestCase | ||
|
||
from ..builder import PythonObjectsJSONHTMLBuilder | ||
|
||
|
||
class TestPythonObjectsJSONHTMLBuilder(SimpleTestCase): | ||
def setUp(self): | ||
self.app = Mock() | ||
self.env = Mock() | ||
self.app.doctreedir = "/tmp" | ||
self.env.get_domain = Mock() | ||
self.mock_domain = Mock() | ||
self.env.get_domain.return_value = self.mock_domain | ||
self.builder = PythonObjectsJSONHTMLBuilder(self.app, self.env) | ||
|
||
def test_domain_objects_excludes_modules(self): | ||
self.mock_domain.get_objects.return_value = [ | ||
("module1", "module1", "module", "doc1", "", ""), | ||
("ClassA", "ClassA", "class", "doc2", "", ""), | ||
("function_b", "function_b", "function", "doc2", "", ""), | ||
] | ||
|
||
expected_objects = [ | ||
("ClassA", "ClassA", "class", "doc2", "", ""), | ||
("function_b", "function_b", "function", "doc2", "", ""), | ||
] | ||
self.assertEqual(self.builder.domain_objects, expected_objects) | ||
|
||
def test_get_python_objects(self): | ||
self.mock_domain.get_objects.return_value = [ | ||
( | ||
"module1.ClassA.method", | ||
"module1.ClassA.method", | ||
"method", | ||
"doc1", | ||
"", | ||
"", | ||
), | ||
("module1.ClassA", "module1.ClassA", "class", "doc1", "", ""), | ||
("module1.function_b", "module1.function_b", "function", "doc1", "", ""), | ||
] | ||
expected_result = { | ||
"ClassA": "module1.ClassA", | ||
"ClassA.method": "module1.ClassA.method", | ||
"function_b": "module1.function_b", | ||
} | ||
self.assertEqual(self.builder.get_python_objects("doc1"), expected_result) | ||
|
||
@patch("docs.builder.JSONHTMLBuilder.get_doc_context") | ||
def test_get_doc_context(self, mock_super_get_doc_context): | ||
mock_super_get_doc_context.return_value = {} | ||
self.mock_domain.get_objects.return_value = [ | ||
("module1", "module1", "module", "doc1", "", ""), | ||
("module1.ClassA", "module1.ClassA", "class", "doc1", "", ""), | ||
("function_b", "function_b", "function", "doc2", "", ""), | ||
] | ||
result = self.builder.get_doc_context("doc1", "", "") | ||
self.assertIn("python_objects", result) | ||
self.assertIn("python_objects_search", result) | ||
self.assertEqual(result["python_objects"], {"ClassA": "module1.ClassA"}) | ||
self.assertEqual(result["python_objects_search"], "ClassA") |
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
Oops, something went wrong.