Permalink
Browse files

Remove pointless example resources

  • Loading branch information...
1 parent c14de32 commit ce4a91d4ec7f0877b8d4f0667f2e19935fc001c3 @alekstorm committed Dec 7, 2011
Showing with 3 additions and 138 deletions.
  1. +1 −45 test/test.py
  2. +1 −49 vortex/__init__.py
  3. +0 −43 vortex/memcached.py
  4. +1 −1 vortex/resources.py
View
@@ -8,31 +8,13 @@
from tornado.template import Loader
import uuid
-from vortex import Application, HTTPResponse, HTTPStream, Resource, authenticate, add_slash, format, json2xml, remove_slash, signed_cookie, xsrf
-from vortex.memcached import Memcacher, memcached
+from vortex import Application, HTTPResponse, HTTPStream, Resource, authenticate, signed_cookie, xsrf
from vortex.resources import DictResource, JSONResource, StaticDirectoryResource, StaticFileResource, UploadResource
class ArgResource(Resource):
def get(self, request, a, b='default'):
return 'Success: a=%s, b=%s' % (a, b)
-@memcached
-class MemcachedResource(Resource):
- def __getitem__(self, name):
- return ExpensiveResource()
-
-class NonMemcachedResource(Resource):
- def __getitem__(self, name):
- return ExpensiveResource()
-
-class ExpensiveResource(Resource):
- def __init__(self):
- time.sleep(2)
- self.expensive = 'This took a long time to compute'
-
- def get(self, request):
- return self.expensive
-
COOKIE_SECRET = str(uuid.uuid4())
class AppResource(Resource):
@@ -62,11 +44,6 @@ class LogoutResource(AppResource):
def get(self, request, user):
return HTTPResponse(entity='Bye, '+user, cookies={'user': ''})
-class FormatResource(Resource):
- @format({'json': lambda r: JSONResource(r), 'xml': json2xml})
- def __getitem__(self, name):
- return {'name': 'Guido', 'interests': [{'title': 'python', 'level': 9}, {'title': 'dancing', 'level': 5}], 'books': ['Python Tutorial', 'Python Reference Manual'], 'address': {'city': 'Mountain View', 'state': 'CA'}}
-
class UploadFormResource(UploadResource):
def __init__(self, loader):
UploadResource.__init__(self)
@@ -75,20 +52,6 @@ def __init__(self, loader):
def get(self, request):
return self.loader.load('upload.html').generate(items=self.sub_resources.keys())
-class AddSlashResource(DictResource):
- def __init__(self):
- DictResource.__init__(self, {'': lambda request: 'Slash added'})
-
- @add_slash
- def __call__(self, request): pass
-
-class RemoveSlashResource(Resource):
- def __call__(self, request):
- return 'Slash removed'
-
- @remove_slash
- def __getitem__(self, name): pass
-
class AsyncResource(Resource):
def get(self, request):
def callback():
@@ -110,18 +73,11 @@ def callback():
'favicon.ico': StaticFileResource(os.path.join(static_dir, 'favicon.ico')),
'json': JSONResource({'a': ['b', 1], 'c': {'d': True}}),
'args': ArgResource(),
- 'memcached': Memcacher(Client(['127.0.0.1:11211']), {
- 'slow': NonMemcachedResource(),
- 'fast': MemcachedResource(),
- }),
- 'format': FormatResource(),
'auth': {
'login': LoginResource(),
'secret': SecretResource(),
'logout': LogoutResource(),
},
- 'add-slash': AddSlashResource(),
- 'remove-slash': RemoveSlashResource(),
'async': AsyncResource(),
'upload': UploadFormResource(loader),
})
View
@@ -5,8 +5,7 @@
from cStringIO import StringIO as BytesIO
except ImportError:
from StringIO import StringIO as BytesIO
-import Cookie
-import collections
+from Cookie import SimpleCookie
from email.utils import formatdate
from gzip import GzipFile
import hashlib
@@ -28,53 +27,6 @@
SAFE_METHODS = set(('GET', 'HEAD'))
-def add_slash(call):
- def wrap(self, request, *args, **kwargs):
- return self[''](request, *args, **kwargs)
- return wrap
-
-def remove_slash(getitem):
- def wrap(self, name):
- if len(name) == 0:
- return self
- return getitem(self, name)
- return wrap
-
-def json2xml(data):
- def convert_elem(data, tag='item'):
- root = Element(tag)
- if isinstance(data, dict):
- for key, val in data.iteritems():
- if isinstance(val, dict) or isinstance(val, list):
- root.append(convert_elem(val, key))
- else:
- root.set(key, str(val))
- elif isinstance(data, list):
- for item in data:
- root.append(convert_elem(item))
- else:
- root.set('_value', str(data))
- return root
-
- return lambda request: convert_elem(data, 'root')
-
-def format(handlers, default=None, unknown=None):
- def wrap1(getitem):
- def wrap2(self, name):
- parts = name.split('.')
- resource = getitem(self, parts[0])
- if len(parts) > 1:
- handler = handlers.get(parts[1], None)
- if handler:
- return handler(resource)
- if unknown:
- return unknown(resource)
- raise KeyError()
- if default:
- return default(resource)
- raise KeyError()
- return wrap2
- return wrap1
def authenticate(retrieve, cookie_name, redirect=None, unauthorized=None):
def wrap1(fn):
View
@@ -1,43 +0,0 @@
-class _Accumulator(object):
- def __init__(self, mc, root):
- self.mc = mc
- self.path = ''
- self.parent = root
-
- def __getitem__(self, name):
- self.path += '/'+name
- if hasattr(self.parent, '_memcached'):
- child = self.mc.get(self.path)
- if child is None:
- child = self.parent[name]
- self.mc.set(self.path, child)
- child._memcached_path = self.path
- else:
- child = self.parent[name]
- self.parent = child
- return self
-
- def __call__(self, *args, **kwargs):
- return self.parent(*args, **kwargs)
-
-class Memcacher(object):
- def __init__(self, mc, root):
- self.mc = mc
- self.root = root
-
- def persistent_id(obj):
- if hasattr(obj, '_memcached'):
- return obj._memcached_path
- return None
- self.mc.persistent_id = persistent_id
-
- def persistent_load(path):
- return self.mc.get(path)
- self.mc.persistent_load = persistent_load
-
- def __getitem__(self, name):
- return _Accumulator(self.mc, self.root)[name]
-
-def memcached(cls):
- cls._memcached = True
- return cls
View
@@ -6,7 +6,7 @@
import time
import uuid
-from vortex import Application, HTTPResponse, HTTPStream, Resource, authenticate, format, http_date, json2xml, signed_cookie, xsrf
+from vortex import HTTPStream, Resource, authenticate, http_date, signed_cookie, xsrf
from vortex.responses import *
class DictResource(Resource):

0 comments on commit ce4a91d

Please sign in to comment.