Mercurial > hg > svgsitemap
view 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 |
line wrap: on
line source
""" request dispatcher """ __all__ = ['MapserverMiddleware', 'SVGSiteMap'] import os from pygraphviz import AGraph from webob import Request, exc class MapserverMiddleware(object): """silly middleware to serve just the svg""" def __init__(self, app, svgmap, path='/map'): self.app self.svgmap = svgmap self.path = path def __call__(self, environ, start_response): request = Request(environ) if request.path_info == self.path or not self.path: content = file(self.svgmap).read() return Response(content_type='image/svg+xml', body=content) return app(environ, start_response) class SVGSiteMap(object): ### class level variables defaults = { 'app': None, 'name': 'k0s.org', 'file': None, 'output': None, 'bgcolor': 'black', 'fontcolor': 'white', 'fontname': 'Helvetica', 'nodecolor': 'aqua', 'edgecolor': 'lime', 'shape': 'box' } def __init__(self, app, **kw): self.app = app self.edges = {} for key in self.defaults: setattr(self, key, kw.get(key, self.defaults[key])) assert self.output, "Please give an output file" assert self.file, "Cannot save file!" if os.path.exists(self.file): self.graph = AGraph(self.file, directed=True) for edge in self.graph.edges(): self.edges[(edge[0], edge[1])] = int(edge.attr['label']) else: self.graph = AGraph(directed=True) self.graph.graph_attr['title'] = 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[ self.graph.node_attr['shape'] = self.shape self.graph.edge_attr['color'] = self.edgecolor self.graph.edge_attr['fontcolor'] = self.fontcolor ### methods dealing with HTTP def __call__(self, environ, start_response): request = Request(environ) import pdb; pdb.set_trace() return self.app(environ, start_response) def add(self, from_url, to_url): if (from_url, to_url) in self.edges: count = self.edges[(from_url, to_url)] count += 1 self.edges[(from_url, to_url)] = count edge = self.graph.get_edge(from_url, to_url) edge.attr['label'] = str(count) else: self.edges[(from_url, to_url)] = 1 self.graph.add_edge(from_url, to_url, label='1') 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: self.graph.draw(self.output, prog='dot')