Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • tvincent/ewokscore
  • workflow/ewoks/ewokscore
2 results
Show changes
Commits on Source (2)
...@@ -25,16 +25,16 @@ GraphRepresentation = enum.Enum( ...@@ -25,16 +25,16 @@ GraphRepresentation = enum.Enum(
def dump( def dump(
graph: networkx.DiGraph, graph: networkx.DiGraph,
destination=None, destination: Optional[Union[str, Path]] = None,
representation: Optional[Union[GraphRepresentation, str]] = None, representation: Optional[Union[GraphRepresentation, str]] = None,
**kw, **kw,
) -> Union[str, dict]: ) -> Union[str, Path, dict]:
"""From runtime to persistent representation""" """From runtime to persistent representation"""
if isinstance(representation, str): if isinstance(representation, str):
representation = GraphRepresentation.__members__[representation] representation = GraphRepresentation.__members__[representation]
if representation is None: if representation is None:
if isinstance(destination, str): if isinstance(destination, (str, Path)):
filename = destination.lower() filename = str(destination).lower()
if filename.endswith(".json"): if filename.endswith(".json"):
representation = GraphRepresentation.json representation = GraphRepresentation.json
elif filename.endswith((".yml", ".yaml")): elif filename.endswith((".yml", ".yaml")):
...@@ -46,6 +46,8 @@ def dump( ...@@ -46,6 +46,8 @@ def dump(
return _networkx_to_dict(graph) return _networkx_to_dict(graph)
if representation == GraphRepresentation.json: if representation == GraphRepresentation.json:
if destination is None:
raise TypeError("Destination should be specified when dumping to json")
dictrepr = dump(graph) dictrepr = dump(graph)
makedirs_from_filename(destination) makedirs_from_filename(destination)
kw.setdefault("indent", 2) kw.setdefault("indent", 2)
...@@ -58,6 +60,8 @@ def dump( ...@@ -58,6 +60,8 @@ def dump(
return json.dumps(dictrepr, **kw) return json.dumps(dictrepr, **kw)
if representation == GraphRepresentation.yaml: if representation == GraphRepresentation.yaml:
if destination is None:
raise TypeError("Destination should be specified when dumping to yaml")
dictrepr = dump(graph) dictrepr = dump(graph)
makedirs_from_filename(destination) makedirs_from_filename(destination)
with open(destination, mode="w") as f: with open(destination, mode="w") as f:
...@@ -65,7 +69,9 @@ def dump( ...@@ -65,7 +69,9 @@ def dump(
return destination return destination
if representation == GraphRepresentation.json_module: if representation == GraphRepresentation.json_module:
package, _, file = destination.rpartition(".") if destination is None:
raise TypeError("Destination should be specified when dumping to json")
package, _, file = str(destination).rpartition(".")
assert package, f"No package provided when saving graph to '{destination}'" assert package, f"No package provided when saving graph to '{destination}'"
destination = os.path.join(_package_path(package), f"{file}.json") destination = os.path.join(_package_path(package), f"{file}.json")
return dump(graph, destination=destination, representation="json", **kw) return dump(graph, destination=destination, representation="json", **kw)
......
from pathlib import Path
from typing import Hashable, Optional, Union from typing import Hashable, Optional, Union
import networkx import networkx
from ewoksutils.import_utils import qualname from ewoksutils.import_utils import qualname
...@@ -107,10 +108,10 @@ class TaskGraph: ...@@ -107,10 +108,10 @@ class TaskGraph:
def dump( def dump(
self, self,
destination=None, destination: Optional[Union[str, Path]] = None,
representation: Optional[Union[serialize.GraphRepresentation, str]] = None, representation: Optional[Union[serialize.GraphRepresentation, str]] = None,
**kw, **kw,
) -> Optional[Union[str, dict]]: ) -> Optional[Union[str, Path, dict]]:
return serialize.dump( return serialize.dump(
self.graph, destination=destination, representation=representation, **kw self.graph, destination=destination, representation=representation, **kw
) )
......
from typing import Iterable, Optional, Tuple from pathlib import Path
import pytest import pytest
from ewokscore import execute_graph from ewokscore import execute_graph
...@@ -68,13 +68,14 @@ def test_start_nodes(): ...@@ -68,13 +68,14 @@ def test_start_nodes():
@pytest.mark.parametrize( @pytest.mark.parametrize(
"representation", (None, "json", "json_dict", "json_string", "yaml") "representation", (None, "json", "json_dict", "json_string", "yaml")
) )
def test_serialize_graph(graph_name, representation, tmpdir): @pytest.mark.parametrize("path_format", (str, Path))
def test_serialize_graph(graph_name, representation, path_format, tmpdir):
graph, _ = get_graph(graph_name) graph, _ = get_graph(graph_name)
ewoksgraph = load_graph(graph) ewoksgraph = load_graph(graph)
if representation == "yaml": if representation == "yaml":
destination = str(tmpdir / "file.yml") destination = path_format(tmpdir / "file.yml")
elif representation == "json": elif representation == "json":
destination = str(tmpdir / "file.json") destination = path_format(tmpdir / "file.json")
else: else:
destination = None destination = None
inmemorydump = ewoksgraph.dump(destination, representation=representation) inmemorydump = ewoksgraph.dump(destination, representation=representation)
...@@ -89,17 +90,18 @@ def test_serialize_graph(graph_name, representation, tmpdir): ...@@ -89,17 +90,18 @@ def test_serialize_graph(graph_name, representation, tmpdir):
@pytest.mark.parametrize("graph_name", graph_names()) @pytest.mark.parametrize("graph_name", graph_names())
def test_convert_graph(graph_name, tmpdir): @pytest.mark.parametrize("path_format", (str, Path))
def test_convert_graph(graph_name, path_format, tmpdir):
graph, _ = get_graph(graph_name) graph, _ = get_graph(graph_name)
ewoksgraph = load_graph(graph) ewoksgraph = load_graph(graph)
assert_convert_graph(convert_graph, ewoksgraph, tmpdir) assert_convert_graph(convert_graph, ewoksgraph, path_format, tmpdir)
def assert_convert_graph( def assert_convert_graph(
convert_graph, convert_graph,
ewoksgraph, ewoksgraph,
path_format,
tmpdir, tmpdir,
representations: Optional[Iterable[Tuple[dict, dict, Optional[str]]]] = None,
): ):
"""All graph `representations` need to be known by `convert_graph`. It will always """All graph `representations` need to be known by `convert_graph`. It will always
test the basic representations (e.g. json and yaml) in addition to the provided test the basic representations (e.g. json and yaml) in addition to the provided
...@@ -115,15 +117,13 @@ def assert_convert_graph( ...@@ -115,15 +117,13 @@ def assert_convert_graph(
(dict(), {"representation": "json_dict"}, None), (dict(), {"representation": "json_dict"}, None),
(dict(), {"representation": "json_string"}, None), (dict(), {"representation": "json_string"}, None),
] ]
if representations:
conversion_chain.extend(representations)
conversion_chain.append(non_serialized_representation) conversion_chain.append(non_serialized_representation)
source = ewoksgraph source = ewoksgraph
for convert_from, convert_to in zip(conversion_chain[:-1], conversion_chain[1:]): for convert_from, convert_to in zip(conversion_chain[:-1], conversion_chain[1:]):
load_options, _, _ = convert_from load_options, _, _ = convert_from
_, save_options, fileext = convert_to _, save_options, fileext = convert_to
if fileext: if fileext:
destination = str(tmpdir / f"file.{fileext}") destination = path_format(tmpdir / f"file.{fileext}")
else: else:
destination = None destination = None
result = convert_graph( result = convert_graph(
......