# HG changeset patch # User Jeff Hammel # Date 1322271028 28800 # Node ID eb3749dbdbf5c73f7f8e8e0f82d61491f2f47243 # Parent 7e0c931a201dfdd850a4c2d66eb4a3b7fa4ce13e finish web handler diff -r 7e0c931a201d -r eb3749dbdbf5 licenser/web.py --- 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 @@

And enter your name:

and email:

+

+You can use this with curl on a local file if you have tempfile (/bin/tempfile): +

+
+export TEMPFILE=$(tempfile) && curl -F "file=@foo.py" 'http://localhost:8080/?author=Jeff+Hammel&email=jhammel@example.com' > ${TEMPFILE} && mv ${TEMPFILE} foo.py
+
""" @@ -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']