Skip to content

Commit

Permalink
feat: added customization of style
Browse files Browse the repository at this point in the history
  • Loading branch information
tsypuk committed Aug 17, 2023
1 parent f58c658 commit 84a6ca3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 20 deletions.
26 changes: 19 additions & 7 deletions multicloud_diagrams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@
import yaml


def update_fill_color(style_str, node_color):
pattern = r'fillColor=([^;]+)'
# Replace the 'fillColor' value with the node_color
return re.sub(pattern, f'fillColor={node_color}', style_str)
def update_style_by_key(style_str: str, key: str, value: str):
pattern = r'{}=([^;]+)'.format(key)
if re.search(pattern, style_str) is None:
return f"{style_str}{key}={value};"
else:
return re.sub(pattern, f'{key}={value}', style_str)


def customize(node_template: dict, style: dict = None):
for (key, value) in style.items():
node_template['style'] = update_style_by_key(style_str=node_template['style'], key=key, value=value)


def stringify_dict(metadata: dict) -> str:
Expand Down Expand Up @@ -216,10 +223,12 @@ def prepare_table(self, table_id='', table_name='', fill_color='', rows_count=0,
# Position Vertex based on X,Y cords
self.update_vertex_coords_width_height_from_prev_version(mx_geometry, f'vertex:{table_id}:list')

def add_vertex(self, node_id: str, node_name: str, metadata: dict = None, node_type: str = '', layer_name: str = None, layer_id: str = None, fill_color: str = None,
def add_vertex(self, node_id: str, node_name: str, metadata: dict = None, node_type: str = '', layer_name: str = None, layer_id: str = None, style: dict = None,
x: int = None, y: int = None):
if metadata is None:
metadata = {}
if style is None:
style = {}
# check that there is no such vertex already
exist = False
for mx_cell in self.root:
Expand All @@ -231,8 +240,11 @@ def add_vertex(self, node_id: str, node_name: str, metadata: dict = None, node_t

if not exist:
node_template = self.get_node_template(node_type)
if fill_color is not None:
node_template['style'] = update_fill_color(node_template['style'], fill_color)

# Customization
customize(node_template=node_template, style=style)
# for (key, value) in style.items():
# node_template['style'] = update_style_by_key(style_str=node_template['style'], key=key, value=value)

parent_id = str(self.get_layer_id(layer_name, layer_id))
mx_cell = Et.SubElement(self.root,
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_color_red(self):
mcd.add_vertex(node_id="arn:aws:lambda:eu-west-1:123456789012:function:prod-lambda-name",
node_name='prod-lambda-name',
node_type='lambda_function',
fill_color='#FF0000')
style={'fillColor': '#FF0000'})

# then
# docs
Expand All @@ -32,7 +32,7 @@ def test_color_blue(self):
mcd.add_vertex(node_id="arn:aws:lambda:eu-west-1:123456789012:function:prod-lambda-name",
node_name='prod-lambda-name',
node_type='lambda_function',
fill_color='#0000FF')
style={'fillColor': '#0000FF'})

# then
# docs
Expand Down
7 changes: 4 additions & 3 deletions tests/test___init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from multicloud_diagrams import update_fill_color, stringify_dict, build_vertex_id
from multicloud_diagrams import update_style_by_key, stringify_dict, build_vertex_id


class TestMultiCloudDiagrams(TestCase):
Expand Down Expand Up @@ -80,11 +80,12 @@ def test_stringify_dict_empty(self):

def test_update_fill_color(self):
# given-when
result = update_fill_color(
result = update_style_by_key(
style_str=('sketch=0;outlineConnect=0;fontColor=#232F3E;gradientColor=#FF4F8B;gradientDirection=north;'
'fillColor=#BC1356;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;'
'align=left;html=1;fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.sns;'),
node_color='#FF0000')
key='fillColor',
value='#FF0000')
# then
self.assertEqual(
('sketch=0;outlineConnect=0;fontColor=#232F3E;gradientColor=#FF4F8B;gradientDirection=north;'
Expand Down
33 changes: 29 additions & 4 deletions tests/test_colors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from multicloud_diagrams import MultiCloudDiagrams
from multicloud_diagrams import MultiCloudDiagrams, update_style_by_key

from utils.utils import TestUtilities

Expand Down Expand Up @@ -32,7 +32,7 @@ def test_add_default_node_color(self):
def test_add_custom_node_color(self):
# given
mcd = MultiCloudDiagrams()
node_color = '#FF0000'
node_style = {'fillColor': '#FF0000'}

sns_arn = 'arn:aws:sns:eu-west-1:123456789012:internal.fifo'
metadata = {
Expand All @@ -42,7 +42,8 @@ def test_add_custom_node_color(self):
}

# when
mcd.add_vertex(node_id=sns_arn, node_name='internal.fifo', node_type='sns', metadata=metadata, fill_color=node_color)
mcd.add_vertex(node_id=sns_arn, node_name='internal.fifo', node_type='sns', metadata=metadata,
style=node_style)

# then
expected = {
Expand All @@ -53,4 +54,28 @@ def test_add_custom_node_color(self):
'parent': '1',
'vertex': '1'
}
self.verify_resource(expected, mcd.mx_file, 'internal.fifo', 'sns', fill_color=node_color)
self.verify_resource(expected, mcd.mx_file, 'internal.fifo', 'sns', style=node_style)

def test_update_fill_color(self):
# given
style = "sketch=0;outlineConnect=0;fontColor=#232F3E;gradientColor=#FF4F8B;gradientDirection=north;fillColor=#BC1356;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=left;html=1;fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.sqs;"
color = '#FF0000'

# when
result = update_style_by_key(style_str=style, key='fillColor', value=color)

# then
expected = "sketch=0;outlineConnect=0;fontColor=#232F3E;gradientColor=#FF4F8B;gradientDirection=north;fillColor=#FF0000;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=left;html=1;fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.sqs;"
self.assertEqual(expected, result)

def test_no_fill_color(self):
# given
style = "sketch=0;outlineConnect=0;fontColor=#232F3E;gradientColor=#FF4F8B;gradientDirection=north;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=left;html=1;fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.sqs;"
color = '#FF0000'

# when
result = update_style_by_key(style_str=style, key='fillColor', value=color)

# then
expected = "sketch=0;outlineConnect=0;fontColor=#232F3E;gradientColor=#FF4F8B;gradientDirection=north;strokeColor=#ffffff;dashed=0;verticalLabelPosition=bottom;verticalAlign=top;align=left;html=1;fontSize=12;fontStyle=0;aspect=fixed;shape=mxgraph.aws4.resourceIcon;resIcon=mxgraph.aws4.sqs;fillColor=#FF0000;"
self.assertEqual(expected, result)
10 changes: 6 additions & 4 deletions utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import xml.etree.ElementTree as Et

from multicloud_diagrams import update_fill_color
from multicloud_diagrams import customize


class TestUtilities(unittest.TestCase):
Expand Down Expand Up @@ -89,7 +89,9 @@ def verify_vertex_in_isolation(self, mx_cells):
self.verify_mx_cell(mx_cells[0], expected={'id': '0'})
self.verify_mx_cell(mx_cells[1], expected={'id': '1', 'parent': '0'})

def verify_resource(self, expected: dict, mx_file: Et.Element, resource_name, resource_type, debug_mode=False, fill_color=None):
def verify_resource(self, expected: dict, mx_file: Et.Element, resource_name, resource_type, debug_mode=False, style=None):
if style is None:
style = {}
tree = Et.ElementTree(mx_file)
self.verify_mxfile_default(tree)

Expand All @@ -103,8 +105,8 @@ def verify_resource(self, expected: dict, mx_file: Et.Element, resource_name, re
if 'style' in expected:
del expected['style']
expected['style'] = self.supported_vertex[resource_type]['style']
if fill_color is not None:
expected['style'] = update_fill_color(expected['style'], fill_color)

customize(expected, style=style)

children = mx_cells[2].findall("./*")

Expand Down

0 comments on commit 84a6ca3

Please sign in to comment.