Mercurial > hg > svgsitemap
annotate svgsitemap/middleware.py @ 9:aa4eab6dc994
* dont set node width, height; * move save() to its own function
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sun, 02 Jan 2011 11:39:45 -0800 |
parents | 1d16bde33eee |
children | 1329127709de |
rev | line source |
---|---|
0 | 1 """ |
2 request dispatcher | |
3 """ | |
4 | |
5 __all__ = ['MapserverMiddleware', 'SVGSiteMap'] | |
6 | |
7 import os | |
1 | 8 import urlparse |
6
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
9 from fnmatch import fnmatch |
0 | 10 from pygraphviz import AGraph |
1 | 11 from webob import Request, Response, exc |
0 | 12 |
13 class MapserverMiddleware(object): | |
14 """silly middleware to serve just the svg""" | |
15 def __init__(self, app, svgmap, path='/map'): | |
1 | 16 self.app = app |
0 | 17 self.svgmap = svgmap |
18 self.path = path | |
19 | |
20 def __call__(self, environ, start_response): | |
21 request = Request(environ) | |
22 if request.path_info == self.path or not self.path: | |
1 | 23 if not os.path.exists(self.svgmap): |
24 res = exc.HTTPNotFound() | |
25 return res(environ, start_response) | |
0 | 26 content = file(self.svgmap).read() |
1 | 27 res = Response(content_type='image/svg+xml', body=content) |
28 return res(environ, start_response) | |
29 return self.app(environ, start_response) | |
0 | 30 |
31 | |
32 class SVGSiteMap(object): | |
33 | |
34 ### class level variables | |
1 | 35 defaults = { 'name': '', |
36 'hosts': '', | |
37 'external_referers': True, | |
2 | 38 'maxwidth': 5, |
39 'minwidth': '0.01', | |
7
145a4d85b37d
exclude long urls for now ::sigh::
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
40 'maxlength': 80, |
8
1d16bde33eee
take out some image too ::sigh::
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
41 'excluded': '*.css *.js */static/* /css/* *.ico /backgrounds/* *.png *.jpg', |
1 | 42 |
43 # input/output | |
6
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
44 'file': None, # graphviz file |
1 | 45 'output': None, # .svg file |
46 | |
47 # graph attributes | |
0 | 48 'bgcolor': 'black', |
49 'fontcolor': 'white', | |
50 'fontname': 'Helvetica', | |
1 | 51 'fontsize': '10.0', |
0 | 52 'nodecolor': 'aqua', |
53 'edgecolor': 'lime', | |
5 | 54 'shape': 'plaintext', |
1 | 55 'len': '1.3', |
56 'arrowsize': '0.5', | |
0 | 57 } |
58 | |
59 def __init__(self, app, **kw): | |
1 | 60 |
61 # boilerplate | |
0 | 62 self.app = app |
63 self.edges = {} | |
1 | 64 self.max = 0 |
65 | |
66 # set attrs from defaults | |
0 | 67 for key in self.defaults: |
68 setattr(self, key, kw.get(key, self.defaults[key])) | |
1 | 69 |
70 # sanity checks + data fixing | |
0 | 71 assert self.output, "Please give an output file" |
72 assert self.file, "Cannot save file!" | |
2 | 73 self.maxwidth = float(self.maxwidth) |
6
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
74 if isinstance(self.excluded, basestring): |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
75 self.excluded = self.excluded.split() |
1 | 76 if self.hosts: |
77 self.hosts = self.hosts.split() | |
78 else: | |
79 self.hosts = [] | |
80 if isinstance(self.external_referers, basestring): | |
81 self.external_referers = self.external_referers.lower() == 'true' | |
82 | |
83 # open the graph | |
0 | 84 if os.path.exists(self.file): |
1 | 85 self.graph = AGraph(self.file, name=self.name, splines=False, directed=True) |
0 | 86 for edge in self.graph.edges(): |
6
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
87 if self.exclude(edge[0], edge[1]): |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
88 self.graph.remove_edge(edge[0], edge[1]) |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
89 continue |
1 | 90 count = int(edge.attr['label']) |
91 self.edges[(edge[0], edge[1])] = count | |
92 if count > self.max: | |
93 self.max = count | |
6
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
94 for node in self.graph.nodes(): |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
95 if not self.graph.neighbors(node) or self.exclude(node): |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
96 self.graph.remove_node(node) |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
97 self.set_widths() |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
98 self.graph.write(self.file) |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
99 self.graph.draw(self.output, prog='neato') |
0 | 100 else: |
1 | 101 self.graph = AGraph(name=self.name, splines=False, directed=True) |
102 | |
103 # make it pretty | |
2 | 104 self.graph.graph_attr['name'] = self.name |
0 | 105 self.graph.graph_attr['label'] = self.name |
106 self.graph.graph_attr['fontname'] = self.fontname | |
107 self.graph.graph_attr['fontcolor'] = self.fontcolor | |
108 self.graph.graph_attr['bgcolor'] = self.bgcolor | |
109 self.graph.node_attr['color'] = self.nodecolor | |
110 self.graph.node_attr['fontcolor'] = self.fontcolor | |
1 | 111 self.graph.node_attr['fontname'] = self.fontname |
112 self.graph.node_attr['fontsize'] = self.fontsize | |
0 | 113 self.graph.node_attr['shape'] = self.shape |
114 self.graph.edge_attr['color'] = self.edgecolor | |
115 self.graph.edge_attr['fontcolor'] = self.fontcolor | |
1 | 116 self.graph.edge_attr['fontname'] = self.fontname |
117 self.graph.edge_attr['fontsize'] = self.fontsize | |
118 self.graph.edge_attr['len'] = self.len | |
119 self.graph.edge_attr['arrowsize'] = self.arrowsize | |
9
aa4eab6dc994
* dont set node width, height; * move save() to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
120 |
aa4eab6dc994
* dont set node width, height; * move save() to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
121 if self.edges: |
aa4eab6dc994
* dont set node width, height; * move save() to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
122 self.save() |
0 | 123 |
124 def __call__(self, environ, start_response): | |
125 request = Request(environ) | |
1 | 126 if request.referer: |
127 | |
128 # parse the URLs | |
129 parsed_referer = urlparse.urlsplit(request.referer) | |
130 parsed_referee = urlparse.urlsplit(request.url) | |
131 islocal = False | |
132 | |
133 # see if its local or not | |
134 localhosts = self.hosts[:] | |
135 if parsed_referee.hostname not in localhosts: | |
136 localhosts.append(parsed_referee.hostname) | |
137 for host in localhosts: | |
138 if parsed_referer.hostname == host or parsed_referer.hostname.endswith('.' + host): | |
139 islocal = True | |
140 break | |
141 | |
142 # make the connection | |
143 if islocal: | |
144 self.add(parsed_referer.path, parsed_referee.path) | |
145 else: | |
146 if self.external_referers: | |
147 self.add(request.referer, parsed_referee.path) | |
148 | |
0 | 149 return self.app(environ, start_response) |
150 | |
151 def add(self, from_url, to_url): | |
1 | 152 """add a conncection in the graph""" |
153 | |
154 if from_url == to_url: | |
155 return # don't do self-references | |
6
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
156 if self.exclude(from_url, to_url): |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
157 return # ignore certain urls |
1 | 158 |
0 | 159 if (from_url, to_url) in self.edges: |
160 count = self.edges[(from_url, to_url)] | |
161 count += 1 | |
1 | 162 if count > self.max: |
163 self.max = count | |
0 | 164 self.edges[(from_url, to_url)] = count |
165 edge = self.graph.get_edge(from_url, to_url) | |
166 edge.attr['label'] = str(count) | |
167 else: | |
168 self.edges[(from_url, to_url)] = 1 | |
4 | 169 self.max = 1 |
0 | 170 self.graph.add_edge(from_url, to_url, label='1') |
171 | |
6
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
172 if self.maxwidth: |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
173 self.set_widths() |
2 | 174 |
0 | 175 for url in from_url, to_url: |
176 node = self.graph.get_node(url) | |
177 node.attr['label'] = url | |
178 node.attr['href'] = url | |
179 | |
9
aa4eab6dc994
* dont set node width, height; * move save() to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
180 self.save() |
6
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
181 |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
182 def exclude(self, *urls): |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
183 """tell whether the edge is excluded""" |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
184 for pattern in self.excluded: |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
185 for url in urls: |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
186 if fnmatch(url, pattern): |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
187 return True |
7
145a4d85b37d
exclude long urls for now ::sigh::
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
188 for url in urls: |
145a4d85b37d
exclude long urls for now ::sigh::
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
189 if len(url) > self.maxlength: |
145a4d85b37d
exclude long urls for now ::sigh::
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
190 return True |
6
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
191 return False |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
192 |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
193 def set_widths(self): |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
194 if self.maxwidth: |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
195 for edge in self.graph.edges(): |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
196 count = self.edges[(edge[0], edge[1])] |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
197 width = self.maxwidth * count / self.max |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
198 if not width: |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
199 width = self.minwidth |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
200 edge.attr['style'] = 'setlinewidth(%s)' % width |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
201 |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
202 else: |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
203 for edge in self.graph.edges(): |
a88a670c92d0
dont display things that you dont want
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
204 edge.attr['style'] = '' |
9
aa4eab6dc994
* dont set node width, height; * move save() to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
205 |
aa4eab6dc994
* dont set node width, height; * move save() to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
206 def save(self): |
aa4eab6dc994
* dont set node width, height; * move save() to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
207 if self.file: |
aa4eab6dc994
* dont set node width, height; * move save() to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
208 self.graph.write(self.file) |
aa4eab6dc994
* dont set node width, height; * move save() to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
209 if self.output: |
aa4eab6dc994
* dont set node width, height; * move save() to its own function
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
210 self.graph.draw(self.output, prog='neato') |