|
8 | 8 | """
|
9 | 9 | import warnings
|
10 | 10 | from enum import Enum, EnumMeta
|
11 |
| -from typing import Any, Optional |
| 11 | +from typing import Any, Optional, TypeVar |
12 | 12 |
|
13 | 13 | from sentinelhub import BBox, MimeType
|
14 | 14 | from sentinelhub.exceptions import deprecated_function
|
15 | 15 |
|
16 | 16 | from .exceptions import EODeprecationWarning
|
17 | 17 |
|
18 | 18 | TIMESTAMP_COLUMN = "TIMESTAMP"
|
| 19 | +T = TypeVar("T") |
19 | 20 |
|
20 | 21 |
|
21 |
| -def _warn_and_adjust(name: str) -> str: |
| 22 | +def _warn_and_adjust(name: T) -> T: |
22 | 23 | # since we stick with `UPPER` for attributes and `lower` for values, we include both to reuse function
|
23 |
| - deprecation_msg = None |
| 24 | + deprecation_msg = None # placeholder |
24 | 25 | if name in ("TIMESTAMP", "timestamp"):
|
25 |
| - name = "TIMESTAMPS" if name == "TIMESTAMP" else "timestamps" |
| 26 | + name = "TIMESTAMPS" if name == "TIMESTAMP" else "timestamps" # type: ignore[assignment] |
26 | 27 |
|
27 | 28 | if deprecation_msg:
|
28 |
| - warnings.warn(deprecation_msg, category=EODeprecationWarning, stacklevel=3) # type: ignore |
| 29 | + warnings.warn(deprecation_msg, category=EODeprecationWarning, stacklevel=3) # type: ignore[unreachable] |
29 | 30 | return name
|
30 | 31 |
|
31 | 32 |
|
32 | 33 | class EnumWithDeprecations(EnumMeta):
|
33 | 34 | """A custom EnumMeta class for catching the deprecated Enum members of the FeatureType Enum class."""
|
34 | 35 |
|
35 |
| - def __getattribute__(cls, name: str) -> Any: |
| 36 | + def __getattribute__(cls, name: str) -> Any: # noqa[N805] |
36 | 37 | return super().__getattribute__(_warn_and_adjust(name))
|
37 | 38 |
|
38 |
| - def __getitem__(cls, name: str) -> Any: |
| 39 | + def __getitem__(cls, name: str) -> Any: # noqa[N805] |
39 | 40 | return super().__getitem__(_warn_and_adjust(name))
|
40 | 41 |
|
41 |
| - def __call__(cls, value: str, *args: Any, **kwargs: Any) -> Any: |
| 42 | + def __call__(cls, value: str, *args: Any, **kwargs: Any) -> Any: # noqa[N805] |
42 | 43 | return super().__call__(_warn_and_adjust(value), *args, **kwargs)
|
43 | 44 |
|
44 | 45 |
|
@@ -292,17 +293,54 @@ class FeatureTypeSet(metaclass=DeprecatedCollectionClass):
|
292 | 293 | RASTER_TYPES_1D = frozenset([FeatureType.SCALAR_TIMELESS, FeatureType.LABEL_TIMELESS])
|
293 | 294 |
|
294 | 295 |
|
295 |
| -class OverwritePermission(Enum): |
296 |
| - """Enum class which specifies which content of saved EOPatch can be overwritten when saving new content. |
| 296 | +def _warn_and_adjust_permissions(name: T) -> T: |
| 297 | + if isinstance(name, str) and name.upper() == "OVERWRITE_PATCH": |
| 298 | + warnings.warn( |
| 299 | + '"OVERWRITE_PATCH" permission is deprecated and will be removed in a future version', |
| 300 | + category=EODeprecationWarning, |
| 301 | + stacklevel=3, |
| 302 | + ) |
| 303 | + return name |
| 304 | + |
| 305 | + |
| 306 | +class PermissionsWithDeprecations(EnumMeta): |
| 307 | + """A custom EnumMeta class for catching the deprecated Enum members of the OverwritePermission Enum class.""" |
| 308 | + |
| 309 | + def __getattribute__(cls, name: str) -> Any: # noqa[N805] |
| 310 | + return super().__getattribute__(_warn_and_adjust_permissions(name)) |
| 311 | + |
| 312 | + def __getitem__(cls, name: str) -> Any: # noqa[N805] |
| 313 | + return super().__getitem__(_warn_and_adjust_permissions(name)) |
| 314 | + |
| 315 | + def __call__(cls, value: str, *args: Any, **kwargs: Any) -> Any: # noqa[N805] |
| 316 | + return super().__call__(_warn_and_adjust_permissions(value), *args, **kwargs) |
| 317 | + |
| 318 | + |
| 319 | +class OverwritePermission(Enum, metaclass=PermissionsWithDeprecations): |
| 320 | + """Enum class which specifies which content of the saved EOPatch can be overwritten when saving new content. |
297 | 321 |
|
298 | 322 | Permissions are in the following hierarchy:
|
299 | 323 |
|
300 | 324 | - `ADD_ONLY` - Only new features can be added, anything that is already saved cannot be changed.
|
301 | 325 | - `OVERWRITE_FEATURES` - Overwrite only data for features which have to be saved. The remaining content of saved
|
302 | 326 | EOPatch will stay unchanged.
|
303 |
| - - `OVERWRITE_PATCH` - Overwrite entire content of saved EOPatch and replace it with the new content. |
304 | 327 | """
|
305 | 328 |
|
306 |
| - ADD_ONLY = 0 |
307 |
| - OVERWRITE_FEATURES = 1 |
308 |
| - OVERWRITE_PATCH = 2 |
| 329 | + ADD_ONLY = "ADD_ONLY" |
| 330 | + OVERWRITE_FEATURES = "OVERWRITE_FEATURES" |
| 331 | + OVERWRITE_PATCH = "OVERWRITE_PATCH" |
| 332 | + |
| 333 | + @classmethod |
| 334 | + def _missing_(cls, value: object) -> "OverwritePermission": |
| 335 | + permissions_mapping = {0: "ADD_ONLY", 1: "OVERWRITE_FEATURES", 2: "OVERWRITE_PATCH"} |
| 336 | + if isinstance(value, int) and value in permissions_mapping: |
| 337 | + deprecation_msg = ( |
| 338 | + f"Please use strings to instantiate overwrite permissions, e.g., instead of {value} use" |
| 339 | + f" {permissions_mapping[value]!r}" |
| 340 | + ) |
| 341 | + warnings.warn(deprecation_msg, category=EODeprecationWarning, stacklevel=3) |
| 342 | + |
| 343 | + return cls(permissions_mapping[value]) |
| 344 | + if isinstance(value, str) and value.upper() in cls._value2member_map_: |
| 345 | + return cls(value.upper()) |
| 346 | + return super()._missing_(value) |
0 commit comments