Permalink
Browse files

Update to web.py 0.3

  • Loading branch information...
1 parent 3b7df2b commit 3218b05194573a74f26a87c4a575c8f1a34c6780 @harryf committed Jan 20, 2009
Showing with 26 additions and 18 deletions.
  1. +6 −1 .gitignore
  2. +2 −2 dammit/cachemanager.py
  3. +18 −15 urldammit.py
View
@@ -1,4 +1,9 @@
*.pyc
.svn
config.py
-INSTALL_SSOA
+INSTALL_SSOA
+\#*\#
+common
+run_urldammit.py
+urldammit_init
+package
View
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-def dict_constructor():
+def dict_constructor(namespace):
return dict()
cache_constructor = dict_constructor
@@ -32,7 +32,7 @@ def namespacer(func):
the key argument, obtained from self.namespace
"""
def namespace_wrapper(self, key, *args, **kwargs):
- key = "%s_%s" % ( self.namespace, key )
+ key = "%s_%s" % ( self.namespace, key.encode('UTF-8') )
return func( self, key, *args, **kwargs )
return namespace_wrapper
View
@@ -12,22 +12,24 @@
urls = (
'/', 'urldammit',
- '/_tools', 'tools',
+ '/_tools/?', 'tools',
'/_tools/addurl', 'tools_addurl',
'/_tools/checkurl', 'tools_checkurl',
'/([0-9a-f]{40})', 'urldammit',
'/find/(.*)', 'find',
)
+application = web.application(urls, globals() )
manager = URIManager(config.get_db())
# cache URIs we know about
known = cachemanager.new_instance('known')
-# cache queries we know nothing about otherwise
-# we have to ask couch each time
+# cache queries we know nothing to avoid
+# backend requests
unknown = cachemanager.new_instance('unknown')
+
class urldammit(object):
"""
Main service handler
@@ -49,8 +51,7 @@ def GET(self, id = None):
ID is the SHA-1 of the URI
"""
if not id:
- print "where's my url dammit?"
- return
+ return "where's my url dammit?"
u = self._locate(id)
@@ -61,7 +62,7 @@ def GET(self, id = None):
return
self._ok(u)
- self._render(u)
+ return self._render(u)
validstatus = re.compile("^200|301|404$")
@@ -139,6 +140,7 @@ def ithas(key, cache):
if not u:
u = manager.load(id)
+
if not u:
unknown[id] = True
web.notfound()
@@ -175,10 +177,10 @@ def _store(self, uri, i):
known[u.id] = u
if u.id in unknown: del unknown[u.id]
- web.http.seeother(
+ web.seeother(
"%s/%s" % ( web.ctx.home, u.id)
)
- self._render(u)
+ return self._render(u)
return True
except URIError, e:
@@ -212,15 +214,15 @@ def _render(self, u):
Display result, encoded as json
"""
if not u: return
- print pack_response(u)
+ return pack_response(u)
def _badrequest(self, msg):
"""
Bad request (e.g. trying to record info on a status 404 url which
urldammit has never seen before)
"""
web.ctx.status = statusmap[400]
- print view.badrequest(reason = msg)
+ return view.badrequest(reason = msg)
class find(object):
"""
@@ -240,15 +242,15 @@ class tools:
Tools for humans...
"""
def GET(self):
- print render.base(view.tools())
+ return render.base(view.tools())
class tools_addurl:
def GET(self):
- print render.base(view.addurl())
+ return render.base(view.addurl())
class tools_checkurl:
def GET(self):
- print render.base(view.checkurl())
+ return render.base(view.checkurl())
encoded = re.compile('^https?%3A%2F%2F')
def reduce_uri(i, uri):
@@ -280,7 +282,7 @@ def required(input, key):
val = getattr(input, key)
except AttributeError:
web.ctx.status = statusmap[406]
- print "%s parameter required" % key
+ return "%s parameter required" % key
return val
@@ -293,5 +295,6 @@ def required(input, key):
dammit.webtests.run()
else:
web.webapi.internalerror = web.debugerror
- web.run(urls, globals() )
+ #application = web.application(urls, globals() )
+ application.run()

0 comments on commit 3218b05

Please sign in to comment.