Skip to content

Commit 4b80d1b

Browse files
committed
Issue #324 support simple str/path as file in DataCube.print_json
1 parent 11fb8e3 commit 4b80d1b

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

openeo/internal/compat.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Compatibility layer and small backports.
3+
"""
4+
5+
import contextlib
6+
7+
try:
8+
from contextlib import nullcontext
9+
except ImportError:
10+
# nullcontext for pre-3.7 python
11+
@contextlib.contextmanager
12+
def nullcontext(enter_result=None):
13+
yield enter_result

openeo/rest/_datacube.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import json
22
import logging
3+
import sys
34
import typing
5+
from pathlib import Path
46
from typing import Optional, Union, Tuple
57

8+
from openeo.internal.compat import nullcontext
69
from openeo.internal.graph_building import PGNode, _FromNodeMixin
710
from openeo.util import legacy_alias
811

@@ -71,12 +74,23 @@ def print_json(self, *, file=None, indent: Union[int, None] = 2, separators: Opt
7174
Also see ``json.dumps`` docs for more information on the JSON formatting options.
7275
7376
:param file: file-like object (stream) to print to (current ``sys.stdout`` by default).
77+
Or a path (string or pathlib.Path) to a file to write to.
7478
:param indent: JSON indentation level.
7579
:param separators: (optional) tuple of item/key separators.
7680
7781
.. versionadded:: 0.12.0
7882
"""
79-
print(self.to_json(indent=indent, separators=separators), file=file)
83+
pg = {"process_graph": self.flat_graph()}
84+
if isinstance(file, (str, Path)):
85+
# Create (new) file and automatically close it
86+
file_ctx = Path(file).open("w", encoding="utf8")
87+
else:
88+
# Just use file as-is, but don't close it automatically.
89+
file_ctx = nullcontext(enter_result=file or sys.stdout)
90+
with file_ctx as f:
91+
json.dump(pg, f, indent=indent, separators=separators)
92+
if indent is not None:
93+
f.write("\n")
8094

8195
@property
8296
def _api_version(self):

tests/rest/datacube/test_datacube100.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,16 @@ def test_print_json_file(con100):
14701470
assert f.getvalue() == EXPECTED_JSON_EXPORT_S2_NDVI + "\n"
14711471

14721472

1473+
@pytest.mark.parametrize("path_factory", [str, pathlib.Path])
1474+
def test_print_json_file_path(con100, tmp_path, path_factory):
1475+
ndvi = con100.load_collection("S2").ndvi()
1476+
path = tmp_path / "dump.json"
1477+
assert not path.exists()
1478+
ndvi.print_json(file=path_factory(path))
1479+
assert path.exists()
1480+
assert path.read_text() == EXPECTED_JSON_EXPORT_S2_NDVI + "\n"
1481+
1482+
14731483
def test_sar_backscatter_defaults(con100):
14741484
cube = con100.load_collection("S2").sar_backscatter()
14751485
assert _get_leaf_node(cube) == {

0 commit comments

Comments
 (0)