|
1 | 1 | import json |
2 | 2 | import logging |
3 | 3 | import typing |
4 | | -from typing import Optional |
| 4 | +from typing import Optional, Union, Tuple |
5 | 5 |
|
6 | 6 | from openeo.internal.graph_building import PGNode, _FromNodeMixin |
7 | 7 | from openeo.util import legacy_alias |
@@ -32,23 +32,54 @@ def __str__(self): |
32 | 32 |
|
33 | 33 | def flat_graph(self) -> dict: |
34 | 34 | """ |
35 | | - Get the process graph in flat dict representation |
| 35 | + Get the process graph in internal flat dict representation. |
36 | 36 |
|
37 | | - .. note:: This method is mainly for internal use, subject to change and not recommended for general usage. |
38 | | - Instead, use :py:meth:`to_json()` to get a JSON representation of the process graph. |
| 37 | + .. warning:: This method is mainly intended for internal use. |
| 38 | + It is not recommended for general use and is *subject to change*. |
| 39 | +
|
| 40 | + Instead, it is recommended to use |
| 41 | + :py:meth:`to_json()` or :py:meth:`print_json()` |
| 42 | + to obtain a standardized, interoperable JSON representation of the process graph. |
| 43 | + See :ref:`process_graph_export` for more information. |
39 | 44 | """ |
40 | 45 | # TODO: wrap in {"process_graph":...} by default/optionally? |
41 | 46 | return self._pg.flat_graph() |
42 | 47 |
|
43 | 48 | flatten = legacy_alias(flat_graph, name="flatten") |
44 | 49 |
|
45 | | - def to_json(self, indent=2, separators=None) -> str: |
| 50 | + def to_json(self, *, indent: Union[int, None] = 2, separators: Optional[Tuple[str, str]] = None) -> str: |
46 | 51 | """ |
47 | | - Get JSON representation of (flat dict) process graph. |
| 52 | + Get interoperable JSON representation of the process graph. |
| 53 | +
|
| 54 | + See :py:meth:`DataCube.print_json` to directly print the JSON representation |
| 55 | + and :ref:`process_graph_export` for more usage information. |
| 56 | +
|
| 57 | + Also see ``json.dumps`` docs for more information on the JSON formatting options. |
| 58 | +
|
| 59 | + :param indent: JSON indentation level. |
| 60 | + :param separators: (optional) tuple of item/key separators. |
| 61 | + :return: JSON string |
48 | 62 | """ |
49 | 63 | pg = {"process_graph": self.flat_graph()} |
50 | 64 | return json.dumps(pg, indent=indent, separators=separators) |
51 | 65 |
|
| 66 | + def print_json(self, *, file=None, indent: Union[int, None] = 2, separators: Optional[Tuple[str, str]] = None): |
| 67 | + """ |
| 68 | + Print interoperable JSON representation of the process graph. |
| 69 | +
|
| 70 | + See :py:meth:`DataCube.to_json` to get the JSON representation as a string |
| 71 | + and :ref:`process_graph_export` for more usage information. |
| 72 | +
|
| 73 | + Also see ``json.dumps`` docs for more information on the JSON formatting options. |
| 74 | +
|
| 75 | + :param file: file-like object (stream) to print to (current ``sys.stdout`` by default). |
| 76 | + :param indent: JSON indentation level. |
| 77 | + :param separators: (optional) tuple of item/key separators. |
| 78 | +
|
| 79 | + .. versionadded:: 0.12.0 |
| 80 | + """ |
| 81 | + print(self.to_json(indent=indent, separators=separators), file=file) |
| 82 | + |
52 | 83 | @property |
53 | 84 | def _api_version(self): |
54 | 85 | return self._connection.capabilities().api_version_check |
|
0 commit comments