|
25 | 25 | from lxml.etree import XPath, tostring
|
26 | 26 | from typing_extensions import Self, TypeAlias, deprecated
|
27 | 27 |
|
28 |
| -from fundus.utils.serialization import replace_keys_in_nested_dict |
| 28 | +from fundus.utils.serialization import JSONVal, replace_keys_in_nested_dict |
29 | 29 |
|
30 | 30 | LDMappingValue: TypeAlias = Union[List[Dict[str, Any]], Dict[str, Any]]
|
31 | 31 |
|
@@ -64,23 +64,25 @@ def __init__(self, lds: Iterable[Dict[str, Any]] = ()):
|
64 | 64 | def serialize(self) -> Dict[str, Any]:
|
65 | 65 | return {attribute: value for attribute, value in self.__dict__.items() if "__" not in attribute}
|
66 | 66 |
|
| 67 | + def _add(self, ld: Dict[str, JSONVal], ld_type: str) -> None: |
| 68 | + if value := self.__dict__.get(ld_type): |
| 69 | + if not isinstance(value, list): |
| 70 | + self.__dict__[ld_type] = [value] |
| 71 | + self.__dict__[ld_type].append(ld) |
| 72 | + else: |
| 73 | + self.__dict__[ld_type] = ld |
| 74 | + |
67 | 75 | def add_ld(self, ld: Dict[str, Any], name: Optional[str] = None) -> None:
|
68 |
| - if ld_type := ld.get("@type", name): |
69 |
| - if isinstance(ld_type, list): |
70 |
| - if len(ld_type) == 1: |
71 |
| - ld_type = ld_type[0] |
72 |
| - else: |
73 |
| - raise TypeError(f"Unable tp parse ld_type '{ld_type}' of type {list} with length != 1") |
74 |
| - if value := self.__dict__.get(ld_type): |
75 |
| - if not isinstance(value, list): |
76 |
| - self.__dict__[ld_type] = [value] |
77 |
| - self.__dict__[ld_type].append(ld) |
| 76 | + if ld_type := (name or ld.get("@type")): |
| 77 | + if isinstance(ld_type, str): |
| 78 | + self._add(ld, ld_type) |
| 79 | + elif isinstance(ld_type, list): |
| 80 | + for t in ld_type: |
| 81 | + self._add(ld, t) |
78 | 82 | else:
|
79 |
| - self.__dict__[ld_type] = ld |
| 83 | + raise NotImplementedError(f"Unexpected LD type {type(ld_type)}") |
80 | 84 | else:
|
81 |
| - if not self.__dict__.get(self.__UNKNOWN_TYPE__): |
82 |
| - self.__dict__[self.__UNKNOWN_TYPE__] = [] |
83 |
| - self.__dict__[self.__UNKNOWN_TYPE__].append(ld) |
| 85 | + self._add(ld, self.__UNKNOWN_TYPE__) |
84 | 86 |
|
85 | 87 | @deprecated("Use xpath_search() instead")
|
86 | 88 | def get_value_by_key_path(self, key_path: List[str], default: Any = None) -> Optional[Any]:
|
|
0 commit comments