changeset 25:8729685a2cf6

stub web interface
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 25 Nov 2011 00:01:15 -0800
parents d3e3a506dd29
children 0faf3e7b593a
files licenser/licenses.py licenser/web.py
diffstat 2 files changed, 75 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/licenser/licenses.py	Thu Nov 24 15:29:05 2011 -0800
+++ b/licenser/licenses.py	Fri Nov 25 00:01:15 2011 -0800
@@ -74,7 +74,7 @@
         if self.isempty():
             return # you're done
         lines = self.lines()
-        
+
         # open the file for writing
         f = file(self.filename, 'w')
 
@@ -138,6 +138,12 @@
 
 filetypes = [HashCommentsFile, AsteriskCommentsFile]
 
+def filetypes_string(filetypes=filetypes[:]):
+    extensions = set()
+    for filetype in filetypes:
+        extensions.update(filetype.extensions)
+    return ', '.join([i for i in extensions])
+
 class License(object):
     """Abstract base class for a license"""
 
@@ -151,7 +157,7 @@
                                              self.template)
             assert os.path.exists(self.template)
         self.filetypes = filetypes
-        
+
     def license(self):
         return file(self.template).read()
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/licenser/web.py	Fri Nov 25 00:01:15 2011 -0800
@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+
+"""
+web handler for MPL licenser
+"""
+
+import licenses
+from webob import Request, Response, exc
+
+class Handler(object):
+
+    required = set(['name', 'email', 'url'])
+
+    template = """<html>
+<head><title>MPL licenser</title></head>
+<body>
+<h1>MPL Licenser</h1>
+<div><i>license your files with MPL. Currently works for files of type: %(filetypes)s</i></div>
+<div><form method="POST" enctype="multipart/form-data">
+<p>Enter a file: <input type="file" name="file"/></p>
+<p>or a URL: <input type="text" name="url" value="%(url)s"/></p>
+<p>And enter your name: <input type="text" name="name" value="%(name)s"/></p>
+<p>and email: <input type="text" name="email" value="%(email)s"/></p>
+<input type="submit"/>
+</body>
+</html>"""
+
+    def __init__(self, **kw):
+        self.methods = {'GET': self.GET,
+                        'POST': self.POST}
+
+    def __call__(self, environ, start_response):
+        request = Request(environ)
+        if request.method in self.methods:
+            response = self.methods[request.method](request)
+        else:
+            response = exc.HTTPMethodNotAllowed()
+        return response(environ, start_response)
+
+    def GET(self, request):
+        variables = {}
+        for i in self.required:
+            if i in request.GET:
+                variables[i] = request.GET[i]
+        if set(variables.keys()) == self.required:
+            try:
+                pass
+            except:
+                pass
+        return self.render(**variables)
+
+    def render(self, **variables):
+        for i in self.required:
+            if i not in variables:
+                variables[i] = ''
+        variables['filetypes'] = licenses.filetypes_string()
+        return Response(content_type='text/html', body=(self.template % variables))
+
+    def POST(self, request):
+        pass
+
+if __name__ == '__main__':
+    from wsgiref import simple_server
+    app = Handler()
+    server = simple_server.make_server(host='0.0.0.0', port=8080, app=app)
+    server.serve_forever()
+