Skip to content

Commit 7307024

Browse files
Refactor component kwargs preparation to make it reusable.
1 parent 8545045 commit 7307024

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

tdom/processor.py

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import sys
22
import typing as t
3-
from collections.abc import Iterable, Sequence
3+
from collections.abc import Iterable, Sequence, Callable
44
from functools import lru_cache
55
from string.templatelib import Interpolation, Template
66
from dataclasses import dataclass
77

88
from markupsafe import Markup
99

10-
from .callables import get_callable_info
10+
from .callables import get_callable_info, CallableInfo
1111
from .format import format_interpolation as base_format_interpolation
1212
from .format import format_template
1313
from .nodes import Comment, DocumentType, Element, Fragment, Node, Text
@@ -437,6 +437,39 @@ def _kebab_to_snake(name: str) -> str:
437437
return name.replace("-", "_").lower()
438438

439439

440+
def _prep_component_kwargs(
441+
callable_info: CallableInfo,
442+
attrs: AttributesDict,
443+
system: dict[str, object],
444+
kebab_to_snake: Callable[[str], str] = _kebab_to_snake,
445+
):
446+
if callable_info.requires_positional:
447+
raise TypeError(
448+
"Component callables cannot have required positional arguments."
449+
)
450+
451+
kwargs: AttributesDict = {}
452+
453+
# Add all supported attributes
454+
for attr_name, attr_value in attrs.items():
455+
snake_name = kebab_to_snake(attr_name)
456+
if snake_name in callable_info.named_params or callable_info.kwargs:
457+
kwargs[snake_name] = attr_value
458+
459+
for attr_name, attr_value in system.items():
460+
if attr_name in callable_info.named_params or callable_info.kwargs:
461+
kwargs[attr_name] = attr_value
462+
463+
# Check to make sure we've fully satisfied the callable's requirements
464+
missing = callable_info.required_named_params - kwargs.keys()
465+
if missing:
466+
raise TypeError(
467+
f"Missing required parameters for component: {', '.join(missing)}"
468+
)
469+
470+
return kwargs
471+
472+
440473
def _invoke_component(
441474
attrs: AttributesDict,
442475
children: list[Node], # TODO: why not TNode, though?
@@ -477,29 +510,9 @@ def _invoke_component(
477510
)
478511
callable_info = get_callable_info(value)
479512

480-
if callable_info.requires_positional:
481-
raise TypeError(
482-
"Component callables cannot have required positional arguments."
483-
)
484-
485-
kwargs: AttributesDict = {}
486-
487-
# Add all supported attributes
488-
for attr_name, attr_value in attrs.items():
489-
snake_name = _kebab_to_snake(attr_name)
490-
if snake_name in callable_info.named_params or callable_info.kwargs:
491-
kwargs[snake_name] = attr_value
492-
493-
# Add children if appropriate
494-
if "children" in callable_info.named_params or callable_info.kwargs:
495-
kwargs["children"] = tuple(children)
496-
497-
# Check to make sure we've fully satisfied the callable's requirements
498-
missing = callable_info.required_named_params - kwargs.keys()
499-
if missing:
500-
raise TypeError(
501-
f"Missing required parameters for component: {', '.join(missing)}"
502-
)
513+
kwargs = _prep_component_kwargs(
514+
callable_info, attrs, system={"children": tuple(children)}
515+
)
503516

504517
result = value(**kwargs)
505518
return _node_from_value(result)

0 commit comments

Comments
 (0)