|
17 | 17 | # limitations under the License. |
18 | 18 | from __future__ import annotations |
19 | 19 |
|
| 20 | +import logging |
20 | 21 | import re |
21 | 22 | from collections import UserDict |
22 | | -from typing import TYPE_CHECKING, Any, Optional |
| 23 | +from typing import TYPE_CHECKING, Any, Optional, Union |
23 | 24 |
|
| 25 | +from typing_extensions import override |
| 26 | + |
| 27 | +from eodag.api.product.metadata_mapping import NOT_AVAILABLE, OFFLINE_STATUS |
| 28 | +from eodag.utils import guess_file_type |
24 | 29 | from eodag.utils.exceptions import NotAvailableError |
25 | 30 | from eodag.utils.repr import dict_to_html_table |
26 | 31 |
|
|
29 | 34 | from eodag.types.download_args import DownloadConf |
30 | 35 | from eodag.utils import Unpack |
31 | 36 |
|
| 37 | +logger = logging.getLogger("eodag.api.product.assets") |
| 38 | + |
32 | 39 |
|
33 | 40 | class AssetsDict(UserDict): |
34 | 41 | """A UserDict object which values are :class:`~eodag.api.product._assets.Asset` |
@@ -56,12 +63,84 @@ class AssetsDict(UserDict): |
56 | 63 |
|
57 | 64 | product: EOProduct |
58 | 65 |
|
| 66 | + TECHNICAL_ASSETS = ["download_link", "quicklook", "thumbnail"] |
| 67 | + |
59 | 68 | def __init__(self, product: EOProduct, *args: Any, **kwargs: Any) -> None: |
60 | 69 | self.product = product |
61 | 70 | super(AssetsDict, self).__init__(*args, **kwargs) |
62 | 71 |
|
63 | 72 | def __setitem__(self, key: str, value: dict[str, Any]) -> None: |
| 73 | + if not self._check(key, value): |
| 74 | + return |
64 | 75 | super().__setitem__(key, Asset(self.product, key, value)) |
| 76 | + self.sort() |
| 77 | + |
| 78 | + @override |
| 79 | + def update(self, value: Union[dict[str, Any], AssetsDict]) -> None: # type: ignore |
| 80 | + """Used to self update with exernal value""" |
| 81 | + buffer: dict = {} |
| 82 | + for key in value: |
| 83 | + if self._check(key, value[key]): |
| 84 | + buffer[key] = value[key] |
| 85 | + super().update(buffer) |
| 86 | + self.sort() |
| 87 | + |
| 88 | + def _check(self, asset_key: str, asset_values: dict[str, Any]) -> bool: |
| 89 | + |
| 90 | + # Asset must have href or order_link |
| 91 | + href = asset_values.pop("href", None) |
| 92 | + if href not in [None, "", NOT_AVAILABLE]: |
| 93 | + asset_values["href"] = href |
| 94 | + order_link = asset_values.pop("order_link", None) |
| 95 | + if order_link not in [None, "", NOT_AVAILABLE]: |
| 96 | + asset_values["order_link"] = order_link |
| 97 | + |
| 98 | + if "href" not in asset_values and "order_link" not in asset_values: |
| 99 | + logger.warning( |
| 100 | + "asset '{}' skipped ignored because neither href nor order_link is available".format( |
| 101 | + asset_key |
| 102 | + ), |
| 103 | + ) |
| 104 | + return False |
| 105 | + |
| 106 | + def target_url(asset: dict) -> Optional[str]: |
| 107 | + target_url = None |
| 108 | + if "href" in asset: |
| 109 | + target_url = asset["href"] |
| 110 | + elif "order_link" in asset: |
| 111 | + target_url = asset["order_link"] |
| 112 | + return target_url |
| 113 | + |
| 114 | + assets = self.as_dict() |
| 115 | + used_urls = [] |
| 116 | + for key in assets: |
| 117 | + if key == asset_key: |
| 118 | + # Duplicated key |
| 119 | + return False |
| 120 | + used_urls.append(target_url(assets[key])) |
| 121 | + |
| 122 | + # Prevent asset key / asset target url replication (out from technical ones) |
| 123 | + # thumbnail and quicklook can share same url |
| 124 | + url = target_url(asset_values) |
| 125 | + if asset_key not in AssetsDict.TECHNICAL_ASSETS and (url in used_urls): |
| 126 | + # Duplicated url |
| 127 | + return False |
| 128 | + |
| 129 | + return True |
| 130 | + |
| 131 | + def sort(self): |
| 132 | + """Used to self sort""" |
| 133 | + sorted_assets = {} |
| 134 | + data = self.as_dict() |
| 135 | + # Keep technical assets first |
| 136 | + for key in AssetsDict.TECHNICAL_ASSETS: |
| 137 | + if key in data: |
| 138 | + sorted_assets[key] = data.pop(key) |
| 139 | + # Sort and add others |
| 140 | + data = dict(sorted(data.items())) |
| 141 | + for key in data: |
| 142 | + sorted_assets[key] = data[key] |
| 143 | + super().update(sorted_assets) |
65 | 144 |
|
66 | 145 | def as_dict(self) -> dict[str, Any]: |
67 | 146 | """Builds a representation of AssetsDict to enable its serialization |
@@ -165,14 +244,55 @@ class Asset(UserDict): |
165 | 244 | """ |
166 | 245 |
|
167 | 246 | product: EOProduct |
| 247 | + |
| 248 | + # Location |
| 249 | + location: Optional[str] |
| 250 | + remote_location: Optional[str] |
| 251 | + |
| 252 | + # File |
168 | 253 | size: int |
169 | 254 | filename: Optional[str] |
170 | 255 | rel_path: str |
171 | 256 |
|
172 | 257 | def __init__(self, product: EOProduct, key: str, *args: Any, **kwargs: Any) -> None: |
173 | 258 | self.product = product |
174 | 259 | self.key = key |
| 260 | + self.location = None |
| 261 | + self.remote_location = None |
175 | 262 | super(Asset, self).__init__(*args, **kwargs) |
| 263 | + self._update() |
| 264 | + |
| 265 | + def __setitem__(self, key, item): |
| 266 | + super().__setitem__(key, item) |
| 267 | + self._update() |
| 268 | + |
| 269 | + def _update(self): |
| 270 | + |
| 271 | + title = self.get("title", None) |
| 272 | + if title is None: |
| 273 | + super().__setitem__("title", self.key) |
| 274 | + |
| 275 | + # Order link behaviour require order:status state |
| 276 | + orderlink = self.get("order_link", None) |
| 277 | + orderstatus = self.get("order:status", None) |
| 278 | + if orderlink is not None and orderstatus is None: |
| 279 | + super().__setitem__("order:status", OFFLINE_STATUS) |
| 280 | + |
| 281 | + href = self.get("href", None) |
| 282 | + if href is None: |
| 283 | + super().__setitem__("href", "") |
| 284 | + href = "" |
| 285 | + |
| 286 | + if href != "": |
| 287 | + # Provide location and remote_location when undefined and href updated |
| 288 | + if self.location is None: |
| 289 | + self.location = href |
| 290 | + if self.remote_location is None: |
| 291 | + self.remote_location = href |
| 292 | + # With order behaviour, href can be fill later |
| 293 | + content_type = self.get("type", None) |
| 294 | + if content_type is None: |
| 295 | + super().__setitem__("type", guess_file_type(href)) |
176 | 296 |
|
177 | 297 | def as_dict(self) -> dict[str, Any]: |
178 | 298 | """Builds a representation of Asset to enable its serialization |
|
0 commit comments