changeset 2:30d03e830354

compute line widths
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 28 Dec 2010 16:47:36 -0800
parents 084088505eea
children fc09a7ffb767
files example/example.py svgsitemap/middleware.py
diffstat 2 files changed, 13 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/example/example.py
+++ b/example/example.py
@@ -1,23 +1,23 @@
 import os
 from svgsitemap import *
 from webob import Request, Response
 
 def example(environ, start_response):
     link = '<a href="/%s">%s</a>'
     body = '<br/>'.join([link % (i,i) for i in range(30)])
-    body = '<html><body>%s</body></html>' % body
+    body = '<html><body><a href="/map">map</a><br/>%s</body></html>' % body
     response = Response(content_type='text/html', body=body)
     return response(environ, start_response)
 
 def factory():
     dirname = os.path.dirname(os.path.abspath(__file__))
     inifile = os.path.join(dirname, 'example.gv.txt')
     svgfile = os.path.join(dirname, 'example.svg')
     app = MapserverMiddleware(example, svgfile)
-    app = SVGSiteMap(app, file=inifile, output=svgfile)
+    app = SVGSiteMap(app, file=inifile, output=svgfile, name='foo.com')
     return app
 
 if __name__ == '__main__':
     from wsgiref import simple_server
     server = simple_server.make_server(host='0.0.0.0', port=int(54321), app=factory())
     server.serve_forever() 
--- a/svgsitemap/middleware.py
+++ b/svgsitemap/middleware.py
@@ -29,17 +29,18 @@ class MapserverMiddleware(object):
     
 
 class SVGSiteMap(object):
 
     ### class level variables
     defaults = { 'name': '',
                  'hosts': '',
                  'external_referers': True,
-                 'maxwidth': 20
+                 'maxwidth': 5,
+                 'minwidth': '0.01',
 
                  # input/output
                  'file': None, # .ini file
                  'output': None, # .svg file
 
                  # graph attributes
                  'bgcolor': 'black',
                  'fontcolor': 'white',
@@ -61,16 +62,17 @@ class SVGSiteMap(object):
 
         # set attrs from defaults
         for key in self.defaults:
             setattr(self, key, kw.get(key, self.defaults[key]))
 
         # sanity checks + data fixing
         assert self.output, "Please give an output file"
         assert self.file, "Cannot save file!"
+        self.maxwidth = float(self.maxwidth)
         if self.hosts:
             self.hosts = self.hosts.split()
         else:
             self.hosts = []
         if isinstance(self.external_referers, basestring):
             self.external_referers = self.external_referers.lower() == 'true'
 
         # open the graph
@@ -80,16 +82,17 @@ class SVGSiteMap(object):
                 count = int(edge.attr['label'])
                 self.edges[(edge[0], edge[1])] = count
                 if count > self.max:
                     self.max = count
         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
         self.graph.graph_attr['bgcolor'] = self.bgcolor
         self.graph.node_attr['color'] = self.nodecolor
         self.graph.node_attr['fontcolor'] = self.fontcolor
         self.graph.node_attr['fontname'] = self.fontname
         self.graph.node_attr['fontsize'] = self.fontsize
@@ -139,24 +142,27 @@ class SVGSiteMap(object):
         if (from_url, to_url) in self.edges:
             count = self.edges[(from_url, to_url)]
             count += 1
             if count > self.max:
                 self.max = count
             self.edges[(from_url, to_url)] = count
             edge = self.graph.get_edge(from_url, to_url)
             edge.attr['label'] =  str(count)
-            width = self.maxwidth * (count / self.max)
-            if not width:
-                width = 1
-            edge.attr['style'] = 'setlinewidth(%d)' % width
         else:
             self.edges[(from_url, to_url)] = 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
+
         for url in from_url, to_url:
             node = self.graph.get_node(url)
             node.attr['label'] = url
             node.attr['href'] = url
 
         if self.file:
             self.graph.write(self.file)
         if self.output: