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