Skip to content

Commit

Permalink
Fix multiple out format implementation & test
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-ash committed Nov 17, 2022
1 parent 01bedc4 commit 78cf336
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
17 changes: 10 additions & 7 deletions diagrams/Diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from .Context import Context
from .utils import setdiagram


class Diagram(Context):
__curvestyles = ("ortho", "curved")
__outformats = ("png", "jpg", "svg", "pdf")
__outformats = ("png", "jpg", "svg", "pdf", "dot")

# fmt: off
_default_graph_attrs = {
Expand Down Expand Up @@ -47,7 +48,7 @@ def __init__(
filename: str = "",
direction: str = "LR",
curvestyle: str = "ortho",
outformat: str = "png",
outformats: list = ["png"],
show: bool = True,
graph_attr: dict = {},
node_attr: dict = {},
Expand All @@ -61,7 +62,7 @@ def __init__(
If not given, it will be generated from the name.
:param direction: Data flow direction. Default is 'left to right'.
:param curvestyle: Curve bending style. One of "ortho" or "curved".
:param outformat: Output file format. Default is 'png'.
:param outformats: List of output file formats. Default is ['png'].
:param show: Open generated image after save if true, just only save otherwise.
:param graph_attr: Provide graph_attr dot config attributes.
:param node_attr: Provide node_attr dot config attributes.
Expand Down Expand Up @@ -93,9 +94,10 @@ def __init__(
raise ValueError(f'"{curvestyle}" is not a valid curvestyle')
self.dot.graph_attr["splines"] = curvestyle

if not self._validate_outformat(outformat):
raise ValueError(f'"{outformat}" is not a valid output format')
self.outformat = outformat
for outformat in outformats:
if not self._validate_outformat(outformat):
raise ValueError(f'"{outformat}" is not a valid output format')
self.outformats = outformats

# Merge passed in attributes
self.dot.graph_attr.update(graph_attr)
Expand Down Expand Up @@ -140,4 +142,5 @@ def connect(self, node: "Node", node2: "Node", edge: "Edge") -> None:
self.dot.edge(node.nodeid, node2.nodeid, **edge.attrs)

def render(self) -> None:
self.dot.render(format=self.outformat, view=self.show, quiet=True)
for outformat in self.outformats:
self.dot.render(format=outformat, view=self.show, quiet=True)
11 changes: 7 additions & 4 deletions tests/test_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ def test_validate_curvestyle(self):
def test_validate_outformat(self):
# Normal output formats.
for fmt in ("png", "jpg", "svg", "pdf", "PNG", "dot"):
Diagram(outformat=fmt)
Diagram(outformats=[fmt])

# Invalid output formats.
for fmt in ("pnp", "jpe", "unknown"):
with self.assertRaises(ValueError):
Diagram(outformat=fmt)
Diagram(outformats=[fmt])

def test_with_global_context(self):
self.assertIsNone(getdiagram())
Expand Down Expand Up @@ -115,12 +115,15 @@ def test_autolabel(self):


def test_outformat_list(self):
"""Check that outformat render all the files from the list."""
"""Check that outformats render all the files from the list."""
self.name = 'diagrams_image'
with Diagram(show=False, outformat=["dot", "png"]):
with Diagram(show=False, outformats=["dot", "png", "jpg", "svg", "pdf"]):
Node("node1")
# both files must exist
self.assertTrue(os.path.exists(f"{self.name}.png"))
self.assertTrue(os.path.exists(f"{self.name}.jpg"))
self.assertTrue(os.path.exists(f"{self.name}.svg"))
self.assertTrue(os.path.exists(f"{self.name}.pdf"))
self.assertTrue(os.path.exists(f"{self.name}.dot"))

# clean the dot file as it only generated here
Expand Down

0 comments on commit 78cf336

Please sign in to comment.