Browse Source

feat(option): add curvestyle option (ortho or curved)

tags/v0.15.0
mingrammer 4 years ago
parent
commit
f8ac547492
2 changed files with 24 additions and 0 deletions
  1. +14
    -0
      diagrams/__init__.py
  2. +10
    -0
      tests/test_diagram.py

+ 14
- 0
diagrams/__init__.py View File

@@ -39,6 +39,7 @@ def setcluster(cluster):


class Diagram: class Diagram:
__directions = ("TB", "BT", "LR", "RL") __directions = ("TB", "BT", "LR", "RL")
__curvestyles = ("ortho", "curved")
__outformats = ("png", "jpg", "svg", "pdf") __outformats = ("png", "jpg", "svg", "pdf")


# fmt: off # fmt: off
@@ -78,6 +79,7 @@ class Diagram:
name: str = "", name: str = "",
filename: str = "", filename: str = "",
direction: str = "LR", direction: str = "LR",
curvestyle: str = "ortho",
outformat: str = "png", outformat: str = "png",
show: bool = True, show: bool = True,
graph_attr: dict = {}, graph_attr: dict = {},
@@ -91,6 +93,7 @@ class Diagram:
:param filename: The output filename, without the extension (.png). :param filename: The output filename, without the extension (.png).
If not given, it will be generated from the name. If not given, it will be generated from the name.
:param direction: Data flow direction. Default is 'left to right'. :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 outformat: Output file format. Default is 'png'.
:param show: Open generated image after save if true, just only save otherwise. :param show: Open generated image after save if true, just only save otherwise.
:param graph_attr: Provide graph_attr dot config attributes. :param graph_attr: Provide graph_attr dot config attributes.
@@ -117,6 +120,10 @@ class Diagram:
raise ValueError(f'"{direction}" is not a valid direction') raise ValueError(f'"{direction}" is not a valid direction')
self.dot.graph_attr["rankdir"] = direction self.dot.graph_attr["rankdir"] = direction


if not self._validate_curvestyle(curvestyle):
raise ValueError(f'"{curvestyle}" is not a valid curvestyle')
self.dot.graph_attr["splines"] = curvestyle

if not self._validate_outformat(outformat): if not self._validate_outformat(outformat):
raise ValueError(f'"{outformat}" is not a valid output format') raise ValueError(f'"{outformat}" is not a valid output format')
self.outformat = outformat self.outformat = outformat
@@ -151,6 +158,13 @@ class Diagram:
return True return True
return False return False


def _validate_curvestyle(self, curvestyle: str) -> bool:
curvestyle = curvestyle.lower()
for v in self.__curvestyles:
if v == curvestyle:
return True
return False

def _validate_outformat(self, outformat: str) -> bool: def _validate_outformat(self, outformat: str) -> bool:
outformat = outformat.lower() outformat = outformat.lower()
for v in self.__outformats: for v in self.__outformats:


+ 10
- 0
tests/test_diagram.py View File

@@ -33,6 +33,16 @@ class DiagramTest(unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
Diagram(direction=dir) Diagram(direction=dir)


def test_validate_curvestyle(self):
# Normal directions.
for cvs in ("ortho", "curved"):
Diagram(curvestyle=cvs)

# Invalid directions.
for cvs in ("tangent", "unknown"):
with self.assertRaises(ValueError):
Diagram(curvestyle=cvs)

def test_validate_outformat(self): def test_validate_outformat(self):
# Normal output formats. # Normal output formats.
for fmt in ("png", "jpg", "svg", "pdf"): for fmt in ("png", "jpg", "svg", "pdf"):


Loading…
Cancel
Save