Skip to content

Commit

Permalink
Handle unsafe IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
David Fraser committed Dec 14, 2016
1 parent 5f580ec commit ddb62d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
13 changes: 13 additions & 0 deletions graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from colors import Colorizer


UNSAFE_IDS = {"graph"}

class GraphNode(object):
"""
A node.
Expand Down Expand Up @@ -31,6 +33,11 @@ def __repr__(self):
else:
return 'GraphNode(' + repr(self.id) + ')'

@property
def safe_id(self):
if self.id in UNSAFE_IDS:
return "%sX" % self.id
return self.id

class GraphEdge(object):
"""
Expand Down Expand Up @@ -60,6 +67,12 @@ def __init__(
self.subgraphs = subgraphs or []
self.grouped = grouped

@property
def safe_id(self):
if self.id in UNSAFE_IDS:
return "%sX" % self.id
return self.id

@classmethod
def from_visitor(cls, visitor, options=None, logger=None):
colored = options.get('colored', False)
Expand Down
16 changes: 8 additions & 8 deletions writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def start_graph(self):
def start_subgraph(self, graph):
self.log('Start subgraph %s' % graph.label)
self.write(
"subgraph cluster_%s {\n" % graph.id)
"subgraph cluster_%s {\n" % graph.safe_id)
self.indent()

# translucent gray (no hue to avoid visual confusion with any
Expand All @@ -146,7 +146,7 @@ def write_node(self, node):
'%s [label="%s", style="filled", fillcolor="%s",'
' fontcolor="%s", group="%s"];'
% (
node.id, node.label,
node.safe_id, node.label,
node.fill_color, node.text_color, node.group))

def write_edge(self, edge):
Expand All @@ -156,11 +156,11 @@ def write_edge(self, edge):
self.write(
' %s -> %s [style="dashed",'
' color="azure4"];'
% (source.id, target.id))
% (source.safe_id, target.safe_id))
else:
self.write(
' %s -> %s;'
% (source.id, target.id))
% (source.safe_id, target.safe_id))

def finish_graph(self):
self.write('}') # terminate "digraph G {"
Expand Down Expand Up @@ -201,7 +201,7 @@ def start_graph(self):
def start_subgraph(self, graph):
self.log('Start subgraph %s' % graph.label)

self.write('<node id="%s:" yfiles.foldertype="group">' % graph.id)
self.write('<node id="%s:" yfiles.foldertype="group">' % graph.safe_id)
self.indent()
self.write('<data key="d0">')
self.indent()
Expand All @@ -224,7 +224,7 @@ def start_subgraph(self, graph):
self.write('</y:ProxyAutoBoundsNode>')
self.dedent()
self.write('</data>')
self.write('<graph edgedefault="directed" id="%s::">' % graph.id)
self.write('<graph edgedefault="directed" id="%s::">' % graph.safe_id)

def finish_subgraph(self, graph):
self.log('Finish subgraph %s' % graph.label)
Expand All @@ -236,7 +236,7 @@ def finish_subgraph(self, graph):
def write_node(self, node):
self.log('Write node %s' % node.label)
width = 20 + 10*len(node.label)
self.write('<node id="%s">' % node.id)
self.write('<node id="%s">' % node.safe_id)
self.indent()
self.write('<data key="d0">')
self.indent()
Expand All @@ -263,7 +263,7 @@ def write_edge(self, edge):
target = edge.target
self.write(
'<edge id="%s" source="%s" target="%s">'
% (self.edge_id, source.id, target.id))
% (self.edge_id, source.safe_id, target.safe_id))
self.indent()
self.write('<data key="d1">')
self.indent()
Expand Down

0 comments on commit ddb62d2

Please sign in to comment.