comparison svgsitemap/middleware.py @ 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
comparison
equal deleted inserted replaced
5:65acb32e8dc3 6:a88a670c92d0
4 4
5 __all__ = ['MapserverMiddleware', 'SVGSiteMap'] 5 __all__ = ['MapserverMiddleware', 'SVGSiteMap']
6 6
7 import os 7 import os
8 import urlparse 8 import urlparse
9 from fnmatch import fnmatch
9 from pygraphviz import AGraph 10 from pygraphviz import AGraph
10 from webob import Request, Response, exc 11 from webob import Request, Response, exc
11 12
12 class MapserverMiddleware(object): 13 class MapserverMiddleware(object):
13 """silly middleware to serve just the svg""" 14 """silly middleware to serve just the svg"""
34 defaults = { 'name': '', 35 defaults = { 'name': '',
35 'hosts': '', 36 'hosts': '',
36 'external_referers': True, 37 'external_referers': True,
37 'maxwidth': 5, 38 'maxwidth': 5,
38 'minwidth': '0.01', 39 'minwidth': '0.01',
40 'excluded': '*.css *.js */static/* /css/* *.ico /backgrounds/*',
39 41
40 # input/output 42 # input/output
41 'file': None, # .ini file 43 'file': None, # graphviz file
42 'output': None, # .svg file 44 'output': None, # .svg file
43 45
44 # graph attributes 46 # graph attributes
45 'bgcolor': 'black', 47 'bgcolor': 'black',
46 'fontcolor': 'white', 48 'fontcolor': 'white',
66 68
67 # sanity checks + data fixing 69 # sanity checks + data fixing
68 assert self.output, "Please give an output file" 70 assert self.output, "Please give an output file"
69 assert self.file, "Cannot save file!" 71 assert self.file, "Cannot save file!"
70 self.maxwidth = float(self.maxwidth) 72 self.maxwidth = float(self.maxwidth)
73 if isinstance(self.excluded, basestring):
74 self.excluded = self.excluded.split()
71 if self.hosts: 75 if self.hosts:
72 self.hosts = self.hosts.split() 76 self.hosts = self.hosts.split()
73 else: 77 else:
74 self.hosts = [] 78 self.hosts = []
75 if isinstance(self.external_referers, basestring): 79 if isinstance(self.external_referers, basestring):
77 81
78 # open the graph 82 # open the graph
79 if os.path.exists(self.file): 83 if os.path.exists(self.file):
80 self.graph = AGraph(self.file, name=self.name, splines=False, directed=True) 84 self.graph = AGraph(self.file, name=self.name, splines=False, directed=True)
81 for edge in self.graph.edges(): 85 for edge in self.graph.edges():
86 if self.exclude(edge[0], edge[1]):
87 self.graph.remove_edge(edge[0], edge[1])
88 continue
82 count = int(edge.attr['label']) 89 count = int(edge.attr['label'])
83 self.edges[(edge[0], edge[1])] = count 90 self.edges[(edge[0], edge[1])] = count
84 if count > self.max: 91 if count > self.max:
85 self.max = count 92 self.max = count
93 for node in self.graph.nodes():
94 if not self.graph.neighbors(node) or self.exclude(node):
95 self.graph.remove_node(node)
96 self.set_widths()
97 self.graph.write(self.file)
98 self.graph.draw(self.output, prog='neato')
86 else: 99 else:
87 self.graph = AGraph(name=self.name, splines=False, directed=True) 100 self.graph = AGraph(name=self.name, splines=False, directed=True)
88 101
89 # make it pretty 102 # make it pretty
90 self.graph.graph_attr['name'] = self.name 103 self.graph.graph_attr['name'] = self.name
136 def add(self, from_url, to_url): 149 def add(self, from_url, to_url):
137 """add a conncection in the graph""" 150 """add a conncection in the graph"""
138 151
139 if from_url == to_url: 152 if from_url == to_url:
140 return # don't do self-references 153 return # don't do self-references
154 if self.exclude(from_url, to_url):
155 return # ignore certain urls
141 156
142 if (from_url, to_url) in self.edges: 157 if (from_url, to_url) in self.edges:
143 count = self.edges[(from_url, to_url)] 158 count = self.edges[(from_url, to_url)]
144 count += 1 159 count += 1
145 if count > self.max: 160 if count > self.max:
150 else: 165 else:
151 self.edges[(from_url, to_url)] = 1 166 self.edges[(from_url, to_url)] = 1
152 self.max = 1 167 self.max = 1
153 self.graph.add_edge(from_url, to_url, label='1') 168 self.graph.add_edge(from_url, to_url, label='1')
154 169
155 for edge in self.graph.edges(): 170 if self.maxwidth:
156 count = self.edges[(edge[0], edge[1])] 171 self.set_widths()
157 width = self.maxwidth * count / self.max
158 if not width:
159 width = self.minwidth
160 edge.attr['style'] = 'setlinewidth(%s)' % width
161 172
162 for url in from_url, to_url: 173 for url in from_url, to_url:
163 node = self.graph.get_node(url) 174 node = self.graph.get_node(url)
164 node.attr['label'] = url 175 node.attr['label'] = url
165 node.attr['href'] = url 176 node.attr['href'] = url
166 177
167 if self.file: 178 if self.file:
168 self.graph.write(self.file) 179 self.graph.write(self.file)
169 if self.output: 180 if self.output:
170 self.graph.draw(self.output, prog='neato') 181 self.graph.draw(self.output, prog='neato')
182
183 def exclude(self, *urls):
184 """tell whether the edge is excluded"""
185 for pattern in self.excluded:
186 for url in urls:
187 if fnmatch(url, pattern):
188 return True
189 return False
190
191 def set_widths(self):
192 if self.maxwidth:
193 for edge in self.graph.edges():
194 count = self.edges[(edge[0], edge[1])]
195 width = self.maxwidth * count / self.max
196 if not width:
197 width = self.minwidth
198 edge.attr['style'] = 'setlinewidth(%s)' % width
199
200 else:
201 for edge in self.graph.edges():
202 edge.attr['style'] = ''