Skip to content

Commit 9b1e6a4

Browse files
committed
Issue #732/#733 avoid extra save_result after datacube_from_flat_graph
quickfix for #732 and a first step for #733
1 parent dc7e6da commit 9b1e6a4

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

openeo/rest/connection.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
ProcessListingResponse,
7373
ValidationResponse,
7474
)
75+
from openeo.rest.result import SaveResult
7576
from openeo.rest.service import Service
7677
from openeo.rest.udp import Parameter, RESTUserDefinedProcess
7778
from openeo.rest.userfile import UserFile
@@ -1037,9 +1038,12 @@ def datacube_from_process(self, process_id: str, namespace: Optional[str] = None
10371038
graph = PGNode(process_id, namespace=namespace, arguments=kwargs)
10381039
return DataCube(graph=graph, connection=self)
10391040

1040-
def datacube_from_flat_graph(self, flat_graph: dict, parameters: Optional[dict] = None) -> DataCube:
1041+
def datacube_from_flat_graph(
1042+
self, flat_graph: dict, parameters: Optional[dict] = None
1043+
) -> Union[DataCube, SaveResult]:
10411044
"""
1042-
Construct a :py:class:`DataCube` from a flat dictionary representation of a process graph.
1045+
Construct a :py:class:`~openeo.rest.datacube.DataCube`/:py:class:`~openeo.rest.result.SaveResult`
1046+
from the operations encoded in a flat dictionary representation of an openEO process graph.
10431047
10441048
.. seealso:: :ref:`datacube_from_json`, :py:meth:`~openeo.rest.connection.Connection.datacube_from_json`
10451049
@@ -1048,7 +1052,6 @@ def datacube_from_flat_graph(self, flat_graph: dict, parameters: Optional[dict]
10481052
(and optionally parameter metadata under a "parameters" field).
10491053
:param parameters: Optional dictionary mapping parameter names to parameter values
10501054
to use for parameters occurring in the process graph (e.g. as used in user-defined processes)
1051-
:return: A :py:class:`DataCube` corresponding with the operations encoded in the process graph
10521055
"""
10531056
parameters = parameters or {}
10541057

@@ -1062,18 +1065,26 @@ def datacube_from_flat_graph(self, flat_graph: dict, parameters: Optional[dict]
10621065
flat_graph = flat_graph["process_graph"]
10631066

10641067
pgnode = PGNode.from_flat_graph(flat_graph=flat_graph, parameters=parameters or {})
1065-
return DataCube(graph=pgnode, connection=self)
10661068

1067-
def datacube_from_json(self, src: Union[str, Path], parameters: Optional[dict] = None) -> DataCube:
1069+
# TODO #733: smarter or more dynamic detection of what kind of object to return?
1070+
# TODO #733: rename `datacube_from_flat_graph` to something more general as this is not only about DataCube's anymore?
1071+
if pgnode.process_id == "save_result":
1072+
return SaveResult(graph=pgnode, connection=self)
1073+
else:
1074+
return DataCube(graph=pgnode, connection=self)
1075+
1076+
def datacube_from_json(
1077+
self, src: Union[str, Path], parameters: Optional[dict] = None
1078+
) -> Union[DataCube, SaveResult]:
10681079
"""
1069-
Construct a :py:class:`DataCube` from JSON resource containing (flat) process graph representation.
1080+
Construct a :py:class:`~openeo.rest.datacube.DataCube`/:py:class:`~openeo.rest.result.SaveResult`
1081+
from a JSON resource containing a (flat) openEO process graph representation.
10701082
10711083
.. seealso:: :ref:`datacube_from_json`, :py:meth:`~openeo.rest.connection.Connection.datacube_from_flat_graph`
10721084
10731085
:param src: raw JSON string, URL to JSON resource or path to local JSON file
10741086
:param parameters: Optional dictionary mapping parameter names to parameter values
10751087
to use for parameters occurring in the process graph (e.g. as used in user-defined processes)
1076-
:return: A :py:class:`DataCube` corresponding with the operations encoded in the process graph
10771088
"""
10781089
return self.datacube_from_flat_graph(load_json_resource(src), parameters=parameters)
10791090

tests/rest/datacube/test_datacube100.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,6 +3028,50 @@ def test_reduce_callback(self, con100):
30283028
assert isinstance(cube, DataCube)
30293029
assert cube.flat_graph() == flat_graph
30303030

3031+
@pytest.mark.parametrize(
3032+
["flat_graph", "expected"],
3033+
[
3034+
(
3035+
{
3036+
"lc": {"process_id": "load_collection", "arguments": {"id": "S2"}, "result": True},
3037+
},
3038+
{
3039+
"loadcollection1": {"process_id": "load_collection", "arguments": {"id": "S2"}},
3040+
"saveresult1": {
3041+
"process_id": "save_result",
3042+
"arguments": {"data": {"from_node": "loadcollection1"}, "format": "GTiff", "options": {}},
3043+
"result": True,
3044+
},
3045+
},
3046+
),
3047+
(
3048+
{
3049+
"lc": {"process_id": "load_collection", "arguments": {"id": "S2"}},
3050+
"sr": {
3051+
"process_id": "save_result",
3052+
"arguments": {"data": {"from_node": "lc"}, "format": "netCDF"},
3053+
"result": True,
3054+
},
3055+
},
3056+
{
3057+
"loadcollection1": {"process_id": "load_collection", "arguments": {"id": "S2"}},
3058+
"saveresult1": {
3059+
"process_id": "save_result",
3060+
"arguments": {"data": {"from_node": "loadcollection1"}, "format": "netCDF"},
3061+
"result": True,
3062+
},
3063+
},
3064+
),
3065+
],
3066+
)
3067+
def test_datacube_from_flat_graph_save_result_handling(self, con100, dummy_backend, flat_graph, expected):
3068+
"""
3069+
https://github.com/Open-EO/openeo-python-client/issues/732
3070+
"""
3071+
cube = dummy_backend.connection.datacube_from_flat_graph(flat_graph)
3072+
cube.create_job()
3073+
assert dummy_backend.get_batch_pg() == expected
3074+
30313075

30323076
def test_send_nan_json(con100, requests_mock):
30333077
"""https://github.com/Open-EO/openeo-python-client/issues/185"""

0 commit comments

Comments
 (0)