feat(output): Generate dot file and support multi outformat.(#441)

pull/446/head
tessier 5 years ago
parent c2b10f3924
commit de45ff3638

@ -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:

@ -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

@ -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):

Loading…
Cancel
Save