GitHub

@@ -2,7 +2,9 @@

2233

from __future__ import annotations

445+

import os

56

import posixpath

7+

import sys

68

from collections import ChainMap

79

from contextlib import suppress

810

from typing import Any, BinaryIO, Iterator, Optional, Tuple

@@ -97,14 +99,30 @@ class PythonHandler(BaseHandler):

9799

docstring_section_style (str): The style used to render docstring sections. Options: `table`, `list`, `spacy`. Default: `table`.

98100

""" # noqa: E501

99101100-

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:

101105

"""Initialize the handler.

102106103107

Parameters:

104108

*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.

105111

**kwargs: Same thing, but with keyword arguments.

106112

"""

107113

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

108126

self._modules_collection: ModulesCollection = ModulesCollection()

109127

self._lines_collection: LinesCollection = LinesCollection()

110128

@@ -161,6 +179,7 @@ def collect(self, identifier: str, config: dict) -> CollectorItem: # noqa: WPS2

161179

if unknown_module:

162180

loader = GriffeLoader(

163181

extensions=load_extensions(final_config.get("extensions", [])),

182+

search_paths=self._paths,

164183

docstring_parser=parser,

165184

docstring_options=parser_options,

166185

modules_collection=self._modules_collection,

@@ -229,16 +248,26 @@ def get_anchors(self, data: CollectorItem) -> list[str]: # noqa: D102 (ignore m

229248

def get_handler(

230249

theme: str, # noqa: W0613 (unused argument config)

231250

custom_templates: Optional[str] = None,

251+

config_file_path: str | None = None,

252+

paths: list[str] | None = None,

232253

**config: Any,

233254

) -> PythonHandler:

234255

"""Simply return an instance of `PythonHandler`.

235256236257

Arguments:

237258

theme: The theme to use when rendering contents.

238259

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.

239262

**config: Configuration passed to the handler.

240263241264

Returns:

242265

An instance of `PythonHandler`.

243266

"""

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+

)

Read the original on github.com ↗