Skip to content

Commit

Permalink
issue #693 add parse.get_parameter, to retrieve parameter in schema m…
Browse files Browse the repository at this point in the history
…ore compact, and remove get_schema_from_process_parameter
  • Loading branch information
ElienVandermaesenVITO committed Jan 14, 2025
1 parent ece05dc commit 503154b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 51 deletions.
6 changes: 6 additions & 0 deletions openeo/internal/processes/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def from_json_file(cls, path: Union[str, Path]) -> Process:
with Path(path).open("r") as f:
return cls.from_json(f.read())

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


def parse_all_from_dir(path: Union[str, Path], pattern="*.json") -> Iterator[Process]:
"""Parse all openEO process files in given directory"""
Expand Down
22 changes: 0 additions & 22 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,28 +1067,6 @@ def describe_process(self, id: str, namespace: Optional[str] = None) -> dict:

raise OpenEoClientException("Process does not exist.")

def get_schema_from_process_parameter(
self, process_id: str, parameter_id: str, namespace: Optional[str] = None
) -> Union[dict, list]:
"""
Returns schema of the parameter of the process from the back end.
:param process_id: The id of the process.
:param parameter_id: The id of the parameter.
:param namespace: The namespace of the process.
:return: schema of the parameter in the process.
"""
processes = self.list_processes(namespace)
for process in processes:
if process["id"] == process_id:
schema = Process.from_dict(process)
for parameter in schema.parameters:
if parameter.name == parameter_id:
return parameter.schema.schema
raise OpenEoClientException("Parameter does not exist.")
raise OpenEoClientException("Process does not exist.")

def list_jobs(self, limit: Union[int, None] = None) -> List[dict]:
"""
Lists all jobs of the authenticated user.
Expand Down
18 changes: 14 additions & 4 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
convert_callable_to_pgnode,
get_parameter_names,
)
from openeo.internal.processes.parse import Process
from openeo.internal.warnings import UserDeprecationWarning, deprecated, legacy_alias
from openeo.metadata import (
Band,
Expand Down Expand Up @@ -2739,10 +2740,19 @@ def sar_backscatter(
.. versionadded:: 0.4.9
.. versionchanged:: 0.4.10 replace `orthorectify` and `rtc` arguments with `coefficient`.
"""
coefficient_options = [None]
if self.connection:
schema = self.connection.get_schema_from_process_parameter("sar_backscatter", "coefficient")
coefficient_options += search_list_for_dict_key(schema, "enum")
try:
schema = Process.from_dict(self.connection.describe_process("sar_backscatter")).get_parameter("coefficient")
coefficient_options = search_list_for_dict_key(schema, "enum") + [None]
except Exception:
log.warning(f"Failed to extract option for coefficient in sar_backscatter")
coefficient_options = [
"beta0",
"sigma0-ellipsoid",
"sigma0-terrain",
"gamma0-ellipsoid",
"gamma0-terrain",
None,
]
if coefficient not in coefficient_options:
raise OpenEoClientException("Invalid `sar_backscatter` coefficient {c!r}. Should be one of {o}".format(
c=coefficient, o=coefficient_options
Expand Down
25 changes: 0 additions & 25 deletions tests/rest/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2874,31 +2874,6 @@ def test_list_processes_namespace(requests_mock):
assert m.call_count == 1


def test_get_schema_from_process_parameter(requests_mock):
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
processes = [
{
"id": "incr",
"description": "Increment a value",
"summary": "Increment a value",
"parameters": [{"name": "x", "description": "value", "schema": {"type": "integer"}}],
"returns": {"description": "incremented value", "schema": {"type": "integer"}},
},
{
"id": "pi",
"description": "Pi",
"summary": "Pi",
"parameters": [],
"returns": {"description": "value of pi", "schema": {"type": "number"}},
},
]
m = requests_mock.get(API_URL + "processes", json={"processes": processes})
conn = Connection(API_URL)
assert conn.list_processes() == processes
schema = conn.get_schema_from_process_parameter("incr", "x")
assert schema == {"type": "integer"}


def test_get_job(requests_mock):
requests_mock.get(API_URL, json={"api_version": "1.0.0"})
conn = Connection(API_URL)
Expand Down

0 comments on commit 503154b

Please sign in to comment.