Skip to content

Commit 8699f3c

Browse files
issue #693 use openeo.internal.process.parse intead of new functions
1 parent 488262f commit 8699f3c

File tree

3 files changed

+43
-74
lines changed

3 files changed

+43
-74
lines changed

openeo/rest/connection.py

Lines changed: 17 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from openeo.internal.graph_building import FlatGraphableMixin, PGNode, as_flat_graph
3939
from openeo.internal.jupyter import VisualDict, VisualList
4040
from openeo.internal.processes.builder import ProcessBuilderBase
41+
from openeo.internal.processes.parse import Process
4142
from openeo.internal.warnings import deprecated, legacy_alias
4243
from openeo.metadata import (
4344
Band,
@@ -1065,36 +1066,26 @@ def describe_process(self, id: str, namespace: Optional[str] = None) -> dict:
10651066

10661067
raise OpenEoClientException("Process does not exist.")
10671068

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-
"""
1069+
def get_schema_from_process_parameter(
1070+
self, process_id: str, parameter_id: str, namespace: Optional[str] = None
1071+
) -> dict:
1072+
"""
10841073
Returns schema of the parameter of the process from the back end.
10851074
10861075
:param process_id: The id of the process.
10871076
:param parameter_id: The id of the parameter.
10881077
:param namespace: The namespace of the process.
10891078
10901079
: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"])
1080+
"""
1081+
processes = self.list_processes(namespace)
1082+
for process in processes:
1083+
if process["id"] == process_id:
1084+
schema = Process.from_dict(process)
1085+
for parameter in schema.parameters:
1086+
if parameter.name == parameter_id:
1087+
return parameter.schema.schema
1088+
raise OpenEoClientException("Process does not exist.")
10981089

10991090
def list_jobs(self, limit: Union[int, None] = None) -> List[dict]:
11001091
"""
@@ -2115,36 +2106,15 @@ def extract_connections(
21152106
return connections
21162107

21172108

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-
"""
2109+
def search_list_for_dict_key(lst: list, key: str) -> Union[dict, list]:
2110+
"""
21382111
Searches a value of the dict that matches with the key in the list.
21392112
21402113
:param lst: list with dictionaries
21412114
:param parameters: list of the
21422115
21432116
:return: value that matches key
2144-
"""
2145-
2146-
2147-
def search_list_for_dict_key(lst: list, key: str) -> Union[dict, list]:
2117+
"""
21482118
result = None
21492119
for item in lst:
21502120
if key in item:
@@ -2155,29 +2125,3 @@ def search_list_for_dict_key(lst: list, key: str) -> Union[dict, list]:
21552125
if result is None:
21562126
raise OpenEoClientException("No dictionary found with the key {k}.".format(k=key))
21572127
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2724,7 +2724,7 @@ def sar_backscatter(
27242724
.. versionadded:: 0.4.9
27252725
.. versionchanged:: 0.4.10 replace `orthorectify` and `rtc` arguments with `coefficient`.
27262726
"""
2727-
schema = self.connection.get_schema("sar_backscatter", "coefficient")
2727+
schema = self.connection.get_schema_from_process_parameter("sar_backscatter", "coefficient")
27282728
coefficient_options = search_list_for_dict_key(schema, "enum")
27292729
if coefficient not in coefficient_options:
27302730
raise OpenEoClientException("Invalid `sar_backscatter` coefficient {c!r}. Should be one of {o}".format(

tests/rest/test_connection.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,6 +2874,31 @@ def test_list_processes_namespace(requests_mock):
28742874
assert m.call_count == 1
28752875

28762876

2877+
def test_get_schema_from_process_parameter(requests_mock):
2878+
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
2879+
processes = [
2880+
{
2881+
"id": "incr",
2882+
"description": "Increment a value",
2883+
"summary": "Increment a value",
2884+
"parameters": [{"name": "x", "description": "value", "schema": {"type": "integer"}}],
2885+
"returns": {"description": "incremented value", "schema": {"type": "integer"}},
2886+
},
2887+
{
2888+
"id": "pi",
2889+
"description": "Pi",
2890+
"summary": "Pi",
2891+
"parameters": [],
2892+
"returns": {"description": "value of pi", "schema": {"type": "number"}},
2893+
},
2894+
]
2895+
m = requests_mock.get(API_URL + "processes", json={"processes": processes})
2896+
conn = Connection(API_URL)
2897+
assert conn.list_processes() == processes
2898+
schema = conn.get_schema_from_process_parameter("incr", "x")
2899+
assert schema == {"type": "integer"}
2900+
2901+
28772902
def test_get_job(requests_mock):
28782903
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
28792904
conn = Connection(API_URL)

0 commit comments

Comments
 (0)