0
|
1 """
|
|
2 request dispatcher
|
|
3 """
|
|
4
|
|
5 __all__ = ['MapserverMiddleware', 'SVGSiteMap']
|
|
6
|
|
7 import os
|
1
|
8 import urlparse
|
0
|
9 from pygraphviz import AGraph
|
1
|
10 from webob import Request, Response, exc
|
0
|
11
|
|
12 class MapserverMiddleware(object):
|
|
13 """silly middleware to serve just the svg"""
|
|
14 def __init__(self, app, svgmap, path='/map'):
|
1
|
15 self.app = app
|
0
|
16 self.svgmap = svgmap
|
|
17 self.path = path
|
|
18
|
|
19 def __call__(self, environ, start_response):
|
|
20 request = Request(environ)
|
|
21 if request.path_info == self.path or not self.path:
|
1
|
22 if not os.path.exists(self.svgmap):
|
|
23 res = exc.HTTPNotFound()
|
|
24 return res(environ, start_response)
|
0
|
25 content = file(self.svgmap).read()
|
1
|
26 res = Response(content_type='image/svg+xml', body=content)
|
|
27 return res(environ, start_response)
|
|
28 return self.app(environ, start_response)
|
0
|
29
|
|
30
|
|
31 class SVGSiteMap(object):
|
|
32
|
|
33 ### class level variables
|
1
|
34 defaults = { 'name': '',
|
|
35 'hosts': '',
|
|
36 'external_referers': True,
|
|
37 'maxwidth': 20
|
|
38
|
|
39 # input/output
|
|
40 'file': None, # .ini file
|
|
41 'output': None, # .svg file
|
|
42
|
|
43 # graph attributes
|
0
|
44 'bgcolor': 'black',
|
|
45 'fontcolor': 'white',
|
|
46 'fontname': 'Helvetica',
|
1
|
47 'fontsize': '10.0',
|
0
|
48 'nodecolor': 'aqua',
|
|
49 'edgecolor': 'lime',
|
1
|
50 'shape': 'box',
|
|
51 'len': '1.3',
|
|
52 'arrowsize': '0.5',
|
0
|
53 }
|
|
54
|
|
55 def __init__(self, app, **kw):
|
1
|
56
|
|
57 # boilerplate
|
0
|
58 self.app = app
|
|
59 self.edges = {}
|
1
|
60 self.max = 0
|
|
61
|
|
62 # set attrs from defaults
|
0
|
63 for key in self.defaults:
|
|
64 setattr(self, key, kw.get(key, self.defaults[key]))
|
1
|
65
|
|
66 # sanity checks + data fixing
|
0
|
67 assert self.output, "Please give an output file"
|
|
68 assert self.file, "Cannot save file!"
|
1
|
69 if self.hosts:
|
|
70 self.hosts = self.hosts.split()
|
|
71 else:
|
|
72 self.hosts = []
|
|
73 if isinstance(self.external_referers, basestring):
|
|
74 self.external_referers = self.external_referers.lower() == 'true'
|
|
75
|
|
76 # open the graph
|
0
|
77 if os.path.exists(self.file):
|
1
|
78 self.graph = AGraph(self.file, name=self.name, splines=False, directed=True)
|
0
|
79 for edge in self.graph.edges():
|
1
|
80 count = int(edge.attr['label'])
|
|
81 self.edges[(edge[0], edge[1])] = count
|
|
82 if count > self.max:
|
|
83 self.max = count
|
0
|
84 else:
|
1
|
85 self.graph = AGraph(name=self.name, splines=False, directed=True)
|
|
86
|
|
87 # make it pretty
|
0
|
88 self.graph.graph_attr['label'] = self.name
|
|
89 self.graph.graph_attr['fontname'] = self.fontname
|
|
90 self.graph.graph_attr['fontcolor'] = self.fontcolor
|
|
91 self.graph.graph_attr['bgcolor'] = self.bgcolor
|
|
92 self.graph.node_attr['color'] = self.nodecolor
|
|
93 self.graph.node_attr['fontcolor'] = self.fontcolor
|
1
|
94 self.graph.node_attr['fontname'] = self.fontname
|
|
95 self.graph.node_attr['fontsize'] = self.fontsize
|
0
|
96 self.graph.node_attr['shape'] = self.shape
|
1
|
97 self.graph.node_attr['width'] = '0.1'
|
|
98 self.graph.node_attr['height'] = '0.1'
|
0
|
99 self.graph.edge_attr['color'] = self.edgecolor
|
|
100 self.graph.edge_attr['fontcolor'] = self.fontcolor
|
1
|
101 self.graph.edge_attr['fontname'] = self.fontname
|
|
102 self.graph.edge_attr['fontsize'] = self.fontsize
|
|
103 self.graph.edge_attr['len'] = self.len
|
|
104 self.graph.edge_attr['arrowsize'] = self.arrowsize
|
0
|
105
|
|
106 def __call__(self, environ, start_response):
|
|
107 request = Request(environ)
|
1
|
108 if request.referer:
|
|
109
|
|
110 # parse the URLs
|
|
111 parsed_referer = urlparse.urlsplit(request.referer)
|
|
112 parsed_referee = urlparse.urlsplit(request.url)
|
|
113 islocal = False
|
|
114
|
|
115 # see if its local or not
|
|
116 localhosts = self.hosts[:]
|
|
117 if parsed_referee.hostname not in localhosts:
|
|
118 localhosts.append(parsed_referee.hostname)
|
|
119 for host in localhosts:
|
|
120 if parsed_referer.hostname == host or parsed_referer.hostname.endswith('.' + host):
|
|
121 islocal = True
|
|
122 break
|
|
123
|
|
124 # make the connection
|
|
125 if islocal:
|
|
126 self.add(parsed_referer.path, parsed_referee.path)
|
|
127 else:
|
|
128 if self.external_referers:
|
|
129 self.add(request.referer, parsed_referee.path)
|
|
130
|
0
|
131 return self.app(environ, start_response)
|
|
132
|
|
133 def add(self, from_url, to_url):
|
1
|
134 """add a conncection in the graph"""
|
|
135
|
|
136 if from_url == to_url:
|
|
137 return # don't do self-references
|
|
138
|
0
|
139 if (from_url, to_url) in self.edges:
|
|
140 count = self.edges[(from_url, to_url)]
|
|
141 count += 1
|
1
|
142 if count > self.max:
|
|
143 self.max = count
|
0
|
144 self.edges[(from_url, to_url)] = count
|
|
145 edge = self.graph.get_edge(from_url, to_url)
|
|
146 edge.attr['label'] = str(count)
|
1
|
147 width = self.maxwidth * (count / self.max)
|
|
148 if not width:
|
|
149 width = 1
|
|
150 edge.attr['style'] = 'setlinewidth(%d)' % width
|
0
|
151 else:
|
|
152 self.edges[(from_url, to_url)] = 1
|
|
153 self.graph.add_edge(from_url, to_url, label='1')
|
|
154
|
|
155 for url in from_url, to_url:
|
|
156 node = self.graph.get_node(url)
|
|
157 node.attr['label'] = url
|
|
158 node.attr['href'] = url
|
|
159
|
|
160 if self.file:
|
|
161 self.graph.write(self.file)
|
|
162 if self.output:
|
1
|
163 self.graph.draw(self.output, prog='neato')
|