changeset 10:1329127709de

remove orphans in a unique function
author Jeff Hammel <jhammel@mozilla.com>
date Sun, 02 Jan 2011 11:43:27 -0800
parents aa4eab6dc994
children 1ff7b363e89e
files svgsitemap/middleware.py
diffstat 1 files changed, 11 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/svgsitemap/middleware.py
+++ b/svgsitemap/middleware.py
@@ -82,26 +82,23 @@ class SVGSiteMap(object):
 
         # open the graph
         if os.path.exists(self.file):
             self.graph = AGraph(self.file, name=self.name, splines=False, directed=True)
             for edge in self.graph.edges():
                 if self.exclude(edge[0], edge[1]):
                     self.graph.remove_edge(edge[0], edge[1])
                     continue
+                
                 count = int(edge.attr['label'])
                 self.edges[(edge[0], edge[1])] = count
                 if count > self.max:
                     self.max = count
-            for node in self.graph.nodes():
-                if not self.graph.neighbors(node) or self.exclude(node):
-                    self.graph.remove_node(node)
+            self.remove_orphans()
             self.set_widths()
-            self.graph.write(self.file)
-            self.graph.draw(self.output, prog='neato')
         else:
             self.graph = AGraph(name=self.name, splines=False, directed=True)
 
         # make it pretty
         self.graph.graph_attr['name'] = self.name
         self.graph.graph_attr['label'] = self.name
         self.graph.graph_attr['fontname'] = self.fontname
         self.graph.graph_attr['fontcolor'] = self.fontcolor
@@ -203,8 +200,17 @@ class SVGSiteMap(object):
             for edge in self.graph.edges():
                 edge.attr['style'] = ''
 
     def save(self):
         if self.file:
             self.graph.write(self.file)
         if self.output:
             self.graph.draw(self.output, prog='neato')
+
+    def remove_orphans(self):
+        flag = True
+        while flag:
+            flag = False
+            for node in self.graph.nodes():
+                if not self.graph.neighbors(node) or self.exclude(node):
+                    flag = True
+                    self.graph.remove_node(node)