Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Cluster icon and iconsize #853

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions diagrams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def getcluster() -> "Cluster":
def setcluster(cluster: "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:
__directions = ("TB", "BT", "LR", "RL")
Expand Down Expand Up @@ -197,7 +201,6 @@ def render(self) -> None:
else:
self.dot.render(format=self.outformat, view=self.show, quiet=True)


class Cluster:
__directions = ("TB", "BT", "LR", "RL")
__bgcolors = ("#E5F5FD", "#EBF3E7", "#ECE8F6", "#FDF7E3")
Expand Down Expand Up @@ -228,18 +231,27 @@ def __init__(
:param label: Cluster label.
:param direction: Data flow direction. Default is 'left to right'.
: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
"""
if graph_attr is None:
graph_attr = {}
self.label = label
self.name = "cluster_" + self.label
self.providerIconNode = providerIconNode
self.icon_size = icon_size

self.dot = Digraph(self.name)

# Set attributes.
# Set attributes
for k, v in self._default_graph_attrs.items():
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):
raise ValueError(f'"{direction}" is not a valid direction')
Expand Down Expand Up @@ -273,6 +285,7 @@ def __exit__(self, exc_type, exc_value, traceback):
def _validate_direction(self, direction: str) -> bool:
return direction.upper() in self.__directions


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

_height = 1.9

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

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

Expand Down
9 changes: 8 additions & 1 deletion tests/test_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from diagrams import Cluster, Diagram, Edge, Node
from diagrams import getcluster, getdiagram, setcluster, setdiagram

from diagrams.aws.network import VPC

class DiagramTest(unittest.TestCase):
def setUp(self):
Expand All @@ -23,6 +23,13 @@ def tearDown(self):
os.remove(self.name + ".png")
except FileNotFoundError:
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):
# Normal directions.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.