From ddb62d2eef872329c5efe9ed780120eb11e11419 Mon Sep 17 00:00:00 2001 From: David Fraser Date: Wed, 14 Dec 2016 19:02:59 +0200 Subject: [PATCH] Handle unsafe IDs --- graph.py | 13 +++++++++++++ writers.py | 16 ++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/graph.py b/graph.py index b793af55..a14196c0 100644 --- a/graph.py +++ b/graph.py @@ -3,6 +3,8 @@ from colors import Colorizer +UNSAFE_IDS = {"graph"} + class GraphNode(object): """ A node. @@ -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): """ @@ -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) diff --git a/writers.py b/writers.py index 6a5f4f8d..643d0e2b 100644 --- a/writers.py +++ b/writers.py @@ -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 @@ -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): @@ -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 {" @@ -201,7 +201,7 @@ def start_graph(self): def start_subgraph(self, graph): self.log('Start subgraph %s' % graph.label) - self.write('' % graph.id) + self.write('' % graph.safe_id) self.indent() self.write('') self.indent() @@ -224,7 +224,7 @@ def start_subgraph(self, graph): self.write('') self.dedent() self.write('') - self.write('' % graph.id) + self.write('' % graph.safe_id) def finish_subgraph(self, graph): self.log('Finish subgraph %s' % graph.label) @@ -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) + self.write('' % node.safe_id) self.indent() self.write('') self.indent() @@ -263,7 +263,7 @@ def write_edge(self, edge): target = edge.target self.write( '' - % (self.edge_id, source.id, target.id)) + % (self.edge_id, source.safe_id, target.safe_id)) self.indent() self.write('') self.indent()