-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractx.py
More file actions
105 lines (74 loc) · 3.03 KB
/
Copy pathextractx.py
File metadata and controls
105 lines (74 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from __future__ import annotations
from typing import TYPE_CHECKING
from abc import ABC, abstractmethod
from dataclasses import dataclass
from sphinx.util.docutils import SphinxDirective, SphinxRole
from sphinx.transforms import SphinxTransform
from .template import Phase
if TYPE_CHECKING:
from typing import Any
from sphinx.environment import BuildEnvironment
from .ctxnodes import pending_node
@dataclass(frozen=True)
class ExtraContextRequest:
#: The render phase of the current template.
phase: Phase
#: The pending node being rendered.
node: pending_node
#: The current Sphinx build environment.
env: BuildEnvironment
#: The Sphinx execution object associated with this render:
#: a :py:class:`~sphinx.util.docutils.SphinxDirective` or
#: :py:class:`~sphinx.util.docutils.SphinxRole` during :py:data:`Phase.Parsing`,
#: or a :py:class:`~sphinx.transforms.SphinxTransform` during later phases.
host: SphinxDirective | SphinxRole | SphinxTransform
class ExtraContext(ABC):
"""Base class of extra context."""
@abstractmethod
def generate(self, req: ExtraContextRequest, *args, **kwargs) -> Any: ...
# ==========================
# Extra context registration
# ==========================
class ExtraContextRegistry:
"""Registry for extra contexts."""
_ctxs: dict[str, ExtraContext]
def __init__(self) -> None:
self._ctxs = {}
def add(self, name: str, ctx: ExtraContext) -> None:
"""Register an extra context.
:param name: The context name, used in templates via ``load_extra('name')``
:param ctx: An :py:class:`ExtraContext` instance
.. note:: Using the :py:deco:`extra_context` decorator is recommended for most cases.
"""
if name in self._ctxs:
raise ValueError(f'Extra context "{name}" already registered')
self._ctxs[name] = ctx
REGISTRY = ExtraContextRegistry()
"""The global registry for extra contexts.
This is the underlying registry used by the :py:func:`extra_context` decorator.
Using the decorator is recommended for most cases, but you can also register
extra contexts directly via this registry.
"""
def extra_context(name: str):
"""Decorator to register an :py:class:`ExtraContext`.
:param name: The context name, used in templates via ``load_extra('name')``.
"""
def decorator(cls):
REGISTRY.add(name, cls())
return cls
return decorator
def extra_context_names() -> set[str]:
return set(REGISTRY._ctxs.keys())
def extra_context_loader(request: ExtraContextRequest):
def load_extra(name: str, *args, **kwargs) -> Any:
ctx = REGISTRY._ctxs.get(name)
if ctx is None:
raise ValueError(
f'Extra context "{name}" is not registered. '
f'Available: {sorted(extra_context_names())}'
)
try:
return ctx.generate(request, *args, **kwargs)
except Exception as e:
raise ValueError(f'Failed to load extra context "{name}".') from e
return load_extra