|
24 | 24 | from collections import UserDict |
25 | 25 | from dataclasses import dataclass, field |
26 | 26 | from inspect import isclass |
27 | | -from typing import Any, Iterator, Optional, Self, Union, get_type_hints |
| 27 | +from typing import TYPE_CHECKING, Any, Iterator, Optional, Union, get_type_hints |
28 | 28 |
|
29 | 29 | import yaml |
30 | 30 |
|
|
43 | 43 | slugify, |
44 | 44 | update_nested_dict, |
45 | 45 | ) |
46 | | -from eodag.utils.exceptions import ValidationError |
| 46 | +from eodag.utils.exceptions import ( |
| 47 | + UnsupportedProductType, |
| 48 | + UnsupportedProvider, |
| 49 | + ValidationError, |
| 50 | +) |
47 | 51 | from eodag.utils.repr import dict_to_html_table |
48 | 52 |
|
| 53 | +if TYPE_CHECKING: |
| 54 | + from typing_extensions import Self |
| 55 | + |
49 | 56 | logger = logging.getLogger("eodag.provider") |
50 | 57 |
|
51 | 58 | AUTH_TOPIC_KEYS = ("auth", "search_auth", "download_auth") |
@@ -237,15 +244,18 @@ def __post_init__(self): |
237 | 244 | self._config = ProviderConfig.from_mapping(self._config) |
238 | 245 |
|
239 | 246 | elif not isinstance(self._config, ProviderConfig): |
240 | | - raise TypeError( |
| 247 | + msg = ( |
241 | 248 | f"Unsupported config type: {type(self._config)}. " |
242 | 249 | "Expected ProviderConfig or dict." |
243 | 250 | ) |
| 251 | + raise ValidationError(msg) |
244 | 252 |
|
245 | 253 | self.name = self.name or self._config.name |
246 | 254 |
|
247 | 255 | if self.name is None: |
248 | | - raise ValueError("Provider name could not be determined from the config.") |
| 256 | + raise ValidationError( |
| 257 | + "Provider name could not be determined from the config." |
| 258 | + ) |
249 | 259 |
|
250 | 260 | def __str__(self) -> str: |
251 | 261 | """Return the provider's name as string.""" |
@@ -385,14 +395,13 @@ def delete_product_type(self, name: str) -> None: |
385 | 395 |
|
386 | 396 | :param name: The product type name. |
387 | 397 |
|
388 | | - :raises KeyError: If the product type is not found. |
| 398 | + :raises UnsupportedProductType: If the product type is not found. |
389 | 399 | """ |
390 | 400 | try: |
391 | 401 | del self.product_types[name] |
392 | 402 | except KeyError: |
393 | | - raise KeyError( |
394 | | - f"Product type '{name}' not found in provider '{self.name}'." |
395 | | - ) |
| 403 | + msg = f"Product type '{name}' not found in provider '{self.name}'." |
| 404 | + raise UnsupportedProductType(msg) |
396 | 405 |
|
397 | 406 | def sync_product_types( |
398 | 407 | self, |
@@ -511,18 +520,20 @@ def __setitem__(self, key: str, value: Provider) -> None: |
511 | 520 | :raises ValueError: If the provider key already exists. |
512 | 521 | """ |
513 | 522 | if key in self.data: |
514 | | - raise ValueError(f"Provider '{key}' already exists.") |
| 523 | + msg = f"Provider '{key}' already exists." |
| 524 | + raise ValueError(msg) |
515 | 525 | super().__setitem__(key, value) |
516 | 526 |
|
517 | 527 | def __delitem__(self, key: str) -> None: |
518 | 528 | """ |
519 | 529 | Delete a provider by name. |
520 | 530 |
|
521 | 531 | :param key: The name of the provider to delete. |
522 | | - :raises KeyError: If the provider key is not found. |
| 532 | + :raises UnsupportedProvider: If the provider key is not found. |
523 | 533 | """ |
524 | 534 | if key not in self.data: |
525 | | - raise KeyError(f"Provider '{key}' not found.") |
| 535 | + msg = f"Provider '{key}' not found." |
| 536 | + raise UnsupportedProvider(msg) |
526 | 537 | super().__delitem__(key) |
527 | 538 |
|
528 | 539 | def __or__(self, other: Self) -> Self: |
@@ -642,23 +653,23 @@ def filter_by_name(self, name: Optional[str] = None) -> Self: |
642 | 653 | {n: p for n, p in self.data.items() if name in [p.name, p.group]} |
643 | 654 | ) |
644 | 655 |
|
645 | | - def delete_product_type(self, provider: str, product_ID: str) -> None: |
| 656 | + def delete_product_type(self, provider: str, product_type: str) -> None: |
646 | 657 | """ |
647 | 658 | Delete a product type from a provider. |
648 | 659 |
|
649 | 660 | :param provider: The provider's name. |
650 | 661 | :param product_ID: The product type to delete. |
651 | | - :raises KeyError: If the provider or product is not found. |
| 662 | + :raises UnsupportedProvider: If the provider or product is not found. |
652 | 663 | """ |
653 | 664 | if provider_obj := self.get(provider): |
654 | | - if product_ID in provider_obj.products: |
655 | | - provider_obj.delete_product_type(product_ID) |
| 665 | + if product_type in provider_obj.product_types: |
| 666 | + provider_obj.delete_product_type(product_type) |
656 | 667 | else: |
657 | | - raise KeyError( |
658 | | - f"Product '{product_ID}' not found for Provider '{provider}'." |
659 | | - ) |
| 668 | + msg = f"Product type '{product_type}' not found for provider '{provider}'." |
| 669 | + raise UnsupportedProductType(msg) |
660 | 670 | else: |
661 | | - raise KeyError(f"Provider '{provider}' not found.") |
| 671 | + msg = f"Provider '{provider}' not found." |
| 672 | + raise UnsupportedProvider(msg) |
662 | 673 |
|
663 | 674 | def _share_credentials(self) -> None: |
664 | 675 | """ |
|
0 commit comments