Skip to content

Commit b787e78

Browse files
committed
refactor: Move handler into its own module
1 parent 6f9d9dd commit b787e78

File tree

3 files changed

+79
-74
lines changed

3 files changed

+79
-74
lines changed
Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,5 @@
1-
"""This module implements a handler for the Python language."""
1+
"""This package implements a handler for the Python language."""
22

3-
import posixpath
4-
from typing import Any, BinaryIO, Iterator, Optional, Tuple
3+
from mkdocstrings_handlers.python.handler import get_handler
54

6-
from griffe.logger import patch_loggers
7-
8-
from mkdocstrings.handlers.base import BaseHandler
9-
from mkdocstrings.inventory import Inventory
10-
from mkdocstrings.loggers import get_logger
11-
from mkdocstrings_handlers.python.collector import PythonCollector
12-
from mkdocstrings_handlers.python.renderer import PythonRenderer
13-
14-
patch_loggers(get_logger)
15-
16-
17-
class PythonHandler(BaseHandler):
18-
"""The Python handler class.
19-
20-
Attributes:
21-
domain: The cross-documentation domain/language for this handler.
22-
enable_inventory: Whether this handler is interested in enabling the creation
23-
of the `objects.inv` Sphinx inventory file.
24-
"""
25-
26-
domain: str = "py" # to match Sphinx's default domain
27-
enable_inventory: bool = True
28-
29-
@classmethod
30-
def load_inventory(
31-
cls,
32-
in_file: BinaryIO,
33-
url: str,
34-
base_url: Optional[str] = None,
35-
**kwargs: Any,
36-
) -> Iterator[Tuple[str, str]]:
37-
"""Yield items and their URLs from an inventory file streamed from `in_file`.
38-
39-
This implements mkdocstrings' `load_inventory` "protocol" (see plugin.py).
40-
41-
Arguments:
42-
in_file: The binary file-like object to read the inventory from.
43-
url: The URL that this file is being streamed from (used to guess `base_url`).
44-
base_url: The URL that this inventory's sub-paths are relative to.
45-
**kwargs: Ignore additional arguments passed from the config.
46-
47-
Yields:
48-
Tuples of (item identifier, item URL).
49-
"""
50-
if base_url is None:
51-
base_url = posixpath.dirname(url)
52-
53-
for item in Inventory.parse_sphinx(in_file, domain_filter=("py",)).values(): # noqa: WPS526
54-
yield item.name, posixpath.join(base_url, item.uri)
55-
56-
57-
def get_handler(
58-
theme: str, # noqa: W0613 (unused argument config)
59-
custom_templates: Optional[str] = None,
60-
**config: Any,
61-
) -> PythonHandler:
62-
"""Simply return an instance of `PythonHandler`.
63-
64-
Arguments:
65-
theme: The theme to use when rendering contents.
66-
custom_templates: Directory containing custom templates.
67-
**config: Configuration passed to the handler.
68-
69-
Returns:
70-
An instance of `PythonHandler`.
71-
"""
72-
return PythonHandler(
73-
collector=PythonCollector(),
74-
renderer=PythonRenderer("python", theme, custom_templates),
75-
)
5+
__all__ = ["get_handler"]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""This module implements a handler for the Python language."""
2+
3+
import posixpath
4+
from typing import Any, BinaryIO, Iterator, Optional, Tuple
5+
6+
from griffe.logger import patch_loggers
7+
8+
from mkdocstrings.handlers.base import BaseHandler
9+
from mkdocstrings.inventory import Inventory
10+
from mkdocstrings.loggers import get_logger
11+
from mkdocstrings_handlers.python.collector import PythonCollector
12+
from mkdocstrings_handlers.python.renderer import PythonRenderer
13+
14+
patch_loggers(get_logger)
15+
16+
17+
class PythonHandler(BaseHandler):
18+
"""The Python handler class.
19+
20+
Attributes:
21+
domain: The cross-documentation domain/language for this handler.
22+
enable_inventory: Whether this handler is interested in enabling the creation
23+
of the `objects.inv` Sphinx inventory file.
24+
"""
25+
26+
domain: str = "py" # to match Sphinx's default domain
27+
enable_inventory: bool = True
28+
29+
@classmethod
30+
def load_inventory(
31+
cls,
32+
in_file: BinaryIO,
33+
url: str,
34+
base_url: Optional[str] = None,
35+
**kwargs: Any,
36+
) -> Iterator[Tuple[str, str]]:
37+
"""Yield items and their URLs from an inventory file streamed from `in_file`.
38+
39+
This implements mkdocstrings' `load_inventory` "protocol" (see plugin.py).
40+
41+
Arguments:
42+
in_file: The binary file-like object to read the inventory from.
43+
url: The URL that this file is being streamed from (used to guess `base_url`).
44+
base_url: The URL that this inventory's sub-paths are relative to.
45+
**kwargs: Ignore additional arguments passed from the config.
46+
47+
Yields:
48+
Tuples of (item identifier, item URL).
49+
"""
50+
if base_url is None:
51+
base_url = posixpath.dirname(url)
52+
53+
for item in Inventory.parse_sphinx(in_file, domain_filter=("py",)).values(): # noqa: WPS526
54+
yield item.name, posixpath.join(base_url, item.uri)
55+
56+
57+
def get_handler(
58+
theme: str, # noqa: W0613 (unused argument config)
59+
custom_templates: Optional[str] = None,
60+
**config: Any,
61+
) -> PythonHandler:
62+
"""Simply return an instance of `PythonHandler`.
63+
64+
Arguments:
65+
theme: The theme to use when rendering contents.
66+
custom_templates: Directory containing custom templates.
67+
**config: Configuration passed to the handler.
68+
69+
Returns:
70+
An instance of `PythonHandler`.
71+
"""
72+
return PythonHandler(
73+
collector=PythonCollector(),
74+
renderer=PythonRenderer("python", theme, custom_templates),
75+
)

src/mkdocstrings_handlers/python/renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class PythonRenderer(BaseRenderer):
5757
Attributes:
5858
fallback_theme: The theme to fallback to.
5959
default_config: The default rendering options,
60-
see [`default_config`][mkdocstrings_handlers.python.PythonRenderer.default_config].
60+
see [`default_config`][mkdocstrings_handlers.python.renderer.PythonRenderer.default_config].
6161
"""
6262

6363
fallback_theme = "material"

0 commit comments

Comments
 (0)