comparison svgsitemap/middleware.py @ 0:7a60bacc6a22

initial commit
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 27 Dec 2010 22:09:09 -0800
parents
children 084088505eea
comparison
equal deleted inserted replaced
-1:000000000000 0:7a60bacc6a22
1 """
2 request dispatcher
3 """
4
5 __all__ = ['MapserverMiddleware', 'SVGSiteMap']
6
7 import os
8 from pygraphviz import AGraph
9 from webob import Request, exc
10
11 class MapserverMiddleware(object):
12 """silly middleware to serve just the svg"""
13 def __init__(self, app, svgmap, path='/map'):
14 self.app
15 self.svgmap = svgmap
16 self.path = path
17
18 def __call__(self, environ, start_response):
19 request = Request(environ)
20 if request.path_info == self.path or not self.path:
21 content = file(self.svgmap).read()
22 return Response(content_type='image/svg+xml', body=content)
23 return app(environ, start_response)
24
25
26 class SVGSiteMap(object):
27
28 ### class level variables
29 defaults = { 'app': None,
30 'name': 'k0s.org',
31 'file': None,
32 'output': None,
33 'bgcolor': 'black',
34 'fontcolor': 'white',
35 'fontname': 'Helvetica',
36 'nodecolor': 'aqua',
37 'edgecolor': 'lime',
38 'shape': 'box'
39 }
40
41 def __init__(self, app, **kw):
42 self.app = app
43 self.edges = {}
44 for key in self.defaults:
45 setattr(self, key, kw.get(key, self.defaults[key]))
46 assert self.output, "Please give an output file"
47 assert self.file, "Cannot save file!"
48 if os.path.exists(self.file):
49 self.graph = AGraph(self.file, directed=True)
50 for edge in self.graph.edges():
51 self.edges[(edge[0], edge[1])] = int(edge.attr['label'])
52 else:
53 self.graph = AGraph(directed=True)
54 self.graph.graph_attr['title'] = self.name
55 self.graph.graph_attr['label'] = self.name
56 self.graph.graph_attr['fontname'] = self.fontname
57 self.graph.graph_attr['fontcolor'] = self.fontcolor
58 self.graph.graph_attr['bgcolor'] = self.bgcolor
59 self.graph.node_attr['color'] = self.nodecolor
60 self.graph.node_attr['fontcolor'] = self.fontcolor
61 self.graph.node_attr[
62 self.graph.node_attr['shape'] = self.shape
63 self.graph.edge_attr['color'] = self.edgecolor
64 self.graph.edge_attr['fontcolor'] = self.fontcolor
65
66 ### methods dealing with HTTP
67 def __call__(self, environ, start_response):
68 request = Request(environ)
69 import pdb; pdb.set_trace()
70 return self.app(environ, start_response)
71
72 def add(self, from_url, to_url):
73 if (from_url, to_url) in self.edges:
74 count = self.edges[(from_url, to_url)]
75 count += 1
76 self.edges[(from_url, to_url)] = count
77 edge = self.graph.get_edge(from_url, to_url)
78 edge.attr['label'] = str(count)
79 else:
80 self.edges[(from_url, to_url)] = 1
81 self.graph.add_edge(from_url, to_url, label='1')
82
83 for url in from_url, to_url:
84 node = self.graph.get_node(url)
85 node.attr['label'] = url
86 node.attr['href'] = url
87
88 if self.file:
89 self.graph.write(self.file)
90 if self.output:
91 self.graph.draw(self.output, prog='dot')