|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import functools |
| 4 | +import typing as t |
| 5 | + |
| 6 | +if t.TYPE_CHECKING: |
| 7 | + from collections.abc import Callable, Iterator, Mapping |
| 8 | + |
| 9 | + import htpy |
| 10 | + |
| 11 | + |
| 12 | +C = t.TypeVar("C", bound="htpy.Node") |
| 13 | +P = t.ParamSpec("P") |
| 14 | +R = t.TypeVar("R", bound="htpy.Renderable") |
| 15 | + |
| 16 | + |
| 17 | +class _WithChildrenUnbound(t.Generic[C, P, R]): |
| 18 | + """Decorator to make a component support children nodes. |
| 19 | +
|
| 20 | + This decorator is used to create a component that can accept children nodes, |
| 21 | + just like native htpy components. |
| 22 | +
|
| 23 | + It lets you convert this: |
| 24 | +
|
| 25 | + ```python |
| 26 | + def my_component(*, title: str, children: h.Node) -> h.Element: |
| 27 | + ... |
| 28 | +
|
| 29 | + my_component(title="My title", children=h.div["My content"]) |
| 30 | + ``` |
| 31 | +
|
| 32 | + To this: |
| 33 | +
|
| 34 | + ```python |
| 35 | + @h.with_children |
| 36 | + def my_component(children: h.Node, *, title: str) -> h.Element: |
| 37 | + ... |
| 38 | +
|
| 39 | + my_component(title="My title")[h.div["My content"]] |
| 40 | + ``` |
| 41 | + """ |
| 42 | + |
| 43 | + wrapped: Callable[t.Concatenate[C | None, P], R] |
| 44 | + |
| 45 | + def __init__(self, func: Callable[t.Concatenate[C | None, P], R]) -> None: |
| 46 | + # This instance is created at import time when decorating the component. |
| 47 | + # It means that this object is global, and shared between all renderings |
| 48 | + # of the same component. |
| 49 | + self.wrapped = func |
| 50 | + functools.update_wrapper(self, func) |
| 51 | + |
| 52 | + def __repr__(self) -> str: |
| 53 | + return f"with_children({self.wrapped.__name__}, <unbound>)" |
| 54 | + |
| 55 | + def __call__(self, *args: P.args, **kwargs: P.kwargs) -> _WithChildrenBound[C, P, R]: |
| 56 | + # This is the first call to the component, where we get the |
| 57 | + # component's args and kwargs: |
| 58 | + # |
| 59 | + # my_component(title="My title") |
| 60 | + # |
| 61 | + # It is important that we return a new instance bound to the args |
| 62 | + # and kwargs instead of mutating, so that state doesn't leak between |
| 63 | + # multiple renderings of the same component. |
| 64 | + # |
| 65 | + return _WithChildrenBound(self.wrapped, args, kwargs) |
| 66 | + |
| 67 | + def __getitem__(self, children: C | None) -> R: |
| 68 | + # This is the unbound component being used with children: |
| 69 | + # |
| 70 | + # my_component["My content"] |
| 71 | + # |
| 72 | + return self.wrapped(children) # type: ignore[call-arg] # pyright: ignore[reportCallIssue] |
| 73 | + |
| 74 | + def __str__(self) -> str: |
| 75 | + # This is the unbound component being rendered to a string: |
| 76 | + # |
| 77 | + # str(my_component) |
| 78 | + # |
| 79 | + return str(self.wrapped(None)) # type: ignore[call-arg] # pyright: ignore[reportCallIssue, reportArgumentType] |
| 80 | + |
| 81 | + __html__ = __str__ |
| 82 | + |
| 83 | + def encode(self, encoding: str = "utf-8", errors: str = "strict") -> bytes: |
| 84 | + return str(self).encode(encoding, errors) |
| 85 | + |
| 86 | + def iter_chunks(self) -> Iterator[str]: |
| 87 | + return self.wrapped(None).iter_chunks() # type: ignore[call-arg] # pyright: ignore |
| 88 | + |
| 89 | + |
| 90 | +class _WithChildrenBound(t.Generic[C, P, R]): |
| 91 | + _func: Callable[t.Concatenate[C | None, P], R] |
| 92 | + _args: tuple[t.Any, ...] |
| 93 | + _kwargs: Mapping[str, t.Any] |
| 94 | + |
| 95 | + def __init__( |
| 96 | + self, |
| 97 | + func: Callable[t.Concatenate[C | None, P], R], |
| 98 | + args: tuple[t.Any, ...], |
| 99 | + kwargs: Mapping[str, t.Any], |
| 100 | + ) -> None: |
| 101 | + # This is called at runtime when the component is being passed args and |
| 102 | + # kwargs. This instance is only used for the current rendering of the |
| 103 | + # component. |
| 104 | + self._func = func |
| 105 | + self._args = args |
| 106 | + self._kwargs = kwargs |
| 107 | + |
| 108 | + def __repr__(self) -> str: |
| 109 | + return f"with_children({self._func.__name__}, {self._args}, {self._kwargs})" |
| 110 | + |
| 111 | + def __getitem__(self, children: C | None) -> R: |
| 112 | + # This is a bound component being used with children: |
| 113 | + # |
| 114 | + # my_component(title="My title")["My content"] |
| 115 | + # |
| 116 | + return self._func(children, *self._args, **self._kwargs) |
| 117 | + |
| 118 | + def __str__(self) -> str: |
| 119 | + # This is a bound component being rendered to a string: |
| 120 | + # |
| 121 | + # str(my_component(title="My title")) |
| 122 | + # |
| 123 | + return str(self._func(None, *self._args, **self._kwargs)) |
| 124 | + |
| 125 | + __html__ = __str__ |
| 126 | + |
| 127 | + def encode(self, encoding: str = "utf-8", errors: str = "strict") -> bytes: |
| 128 | + return str(self).encode(encoding, errors) |
| 129 | + |
| 130 | + def iter_chunks(self) -> Iterator[str]: |
| 131 | + return self._func(None, *self._args, **self._kwargs).iter_chunks() # pyright: ignore |
| 132 | + |
| 133 | + |
| 134 | +with_children = _WithChildrenUnbound |
0 commit comments