|
21 | 21 |
|
22 | 22 | extensions = ["sphinx.ext.autodoc", "sphinx.ext.doctest", "sphinx.ext.intersphinx"]
|
23 | 23 |
|
24 |
| -autoclass_content = "both" |
| 24 | +autoclass_content = "class" |
| 25 | +autodoc_class_signature = "separated" |
25 | 26 |
|
26 | 27 | autodoc_typehints = "description"
|
27 | 28 |
|
| 29 | + |
| 30 | +def client_name(full_name): |
| 31 | + # Get the class name, e.g. ['elasticsearch', 'client', 'TextStructureClient'] -> 'TextStructure' |
| 32 | + class_name = full_name.split(".")[-1].removesuffix("Client") |
| 33 | + # Convert to snake case, e.g. 'TextStructure' -> '_text_structure' |
| 34 | + snake_case = "".join(["_" + c.lower() if c.isupper() else c for c in class_name]) |
| 35 | + # Remove the leading underscore |
| 36 | + return snake_case.lstrip("_") |
| 37 | + |
| 38 | + |
| 39 | +def add_client_usage_example(app, what, name, obj, options, lines): |
| 40 | + if what == "class" and "Client" in name: |
| 41 | + sub_client_name = client_name(name) |
| 42 | + lines.append( |
| 43 | + f"To use this client, access ``client.{sub_client_name}`` from an " |
| 44 | + " :class:`~elasticsearch.Elasticsearch` client. For example::" |
| 45 | + ) |
| 46 | + lines.append("") |
| 47 | + lines.append(" from elasticsearch import Elasticsearch") |
| 48 | + lines.append("") |
| 49 | + lines.append(" # Create the client instance") |
| 50 | + lines.append(" client = Elasticsearch(...)") |
| 51 | + lines.append(f" # Use the {sub_client_name} client") |
| 52 | + lines.append(f" client.{sub_client_name}.<method>(...)") |
| 53 | + lines.append("") |
| 54 | + |
| 55 | + |
| 56 | +def setup(app): |
| 57 | + app.connect("autodoc-process-docstring", add_client_usage_example) |
| 58 | + |
| 59 | + |
28 | 60 | # Add any paths that contain templates here, relative to this directory.
|
29 | 61 | templates_path = ["_templates"]
|
30 | 62 |
|
|
0 commit comments