Skip to content

Commit 503154b

Browse files
issue #693 add parse.get_parameter, to retrieve parameter in schema more compact, and remove get_schema_from_process_parameter
1 parent ece05dc commit 503154b

File tree

4 files changed

+20
-51
lines changed

4 files changed

+20
-51
lines changed

openeo/internal/processes/parse.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ def from_json_file(cls, path: Union[str, Path]) -> Process:
127127
with Path(path).open("r") as f:
128128
return cls.from_json(f.read())
129129

130+
def get_parameter(self, parameter_id: str) -> Union[dict, list]:
131+
for parameter in self.parameters:
132+
if parameter.name == parameter_id:
133+
return parameter.schema.schema
134+
raise LookupError(f"Expected parameter {parameter_id} not found.")
135+
130136

131137
def parse_all_from_dir(path: Union[str, Path], pattern="*.json") -> Iterator[Process]:
132138
"""Parse all openEO process files in given directory"""

openeo/rest/connection.py

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

10681068
raise OpenEoClientException("Process does not exist.")
10691069

1070-
def get_schema_from_process_parameter(
1071-
self, process_id: str, parameter_id: str, namespace: Optional[str] = None
1072-
) -> Union[dict, list]:
1073-
"""
1074-
Returns schema of the parameter of the process from the back end.
1075-
1076-
:param process_id: The id of the process.
1077-
:param parameter_id: The id of the parameter.
1078-
:param namespace: The namespace of the process.
1079-
1080-
:return: schema of the parameter in the process.
1081-
"""
1082-
processes = self.list_processes(namespace)
1083-
for process in processes:
1084-
if process["id"] == process_id:
1085-
schema = Process.from_dict(process)
1086-
for parameter in schema.parameters:
1087-
if parameter.name == parameter_id:
1088-
return parameter.schema.schema
1089-
raise OpenEoClientException("Parameter does not exist.")
1090-
raise OpenEoClientException("Process does not exist.")
1091-
10921070
def list_jobs(self, limit: Union[int, None] = None) -> List[dict]:
10931071
"""
10941072
Lists all jobs of the authenticated user.

openeo/rest/datacube.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
convert_callable_to_pgnode,
3636
get_parameter_names,
3737
)
38+
from openeo.internal.processes.parse import Process
3839
from openeo.internal.warnings import UserDeprecationWarning, deprecated, legacy_alias
3940
from openeo.metadata import (
4041
Band,
@@ -2739,10 +2740,19 @@ def sar_backscatter(
27392740
.. versionadded:: 0.4.9
27402741
.. versionchanged:: 0.4.10 replace `orthorectify` and `rtc` arguments with `coefficient`.
27412742
"""
2742-
coefficient_options = [None]
2743-
if self.connection:
2744-
schema = self.connection.get_schema_from_process_parameter("sar_backscatter", "coefficient")
2745-
coefficient_options += search_list_for_dict_key(schema, "enum")
2743+
try:
2744+
schema = Process.from_dict(self.connection.describe_process("sar_backscatter")).get_parameter("coefficient")
2745+
coefficient_options = search_list_for_dict_key(schema, "enum") + [None]
2746+
except Exception:
2747+
log.warning(f"Failed to extract option for coefficient in sar_backscatter")
2748+
coefficient_options = [
2749+
"beta0",
2750+
"sigma0-ellipsoid",
2751+
"sigma0-terrain",
2752+
"gamma0-ellipsoid",
2753+
"gamma0-terrain",
2754+
None,
2755+
]
27462756
if coefficient not in coefficient_options:
27472757
raise OpenEoClientException("Invalid `sar_backscatter` coefficient {c!r}. Should be one of {o}".format(
27482758
c=coefficient, o=coefficient_options

tests/rest/test_connection.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,31 +2874,6 @@ 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-
29022877
def test_get_job(requests_mock):
29032878
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
29042879
conn = Connection(API_URL)

0 commit comments

Comments
 (0)