|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import posixpath |
| 6 | +import sys |
6 | 7 | from collections import ChainMap |
7 | 8 | from contextlib import suppress |
8 | 9 | from typing import Any, BinaryIO, Iterator, Optional, Tuple |
@@ -97,14 +98,27 @@ class PythonHandler(BaseHandler): |
97 | 98 | docstring_section_style (str): The style used to render docstring sections. Options: `table`, `list`, `spacy`. Default: `table`. |
98 | 99 | """ # noqa: E501 |
99 | 100 |
|
100 | | - def __init__(self, *args, **kwargs) -> None: |
| 101 | + def __init__( |
| 102 | + self, *args: Any, config_file_path: str | None = None, paths: list[str] | None = None, **kwargs: Any |
| 103 | + ) -> None: |
101 | 104 | """Initialize the handler. |
102 | 105 |
|
103 | 106 | Parameters: |
104 | 107 | *args: Handler name, theme and custom templates. |
| 108 | + config_file_path: The MkDocs configuration file path. |
| 109 | + paths: A list of paths to use as Griffe search paths. |
105 | 110 | **kwargs: Same thing, but with keyword arguments. |
106 | 111 | """ |
107 | 112 | super().__init__(*args, **kwargs) |
| 113 | + self._config_file_path = config_file_path |
| 114 | + paths = paths or [] |
| 115 | + if not paths and config_file_path: |
| 116 | + paths.append(posixpath.dirname(config_file_path)) |
| 117 | + search_paths = [path for path in sys.path if path] # eliminate empty path |
| 118 | + for path in reversed(paths): |
| 119 | + if path not in search_paths: |
| 120 | + search_paths.insert(0, path) |
| 121 | + self._paths = search_paths |
108 | 122 | self._modules_collection: ModulesCollection = ModulesCollection() |
109 | 123 | self._lines_collection: LinesCollection = LinesCollection() |
110 | 124 |
|
@@ -161,6 +175,7 @@ def collect(self, identifier: str, config: dict) -> CollectorItem: # noqa: WPS2 |
161 | 175 | if unknown_module: |
162 | 176 | loader = GriffeLoader( |
163 | 177 | extensions=load_extensions(final_config.get("extensions", [])), |
| 178 | + search_paths=self._paths, |
164 | 179 | docstring_parser=parser, |
165 | 180 | docstring_options=parser_options, |
166 | 181 | modules_collection=self._modules_collection, |
@@ -229,16 +244,26 @@ def get_anchors(self, data: CollectorItem) -> list[str]: # noqa: D102 (ignore m |
229 | 244 | def get_handler( |
230 | 245 | theme: str, # noqa: W0613 (unused argument config) |
231 | 246 | custom_templates: Optional[str] = None, |
| 247 | + config_file_path: str | None = None, |
| 248 | + paths: list[str] | None = None, |
232 | 249 | **config: Any, |
233 | 250 | ) -> PythonHandler: |
234 | 251 | """Simply return an instance of `PythonHandler`. |
235 | 252 |
|
236 | 253 | Arguments: |
237 | 254 | theme: The theme to use when rendering contents. |
238 | 255 | custom_templates: Directory containing custom templates. |
| 256 | + config_file_path: The MkDocs configuration file path. |
| 257 | + paths: A list of paths to use as Griffe search paths. |
239 | 258 | **config: Configuration passed to the handler. |
240 | 259 |
|
241 | 260 | Returns: |
242 | 261 | An instance of `PythonHandler`. |
243 | 262 | """ |
244 | | - return PythonHandler("python", theme, custom_templates) |
| 263 | + return PythonHandler( |
| 264 | + handler="python", |
| 265 | + theme=theme, |
| 266 | + custom_templates=custom_templates, |
| 267 | + config_file_path=config_file_path, |
| 268 | + paths=paths, |
| 269 | + ) |
0 commit comments