def delete(self, key, **options): del self._data[key] def get(self, key, **options): value, expires = self._data.get(key, (None, None)) if expires is None or expires < time.time(): return None else: return value def set(self, key, value, **options): expires = time.time() + options.get('ttl', 3600) self._data[key] = (value, expires) if modutil.module_exists('pylru'): from pylru import lrucache class LruCache(Cache): def __init__(self, size=1000): self._size = size self.clear() def clear(self): self._cache = lrucache(self._size) def delete(self, key, **options): del self._cache[key] def get(self, key, **options): try:
# -*- coding: utf-8 -*- from __future__ import absolute_import from decorated.util import modutil import json if modutil.module_exists('webapp2'): from metaweb import coor, views from webapp2 import RequestHandler, WSGIApplication class GaeCoor(coor.coor_maker(RequestHandler)): def get(self, path): resp = self.render(path) self._process_resp(resp) def post(self, path): resp = self.render(path) self._process_resp(resp) def _process_resp(self, resp): self.response.set_status(resp.status_string()) for k, v in resp.headers.items(): self.response.headers[k] = str(v) self.response.out.write(resp.body) def _read_fields(self): fields = dict(self.request.GET) ctype = self.request.headers.get('Content-Type', '').lower() if self.request.method == 'POST' and ctype.startswith('application/json'): data = json.loads(self.request.body) fields.update(data) else:
class SimpleCache(Cache): def __init__(self): self._data = {} def _delete(self, key): del self._data[key] def _get(self, key): return self._data.get(key) def _set(self, key, value): self._data[key] = value if modutil.module_exists("pylru"): from pylru import lrucache class LruCache(Cache): def __init__(self, size=1000): self._cache = lrucache(size) def _delete(self, key): del self._cache[key] def _get(self, key): try: return self._cache[key] except KeyError: return None
# -*- coding: utf-8 -*- from cgi import FieldStorage from decorated.util import modutil import json if modutil.module_exists('web'): from metaweb import files, coor, views from metaweb.files import FileField from web import webapi import web class WebpyFileField(FileField): def __init__(self, storage): super(WebpyFileField, self).__init__(storage.filename) self._file = storage.file def chunks(self, size=files.CHUNK_SIZE): while True: chunk = self._file.read(size) if len(chunk): yield chunk else: break def run(cls=None, context_class=None, default_url=None, roots=('views',)): cls = cls or _create_coor(context_class=context_class) app = _create_app(cls, default_url, roots) app.run() def wsgi(cls=None, context_class=None, default_url=None, roots=('views',)): cls = cls or _create_coor(context_class=context_class)