From de45ff363855654f85b0641ebcd13081c9d7b916 Mon Sep 17 00:00:00 2001 From: tessier Date: Wed, 27 Jan 2021 00:56:01 +0900 Subject: [PATCH] feat(output): Generate dot file and support multi outformat.(#441) --- diagrams/__init__.py | 37 +++++++++++++++++++++---------------- docs/guides/diagram.md | 12 +++++++++++- tests/test_diagram.py | 9 +++++++++ 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/diagrams/__init__.py b/diagrams/__init__.py index a9e0f0c1..147cd0a8 100644 --- a/diagrams/__init__.py +++ b/diagrams/__init__.py @@ -40,7 +40,7 @@ def setcluster(cluster): class Diagram: __directions = ("TB", "BT", "LR", "RL") __curvestyles = ("ortho", "curved") - __outformats = ("png", "jpg", "svg", "pdf") + __outformats = ("png", "jpg", "svg", "pdf", "dot") # fmt: off _default_graph_attrs = { @@ -127,8 +127,13 @@ class Diagram: 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') + if isinstance(outformat, list): + for one_format in outformat: + if not self._validate_outformat(one_format): + raise ValueError(f'"{one_format}" is not a valid output format') + else: + if not self._validate_outformat(outformat): + raise ValueError(f'"{outformat}" is not a valid output format') self.outformat = outformat # Merge passed in attributes @@ -156,23 +161,20 @@ class Diagram: def _validate_direction(self, direction: str) -> bool: direction = direction.upper() - for v in self.__directions: - if v == direction: - return True + if direction in self.__directions: + return True return False def _validate_curvestyle(self, curvestyle: str) -> bool: curvestyle = curvestyle.lower() - for v in self.__curvestyles: - if v == curvestyle: - return True + if curvestyle in self.__curvestyles: + return True return False def _validate_outformat(self, outformat: str) -> bool: outformat = outformat.lower() - for v in self.__outformats: - if v == outformat: - return True + if outformat in self.__outformats: + return True return False def node(self, nodeid: str, label: str, **attrs) -> None: @@ -188,7 +190,11 @@ class Diagram: self.dot.subgraph(dot) def render(self) -> None: - self.dot.render(format=self.outformat, view=self.show, quiet=True) + if isinstance(self.outformat, list): + for one_format in self.outformat: + self.dot.render(format=one_format, view=self.show, quiet=True) + else: + self.dot.render(format=self.outformat, view=self.show, quiet=True) class Cluster: @@ -260,9 +266,8 @@ class Cluster: def _validate_direction(self, direction: str): direction = direction.upper() - for v in self.__directions: - if v == direction: - return True + if direction in self.__directions: + return True return False def node(self, nodeid: str, label: str, **attrs) -> None: diff --git a/docs/guides/diagram.md b/docs/guides/diagram.md index 8420e361..de7f59c0 100644 --- a/docs/guides/diagram.md +++ b/docs/guides/diagram.md @@ -44,7 +44,7 @@ diag You can specify the output file format with `outformat` parameter. Default is **png**. -> (png, jpg, svg, and pdf) are allowed. +> (png, jpg, svg, pdf and dot) are allowed. ```python from diagrams import Diagram @@ -54,6 +54,16 @@ with Diagram("Simple Diagram", outformat="jpg"): EC2("web") ``` +The `outformat` parameter also support list to output all the defined output in one call. + +```python +from diagrams import Diagram +from diagrams.aws.compute import EC2 + +with Diagram("Simple Diagram Multi Output", outformat=["jpg", "png", "dot"]): + EC2("web") +``` + You can specify the output filename with `filename` parameter. The extension shouldn't be included, it's determined by the `outformat` parameter. ```python diff --git a/tests/test_diagram.py b/tests/test_diagram.py index ad8558c5..f8e62ac2 100644 --- a/tests/test_diagram.py +++ b/tests/test_diagram.py @@ -107,6 +107,15 @@ class DiagramTest(unittest.TestCase): Node("node1") self.assertTrue(os.path.exists(f"{self.name}.png")) + def test_outformat_list(self): + """Check that outformat render all the files from the list.""" + self.name = 'diagrams_image' + with Diagram(show=False, outformat=["dot", "png"]): + Node("node1") + # both files must exist + self.assertTrue(os.path.exists(f"{self.name}.png")) + self.assertTrue(os.path.exists(f"{self.name}.dot")) + class ClusterTest(unittest.TestCase): def setUp(self):