Skip to content

Commit 488262f

Browse files
issue #693 add search for schema and find parameters from the backend
1 parent 7064cf3 commit 488262f

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

openeo/rest/connection.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,37 @@ def describe_process(self, id: str, namespace: Optional[str] = None) -> dict:
10651065

10661066
raise OpenEoClientException("Process does not exist.")
10671067

1068+
"""
1069+
Loads all available processes of the back end.
1070+
1071+
:param namespace: The namespace for which to list processes.
1072+
1073+
:return: Dictionary of All available processes of the back end.
1074+
"""
1075+
1076+
def dict_processes(self, namespace: Optional[str] = None) -> dict:
1077+
processes = self.list_processes(namespace)
1078+
result = {}
1079+
for process in processes:
1080+
result[process["id"]] = process
1081+
return result
1082+
1083+
"""
1084+
Returns schema of the parameter of the process from the back end.
1085+
1086+
:param process_id: The id of the process.
1087+
:param parameter_id: The id of the parameter.
1088+
:param namespace: The namespace of the process.
1089+
1090+
:return: schema of the parameter in the process.
1091+
"""
1092+
1093+
def get_schema(self, process_id: str, parameter_id: str, namespace: Optional[str] = None) -> Union[dict, list]:
1094+
process = self._capabilities_cache.get(("processes_dict", "backend"), lambda: self.dict_processes(namespace))[
1095+
process_id
1096+
]
1097+
return extract_process_argument(process, ["parameters", parameter_id, "schema"])
1098+
10681099
def list_jobs(self, limit: Union[int, None] = None) -> List[dict]:
10691100
"""
10701101
Lists all jobs of the authenticated user.
@@ -2082,3 +2113,71 @@ def extract_connections(
20822113
connections.add(item.connection)
20832114

20842115
return connections
2116+
2117+
2118+
"""
2119+
Extract element of the information using the parameters.
2120+
2121+
:param process: The dict or of a process.
2122+
:param parameters: list of the parameters to extract.
2123+
2124+
:return: arguments of process
2125+
"""
2126+
2127+
2128+
def extract_process_argument(information: Union[dict, list], parameters: list[str]) -> Union[dict, list]:
2129+
for parameter in parameters:
2130+
if isinstance(information, dict):
2131+
information = information[parameter]
2132+
elif isinstance(information, list):
2133+
information = search_dict_in_list(information, parameter)
2134+
return information
2135+
2136+
2137+
"""
2138+
Searches a value of the dict that matches with the key in the list.
2139+
2140+
:param lst: list with dictionaries
2141+
:param parameters: list of the
2142+
2143+
:return: value that matches key
2144+
"""
2145+
2146+
2147+
def search_list_for_dict_key(lst: list, key: str) -> Union[dict, list]:
2148+
result = None
2149+
for item in lst:
2150+
if key in item:
2151+
if result is None:
2152+
result = item[key]
2153+
else:
2154+
raise OpenEoClientException("Multiple keys found with value {v}.".format(v=key))
2155+
if result is None:
2156+
raise OpenEoClientException("No dictionary found with the key {k}.".format(k=key))
2157+
return result
2158+
2159+
2160+
"""
2161+
Searches a dictionary that contains the key-value pair in the list
2162+
2163+
:param lst: list with dictionaries
2164+
:param value: the value to search for
2165+
:param key: the key to search for
2166+
2167+
:return: dictionary containing key-value pair
2168+
"""
2169+
2170+
2171+
def search_dict_in_list(lst: list, value: str, key: str = "name") -> Union[dict, list]:
2172+
result = None
2173+
for item in lst:
2174+
if item[key] == value:
2175+
if result is None:
2176+
result = item
2177+
else:
2178+
raise OpenEoClientException(
2179+
"Multiple dictionaries found with the key-value pair ({k},{v}).".format(k=key, v=value)
2180+
)
2181+
if result is None:
2182+
raise OpenEoClientException("No dictionary found with the key-value pair ({k},{v}).".format(k=key, v=value))
2183+
return result

openeo/rest/datacube.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
# Imports for type checking only (circular import issue at runtime).
6666
import xarray
6767

68-
from openeo.rest.connection import Connection
68+
from openeo.rest.connection import Connection, search_list_for_dict_key
6969
from openeo.udf import XarrayDataCube
7070

7171

@@ -2724,9 +2724,8 @@ def sar_backscatter(
27242724
.. versionadded:: 0.4.9
27252725
.. versionchanged:: 0.4.10 replace `orthorectify` and `rtc` arguments with `coefficient`.
27262726
"""
2727-
coefficient_options = [
2728-
"beta0", "sigma0-ellipsoid", "sigma0-terrain", "gamma0-ellipsoid", "gamma0-terrain", None
2729-
]
2727+
schema = self.connection.get_schema("sar_backscatter", "coefficient")
2728+
coefficient_options = search_list_for_dict_key(schema, "enum")
27302729
if coefficient not in coefficient_options:
27312730
raise OpenEoClientException("Invalid `sar_backscatter` coefficient {c!r}. Should be one of {o}".format(
27322731
c=coefficient, o=coefficient_options

0 commit comments

Comments
 (0)