changeset 6:a88a670c92d0

dont display things that you dont want
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 28 Dec 2010 20:27:12 -0800
parents 65acb32e8dc3
children 145a4d85b37d
files svgsitemap/middleware.py
diffstat 1 files changed, 39 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/svgsitemap/middleware.py	Tue Dec 28 17:14:34 2010 -0800
+++ b/svgsitemap/middleware.py	Tue Dec 28 20:27:12 2010 -0800
@@ -6,6 +6,7 @@
 
 import os
 import urlparse
+from fnmatch import fnmatch
 from pygraphviz import AGraph
 from webob import Request, Response, exc
 
@@ -36,9 +37,10 @@
                  'external_referers': True,
                  'maxwidth': 5,
                  'minwidth': '0.01',
+                 'excluded': '*.css *.js */static/* /css/* *.ico /backgrounds/*',
 
                  # input/output
-                 'file': None, # .ini file
+                 'file': None, # graphviz file
                  'output': None, # .svg file
 
                  # graph attributes
@@ -68,6 +70,8 @@
         assert self.output, "Please give an output file"
         assert self.file, "Cannot save file!"
         self.maxwidth = float(self.maxwidth)
+        if isinstance(self.excluded, basestring):
+            self.excluded = self.excluded.split()
         if self.hosts:
             self.hosts = self.hosts.split()
         else:
@@ -79,10 +83,19 @@
         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.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)
 
@@ -138,6 +151,8 @@
 
         if from_url == to_url:
             return # don't do self-references
+        if self.exclude(from_url, to_url):
+            return # ignore certain urls
         
         if (from_url, to_url) in self.edges:
             count = self.edges[(from_url, to_url)]
@@ -152,12 +167,8 @@
             self.max = 1
             self.graph.add_edge(from_url, to_url, label='1')
 
-        for edge in self.graph.edges():
-            count = self.edges[(edge[0], edge[1])]
-            width = self.maxwidth * count / self.max
-            if not width:
-                width = self.minwidth
-            edge.attr['style'] = 'setlinewidth(%s)' % width
+        if self.maxwidth:
+            self.set_widths()
 
         for url in from_url, to_url:
             node = self.graph.get_node(url)
@@ -168,3 +179,24 @@
             self.graph.write(self.file)
         if self.output:
             self.graph.draw(self.output, prog='neato')
+
+    def exclude(self, *urls):
+        """tell whether the edge is excluded"""
+        for pattern in self.excluded:
+            for url in urls:
+                if fnmatch(url, pattern):
+                    return True
+        return False
+
+    def set_widths(self):
+        if self.maxwidth:
+            for edge in self.graph.edges():
+                count = self.edges[(edge[0], edge[1])]
+                width = self.maxwidth * count / self.max
+                if not width:
+                    width = self.minwidth
+                edge.attr['style'] = 'setlinewidth(%s)' % width
+
+        else:
+            for edge in self.graph.edges():
+                edge.attr['style'] = ''