Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #693 add search for schema and find parameters from the backend #698

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test for this new method?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also to stay closer to the openEO spec, I would just use name as argument here. "Parameter Id" is not really a thing in the spec

for parameter in self.parameters:
if parameter.name == parameter_id:
return parameter.schema.schema
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would return the whole parameter instead of just its schema to be more generic

Suggested change
return parameter.schema.schema
return parameter

raise LookupError(f"Expected parameter {parameter_id} not found.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise LookupError(f"Expected parameter {parameter_id} not found.")
raise LookupError(f"Parameter {parameter_id!r} 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
except Exception:
except Exception as e:

log.warning(f"Failed to extract option for coefficient in sar_backscatter")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.warning(f"Failed to extract option for coefficient in sar_backscatter")
log.warning(f"Failed to extract coefficient options for process `sar_backscatter`: {e}")

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
Loading