Skip to content

Commit

Permalink
Merge pull request #91 from Jsoconno/feature-parent-cluster-connections
Browse files Browse the repository at this point in the history
Nested Cluster Connection Functionality
  • Loading branch information
jsoconno authored Jan 26, 2021
2 parents eed2952 + a8f156e commit dfdeb21
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
44 changes: 24 additions & 20 deletions architectures/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,22 @@ def update_state(state: dict, target_key: Union[Cluster, Node], target_value: Un
return {k: v}


def search_state(search_dict: dict, search_key: Cluster) -> list:
def search_state(search_dict: dict, search_key: Cluster, output: list = []) -> list:
"""
Search nested dicts and lists for the search_value (key)
and return the corresponding value.
"""
# Get the value of the matching key

for k, v in search_dict.items():
if k is search_key:
break
elif isinstance(v, list):
if k is not search_key and isinstance(v, list):
for item in v:
if isinstance(item, dict):
v = search_state(item, search_key)
search_state(item, search_key, output)
else:
for item in v:
output.append(item)

# Create a list of nodes to output
node_list = [item for item in v if isinstance(item, Node)]
return node_list
return output

def wrap_text(text: str, max_length: int = 16) -> str:
"""Return a new label with wrapped text.
Expand Down Expand Up @@ -174,15 +173,20 @@ def get_node_obj(obj: Union[Cluster, Node]) -> Node:
"""
if isinstance(obj, Cluster):
state = get_state()
values = search_state(state, obj)
count = len(values)
center_node_index = round(count/2) - 1
obj = values[center_node_index]
return obj
elif isinstance(obj, Node):
return obj
else:
raise TypeError("The Edge object only accepts Clusters, Groups, and Nodes.")
values = search_state(state, obj, [])
print(values)
if any(isinstance(x, Node) for x in values):
node_list = [item for item in values if isinstance(item, Node)]
count = len(node_list)
center_node_index = round(count/2) - 1
obj = node_list[center_node_index]
else:
for item in values:
if isinstance(item, dict):
for k, v in item.items():
return get_node_obj(k)

return obj


def get_cluster_obj(obj: Union[Cluster, Node]) -> Cluster:
Expand Down Expand Up @@ -255,8 +259,8 @@ def __init__(self, name: str = "my-architecture",
# Set initial state to just the Graph
set_state({self: []})

def __str__(self) -> str:
return str(self.dot)
# def __str__(self) -> str:
# return str(self.dot)

def __enter__(self) -> Graph:
set_graph(self)
Expand Down
15 changes: 6 additions & 9 deletions examples/testing.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
from architectures.core import Graph, Cluster, Group, Node, Edge, Flow

# Import the LightMode theme
from architectures.themes import Default, LightMode

theme = LightMode(graph_attr_overrides={"rankdir": "TB"})

with Graph("My Graph", theme=theme, show=True) as graph:
with Graph("My Graph Broken", theme=LightMode(), show=True) as graph:
with Cluster("A") as cluster_a:
a = Node("A")
b = Node("B")

with Cluster("B") as cluster_b:
c = Node("C")
with Cluster("A"):
d = Node("D")
with Cluster("C") as cluster_c:
with Cluster("D") as cluster_d:
c = Node("C")
d = Node("D")

Flow([[a, b], cluster_b])
Edge(cluster_a, cluster_b)

0 comments on commit dfdeb21

Please sign in to comment.