changeset 28:eb3749dbdbf5 default tip

finish web handler
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 25 Nov 2011 17:30:28 -0800
parents 7e0c931a201d
children
files licenser/web.py
diffstat 1 files changed, 26 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/licenser/web.py	Fri Nov 25 00:57:38 2011 -0800
+++ b/licenser/web.py	Fri Nov 25 17:30:28 2011 -0800
@@ -6,6 +6,8 @@
 
 import licenses
 import tempfile
+import urllib2
+import urlparse
 from StringIO import StringIO
 from webob import Request, Response, exc
 
@@ -24,6 +26,12 @@
 <p>And enter your name: <input type="text" name="author" value="%(author)s"/></p>
 <p>and email: <input type="text" name="email" value="%(email)s"/></p>
 <input type="submit"/>
+<p>
+You can use this with curl on a local file if you have <tt>tempfile</tt> (<tt>/bin/tempfile</tt>):
+</p>
+<pre>
+export TEMPFILE=$(tempfile) && curl -F "file=@foo.py" 'http://localhost:8080/?author=Jeff+Hammel&email=jhammel@example.com' > ${TEMPFILE} && mv ${TEMPFILE} foo.py
+</pre>
 </body>
 </html>"""
 
@@ -47,7 +55,11 @@
                 variables[i] = request.GET[i]
         if set(variables.keys()) == self.required:
             try:
-                pass
+                filename, body = self.fetch(request.GET['url'])
+                author = request.GET['author']
+                email = request.GET['email']
+                retval = self.interpolate(filename, body, author, email)
+                return Response(content_type='text/plain', body=retval)
             except:
                 pass
         return self.render(**variables)
@@ -59,18 +71,28 @@
         variables['filetypes'] = licenses.filetypes_string()
         return Response(content_type='text/html', body=(self.template % variables))
 
+    def fetch(self, url):
+        """fetch a url.  returns body, filename"""
+        parsed = urlparse.urlsplit(url)
+        path = parsed[2].rstrip('/')
+        body = urllib2.urlopen(url).read()
+        filename = path.split('/')[-1]
+        return filename, body
+
     def POST(self, request):
         variables = dict([(i, request.params[i])
                           for i in self.required
-                              if i in request.params])
+                          if i in request.params])
         if 'file' in request.POST and hasattr(request.POST['file'], 'filename'):
             body = request.POST['file'].file.getvalue()
             filename = request.POST['file'].filename
-        elif 'url' in request.POST:
+        elif 'url' in request.params:
             try:
-                pass
+                filename, body = self.fetch(request.params['url'])
             except:
                 return self.render(**variables)
+        else:
+            return self.render(variables)
         if not set(['author', 'email']).issubset(request.params):
             return self.render(**variables)
         author = request.params['author']