"""Facility to render node graphs using pydot"""
import os

import pydot
from kitchen.settings import STATIC_ROOT, REPO


def generate_node_map(nodes):
    """Generates a graphviz nodemap"""
    graph = pydot.Dot(graph_type='digraph')
    graph_nodes = {}
    # Create nodes
    for node in nodes:
        label = node['name'] + "\n" + "\n".join(
            [role for role in node['role'] \
                if not role.startswith(REPO['EXCLUDE_ROLE_PREFIX'])])
        node_el = pydot.Node(label,
                             shape="box",
                             style="filled",
                             fillcolor="lightyellow",
                             fontsize="8")
        graph_nodes[node['name']] = node_el
        graph.add_node(node_el)
    # Create links
    for node in nodes:
        for attr in node.keys():
            if isinstance(node[attr], dict) and 'client_roles' in node[attr]:
                for client_node in nodes:
                    if set.intersection(set(node[attr]['client_roles']),
                                        set(client_node['roles'])):
                        edge = pydot.Edge(graph_nodes[client_node['name']],
                                          graph_nodes[node['name']],
                                          fontsize="7")
                        edge.set_label(attr)
                        graph.add_edge(edge)
    # Generate graph
    graph.write_png(os.path.join(STATIC_ROOT, 'img', 'node_map.png'))

@@ -842,78 +842,138 @@
     
-if isinstance(node%5Battr%5D, dict) and 'client_roles' in node%5Battr%5D:%0A    
+try:%0A                client_roles = node%5Battr%5D%5B'client_roles'%5D%0A            except (TypeError, KeyError):%0A                continue%0A
     
@@ -1022,20 +1022,16 @@
         
-    
 if set.i
@@ -1046,42 +1046,9 @@
 ion(
-set(node%5Battr%5D%5B'client_roles'%5D),%0A 
+%0A
     
@@ -1067,30 +1067,34 @@
         
-              
+set(client_roles),
  set(cli
@@ -1114,20 +1114,16 @@
 es'%5D)):%0A
-    
         
@@ -1220,28 +1220,24 @@
             
-   
- 
 graph_nodes%5B
@@ -1243,36 +1243,32 @@
 %5Bnode%5B'name'%5D%5D,%0A
-    
                 
@@ -1319,28 +1319,24 @@
             
-    
 edge.set_lab
@@ -1344,20 +1344,16 @@
 l(attr)%0A
-    
         

Change to "ask for forgiveness", as the 'client_roles' condition could get too complicated