|
3 | 3 | import json
|
4 | 4 | from typing import (
|
5 | 5 | Any,
|
6 |
| - Callable, |
7 | 6 | Dict,
|
8 | 7 | List,
|
9 | 8 | Optional,
|
|
18 | 17 | from urllib.error import HTTPError
|
19 | 18 |
|
20 | 19 | import pystac
|
21 |
| -from pystac.utils import safe_urlparse, get_opt |
| 20 | +from pystac.utils import safe_urlparse |
22 | 21 | from pystac.serialization import (
|
23 | 22 | merge_common_properties,
|
24 | 23 | identify_stac_object_type,
|
@@ -105,8 +104,7 @@ def json_dumps(self, json_dict: Dict[str, Any], *args: Any, **kwargs: Any) -> st
|
105 | 104 | This method may be overwritten in :class:`StacIO` sub-classes to provide custom
|
106 | 105 | serialization logic. The method accepts arbitrary keyword arguments. These are
|
107 | 106 | not used by the default implementation, but may be used by sub-class
|
108 |
| - implementations (see :meth:`DuplicateKeyReportingMixin.json_dumps` as an |
109 |
| - example). |
| 107 | + implementations. |
110 | 108 |
|
111 | 109 | Args:
|
112 | 110 |
|
@@ -300,36 +298,51 @@ class DuplicateKeyReportingMixin(StacIO):
|
300 | 298 | See https://github.com/stac-utils/pystac/issues/313
|
301 | 299 | """
|
302 | 300 |
|
303 |
| - def json_loads(self, txt: str, *args: Any, **kwargs: Any) -> Dict[str, Any]: |
304 |
| - source: Union[str, "Link_Type"] = get_opt(kwargs.get("source")) |
| 301 | + def json_loads(self, txt: str, *_: Any, **__: Any) -> Dict[str, Any]: |
| 302 | + """Overwrites :meth:`StacIO.json_loads` as the internal method used by |
| 303 | + :class:`DuplicateKeyReportingMixin` for deserializing a JSON string to a |
| 304 | + dictionary while checking for duplicate object keys. |
| 305 | +
|
| 306 | + Raises: |
| 307 | +
|
| 308 | + pystac.DuplicateObjectKeyError : If a duplicate object key is found. |
| 309 | + """ |
305 | 310 | result: Dict[str, Any] = json.loads(
|
306 |
| - txt, object_pairs_hook=self.duplicate_object_names_report_builder(source) |
| 311 | + txt, object_pairs_hook=self._report_duplicate_object_names |
307 | 312 | )
|
308 | 313 | return result
|
309 | 314 |
|
310 |
| - @staticmethod |
311 |
| - def duplicate_object_names_report_builder( |
312 |
| - source: Union[str, "Link_Type"] |
313 |
| - ) -> Callable[[List[Tuple[str, Any]]], Dict[str, Any]]: |
314 |
| - def report_duplicate_object_names( |
315 |
| - object_pairs: List[Tuple[str, Any]] |
316 |
| - ) -> Dict[str, Any]: |
317 |
| - result: Dict[str, Any] = {} |
318 |
| - for key, value in object_pairs: |
319 |
| - if key in result: |
320 |
| - url = ( |
321 |
| - source |
322 |
| - if isinstance(source, str) |
323 |
| - else source.get_absolute_href() |
324 |
| - ) |
325 |
| - raise DuplicateObjectKeyError( |
326 |
| - f"Found duplicate object name “{key}” in “{url}”" |
327 |
| - ) |
328 |
| - else: |
329 |
| - result[key] = value |
330 |
| - return result |
| 315 | + def read_json( |
| 316 | + self, source: Union[str, "Link_Type"], *args: Any, **kwargs: Any |
| 317 | + ) -> Dict[str, Any]: |
| 318 | + """Overwrites :meth:`StacIO.read_json` for deserializing a JSON file to a |
| 319 | + dictionary while checking for duplicate object keys. |
| 320 | +
|
| 321 | + Raises: |
| 322 | +
|
| 323 | + pystac.DuplicateObjectKeyError : If a duplicate object key is found. |
| 324 | + """ |
| 325 | + txt = self.read_text(source, *args, **kwargs) |
| 326 | + try: |
| 327 | + return self.json_loads(txt, source=source) |
| 328 | + except pystac.DuplicateObjectKeyError as e: |
| 329 | + url = source if isinstance(source, str) else source.get_absolute_href() |
| 330 | + msg = str(e) + f" in {url}" |
| 331 | + raise pystac.DuplicateObjectKeyError(msg) |
331 | 332 |
|
332 |
| - return report_duplicate_object_names |
| 333 | + @staticmethod |
| 334 | + def _report_duplicate_object_names( |
| 335 | + object_pairs: List[Tuple[str, Any]] |
| 336 | + ) -> Dict[str, Any]: |
| 337 | + result: Dict[str, Any] = {} |
| 338 | + for key, value in object_pairs: |
| 339 | + if key in result: |
| 340 | + raise pystac.DuplicateObjectKeyError( |
| 341 | + f'Found duplicate object name "{key}"' |
| 342 | + ) |
| 343 | + else: |
| 344 | + result[key] = value |
| 345 | + return result |
333 | 346 |
|
334 | 347 |
|
335 | 348 | class STAC_IO:
|
|
0 commit comments