changeset 0:7a60bacc6a22

initial commit
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 27 Dec 2010 22:09:09 -0800
parents
children 084088505eea
files README.txt example/example.py setup.py svgsitemap/__init__.py svgsitemap/middleware.py
diffstat 5 files changed, 152 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/README.txt
@@ -0,0 +1,7 @@
+svgsitemap
+----------
+
+
+
+--
+
new file mode 100644
--- /dev/null
+++ b/example/example.py
@@ -0,0 +1,18 @@
+from svgsitemap import *
+from webob import Request, Response
+
+def example(environ, start_response):
+    link = '<a href="/%s">%s</a>'
+    body = '<br/>'.join([link % (i,i) for i in range(30)])
+    body = '<html><body>%s</body></html>' % body
+    response = Response(content_type='text/plain', body=body)
+    return response(environ, start_response)
+
+def factory():
+    import pdb; pdb.set_trace()
+    app = MapserverMiddleware(example)
+
+if __name__ == '__main__':
+    from wsgiref import simple_server
+    server = simple_server.make_server(host=host, port=int(54321), app=factory())
+    server.serve_forever() 
new file mode 100644
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,35 @@
+from setuptools import setup, find_packages
+import sys, os
+
+try:
+    description = file('README.txt').read()
+except IOError: 
+    description = ''
+
+version = "0.0"
+
+setup(name='svgsitemap',
+      version=version,
+      description="",
+      long_description=description,
+      classifiers=[], # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers
+      author='',
+      author_email='',
+      url='',
+      license="",
+      packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
+      include_package_data=True,
+      zip_safe=False,
+      install_requires=[
+          # -*- Extra requirements: -*-
+         'WebOb',	
+         'Paste',
+         'PasteScript',
+      ],
+      entry_points="""
+      # -*- Entry points: -*-
+      [paste.app_factory]
+      main = svgsitemap.factory:factory
+      """,
+      )
+      
new file mode 100644
--- /dev/null
+++ b/svgsitemap/__init__.py
@@ -0,0 +1,1 @@
+from middleware import *
new file mode 100644
--- /dev/null
+++ b/svgsitemap/middleware.py
@@ -0,0 +1,91 @@
+"""
+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')