forked from stac-utils/stac-pydantic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.py
58 lines (49 loc) · 1.63 KB
/
query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Query Extension."""
import warnings
from enum import auto
from types import DynamicClassAttribute
from typing import Any, Callable
from stac_pydantic.utils import AutoValueEnum
# TODO: These are defined in the spec but aren't currently implemented by the operator syntax
UNSUPPORTED_OPERATORS = {"startsWith", "endsWith", "contains", "in"}
_OPERATIONS = {
"eq": lambda x, y: x == y,
"ne": lambda x, y: x != y, # deprecated
"neq": lambda x, y: x != y,
"lt": lambda x, y: x < y,
"le": lambda x, y: x <= y, # deprecated
"lte": lambda x, y: x <= y,
"gt": lambda x, y: x > y,
"ge": lambda x, y: x >= y, # deprecated
"gte": lambda x, y: x >= y,
"startsWith": lambda x, y: x.startsWith(y),
"endsWith": lambda x, y: x.endsWith(y),
"contains": lambda x, y: y in x,
}
class Operator(str, AutoValueEnum):
"""
https://github.com/radiantearth/stac-api-spec/tree/master/extensions/query#query-api-extension
"""
eq = auto()
ne = auto() # deprecated
neq = auto()
lt = auto()
le = auto() # deprecated
lte = auto()
gt = auto()
ge = auto() # deprecated
gte = auto()
startsWith = auto()
endsWith = auto()
contains = auto()
@DynamicClassAttribute
def operator(self) -> Callable[[Any, Any], bool]:
"""Return python operator"""
if self._value_ in ["ne", "ge", "le"]:
newvalue = self._value_.replace("e", "te")
warnings.warn(
f"`{self._value_}` is deprecated, please use `{newvalue}`",
DeprecationWarning,
stacklevel=3,
)
return _OPERATIONS[self._value_]