Browse Source

Added Cluster icon and iconsize

pull/853/head
Albert Tanure 1 year ago
parent
commit
56d54b4c17
Failed to extract signature
4 changed files with 29 additions and 4 deletions
  1. +21
    -3
      diagrams/__init__.py
  2. +8
    -1
      tests/test_diagram.py
  3. BIN
      website/static/img/resources/onprem/registry/jfrog.png
  4. BIN
      website/static/img/resources/programming/framework/svelte.png

+ 21
- 3
diagrams/__init__.py View File

@@ -36,6 +36,10 @@ def getcluster() -> "Cluster":
def setcluster(cluster: "Cluster"): def setcluster(cluster: "Cluster"):
__cluster.set(cluster) __cluster.set(cluster)


def getIconLabel(iconPath: str = '', iconSize: int = 30, label: str = ''):
return '<<table border="0" width="100%"><tr><td fixedsize="true" width="' + str(
iconSize) + '" height="' + str(
iconSize) + '"><img src="' + iconPath + '" /></td><td>' + label + '</td></tr></table>>'


class Diagram: class Diagram:
__directions = ("TB", "BT", "LR", "RL") __directions = ("TB", "BT", "LR", "RL")
@@ -191,7 +195,6 @@ class Diagram:
else: else:
self.dot.render(format=self.outformat, view=self.show, quiet=True) self.dot.render(format=self.outformat, view=self.show, quiet=True)



class Cluster: class Cluster:
__directions = ("TB", "BT", "LR", "RL") __directions = ("TB", "BT", "LR", "RL")
__bgcolors = ("#E5F5FD", "#EBF3E7", "#ECE8F6", "#FDF7E3") __bgcolors = ("#E5F5FD", "#EBF3E7", "#ECE8F6", "#FDF7E3")
@@ -216,22 +219,33 @@ class Cluster:
label: str = "cluster", label: str = "cluster",
direction: str = "LR", direction: str = "LR",
graph_attr: dict = {}, graph_attr: dict = {},
providerIconNode: object = None,
icon_size: int = 30,
): ):
"""Cluster represents a cluster context. """Cluster represents a cluster context.


:param label: Cluster label. :param label: Cluster label.
:param direction: Data flow direction. Default is 'left to right'. :param direction: Data flow direction. Default is 'left to right'.
:param graph_attr: Provide graph_attr dot config attributes. :param graph_attr: Provide graph_attr dot config attributes.
:param providerIconNode: Provide a node to be included as a link
:param icon_size: The icon size
""" """
self.label = label self.label = label
self.name = "cluster_" + self.label self.name = "cluster_" + self.label
self.providerIconNode = providerIconNode
self.icon_size = icon_size


self.dot = Digraph(self.name) self.dot = Digraph(self.name)


# Set attributes.
# Set attributes
for k, v in self._default_graph_attrs.items(): for k, v in self._default_graph_attrs.items():
self.dot.graph_attr[k] = v self.dot.graph_attr[k] = v
self.dot.graph_attr["label"] = self.label

if self.providerIconNode:
_iconNode = self.providerIconNode() #load the object
self.dot.graph_attr["label"] = getIconLabel(_iconNode._load_icon(), self.icon_size, self.label)
else:
self.dot.graph_attr["label"] = self.label


if not self._validate_direction(direction): if not self._validate_direction(direction):
raise ValueError(f'"{direction}" is not a valid direction') raise ValueError(f'"{direction}" is not a valid direction')
@@ -265,6 +279,7 @@ class Cluster:
def _validate_direction(self, direction: str) -> bool: def _validate_direction(self, direction: str) -> bool:
return direction.upper() in self.__directions return direction.upper() in self.__directions



def node(self, nodeid: str, label: str, **attrs) -> None: def node(self, nodeid: str, label: str, **attrs) -> None:
"""Create a new node in the cluster.""" """Create a new node in the cluster."""
self.dot.node(nodeid, label=label, **attrs) self.dot.node(nodeid, label=label, **attrs)
@@ -284,6 +299,9 @@ class Node:


_height = 1.9 _height = 1.9


def __new__(cls, *args, **kwargs):
return object.__new__(cls)

def __init__(self, label: str = "", *, nodeid: str = None, **attrs: Dict): def __init__(self, label: str = "", *, nodeid: str = None, **attrs: Dict):
"""Node represents a system component. """Node represents a system component.




+ 8
- 1
tests/test_diagram.py View File

@@ -5,7 +5,7 @@ import pathlib


from diagrams import Cluster, Diagram, Edge, Node from diagrams import Cluster, Diagram, Edge, Node
from diagrams import getcluster, getdiagram, setcluster, setdiagram from diagrams import getcluster, getdiagram, setcluster, setdiagram
from diagrams.aws.network import VPC


class DiagramTest(unittest.TestCase): class DiagramTest(unittest.TestCase):
def setUp(self): def setUp(self):
@@ -23,6 +23,13 @@ class DiagramTest(unittest.TestCase):
os.remove(self.name + ".png") os.remove(self.name + ".png")
except FileNotFoundError: except FileNotFoundError:
pass pass
def test_cluster_icon(self):
self.name = "example-cluster-icon"
with Diagram(name=self.name, show=False):
cluster = Cluster("example_cluster_icon", providerIconNode=VPC, icon_size=30)
self.assertIsNotNone(cluster)
self.assertTrue(os.path.exists(f"{self.name}.png"))


def test_validate_direction(self): def test_validate_direction(self):
# Normal directions. # Normal directions.


BIN
website/static/img/resources/onprem/registry/jfrog.png View File

Before After
Width: 256  |  Height: 256  |  Size: 26 KiB

BIN
website/static/img/resources/programming/framework/svelte.png View File

Before After
Width: 256  |  Height: 308  |  Size: 17 KiB

Loading…
Cancel
Save