Skip to content
Browse files

Basic urlfetch handler

  • Loading branch information...
1 parent 9bb9516 commit 07b9a5356fcc6f843e7c8af7f87d19fb87391e0d @parente committed Dec 12, 2010
Showing with 92 additions and 14 deletions.
  1. +3 −1 handlerbag.py
  2. +1 −7 hbag/admin/__init__.py
  3. +7 −2 hbag/admin/admin.py
  4. +1 −0 hbag/urlfetch/__init__.py
  5. +18 −0 hbag/urlfetch/urlfetch.html
  6. +57 −0 hbag/urlfetch/urlfetch.py
  7. +5 −4 hbag/xhrdrop.py
View
4 handlerbag.py
@@ -31,6 +31,7 @@ def __init__(self, handlers=[], **kwargs):
# store paths
self.appPath = os.path.dirname(os.path.abspath(__file__))
self.bagPath = os.path.dirname(hbag.__file__)
+ self.dataPath = os.path.join(self.appPath, 'data')
# update db to reflect available handlers
self.refresh_handlers_in_db()
@@ -169,12 +170,13 @@ def set_handler_status(self, name, enable, opts=None):
define('webroot', default='/', help='absolute root url of all handlers (default: /)')
define('port', default=5000, type=int, help='drop server port (default: 5000)')
define('debug', default=False, type=bool, help='enable debug autoreload (default: false)')
+ define('cookie', default=uuid.uuid4().hex, help='secret key cookie signing (default: random)')
tornado.options.parse_command_line()
settings = {
'login_url' : '/login',
'auth_cookie' : 'handlerbag.user',
- 'cookie_secret' : uuid.uuid4().hex,
+ 'cookie_secret' : str(options.cookie),
'debug' : options.debug
}
handlers = [
View
8 hbag/admin/__init__.py
@@ -1,7 +1 @@
-from admin import AdminHandler
-
-def get_handler_map(app, webroot, **options):
- return [(webroot+'admin/?', AdminHandler)]
-
-def get_default_options(app):
- return {}
+from admin import *
View
9 hbag/admin/admin.py
@@ -30,5 +30,10 @@ def post(self, *args, **kwargs):
for key, value in options.iteritems():
# encode keys to avoid problems passing by kwarg
optionsEnc[key.encode('utf-8')] = value
- # @todo: keys can't be unicode, passed around as kwargs everywhere
- self.application.set_handler_status(name, enabled, optionsEnc)
+ self.application.set_handler_status(name, enabled, optionsEnc)
+
+def get_handler_map(app, webroot, **options):
+ return [(webroot+'admin/?', AdminHandler)]
+
+def get_default_options(app):
+ return {}
View
1 hbag/urlfetch/__init__.py
@@ -0,0 +1 @@
+from urlfetch import *
View
18 hbag/urlfetch/urlfetch.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8" />
+ <title>urlfetch</title>
+ <style type="text/css">
+ input { width: 250px;}
+ </style>
+ </head>
+ <body>
+ <h1>urlfetch</h1>
+ <form method="post">
+ <p>{{ escape(result) }}</p>
+ <label id="url">URL</label> <input type="text" name="url" id="url"/>
+ <button type="submit">Save</button> <button type="reset">Reset</button>
+ </form>
+ </body>
+</html>
View
57 hbag/urlfetch/urlfetch.py
@@ -0,0 +1,57 @@
+'''Downloads a file given a URL to a local directory on the server.'''
+# tornado
+import tornado.web
+from tornado.simple_httpclient import SimpleAsyncHTTPClient
+# std lib
+import json
+import os.path
+import urlparse
+import datetime
+# handlerbag
+import users
+
+class URLFetchHandler(tornado.web.RequestHandler):
+ def initialize(self, **options):
+ self.options = options
+
+ def get_current_user(self):
+ return self.get_secure_cookie(self.settings['auth_cookie'])
+
+ @tornado.web.authenticated
+ @users.requireRole('admin')
+ def get(self):
+ self.render('urlfetch.html', result='')
+
+ @tornado.web.authenticated
+ @users.requireRole('admin')
+ @tornado.web.asynchronous
+ def post(self):
+ url = self.get_argument('url')
+ http = SimpleAsyncHTTPClient()
+ http.fetch(url, callback=self.on_fetch_complete)
+
+ def on_fetch_complete(self, resp):
+ if resp.error is None:
+ result = 'Downloaded %s' % resp.effective_url
+ fn = os.path.basename(resp.effective_url)
+ if not fn:
+ # make up a filename based on the datetime
+ fn = datetime.datetime.now().isoformat()
+ fn = os.path.join(self.options['path'], fn)
+ with file(fn, 'wb') as out:
+ while 1:
+ bytes = resp.buffer.read(int(self.options['buffer_size']))
+ if not bytes: break
+ out.write(bytes)
+ else:
+ result = str(resp.error)
+ self.render('urlfetch.html', result=result)
+
+def get_handler_map(app, webroot, **options):
+ return [(webroot+'urlfetch/?', URLFetchHandler, options)]
+
+def get_default_options(app):
+ return {
+ 'buffer_size' : 4096,
+ 'path' : os.path.join(app.dataPath, 'urlfetch')
+ }
View
9 hbag/xhrdrop.py
@@ -41,9 +41,10 @@ def get(self, *args, **kwargs):
super(XHRDropHandler, self).get(*args, **kwargs)
def get_handler_map(app, webroot, **options):
- tmp = {'path' : os.path.join(app.bagPath, 'xhrdrop')}
- tmp.update(options)
- return [(webroot+'xhrdrop/?(.*)', XHRDropHandler, tmp)]
+ return [(webroot+'xhrdrop/?(.*)', XHRDropHandler, options)]
def get_default_options(app):
- return {'get_enabled' : False}
+ return {
+ 'get_enabled' : False,
+ 'path' : os.path.join(app.dataPath, 'xhrdrop')
+ }

0 comments on commit 07b9a53

Please sign in to comment.
Something went wrong with that request. Please try again.