From 4b320a8d744b8efeb27c983bb6f08208ec743c29 Mon Sep 17 00:00:00 2001 From: yvesf Date: Wed, 9 Jun 2010 18:13:33 +0000 Subject: blablabla git-svn-id: http://10.2.2.13/svn/common/ebus@1677 d0e8fea9-7529-0410-93fb-d39fd5b9c1dd --- ebus/web/controller.py | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) (limited to 'ebus/web/controller.py') diff --git a/ebus/web/controller.py b/ebus/web/controller.py index f3fa438..edf174d 100644 --- a/ebus/web/controller.py +++ b/ebus/web/controller.py @@ -1,11 +1,15 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import sys, os, time -sys.path.insert(0,"lib") -from itty import itty +import os, time +import itty from werkzeug.contrib.cache import SimpleCache from simplejson import dumps as dump_json +from ebus.web import model + + +def data(name): + return {"name":name} """ def data(name): if name in ( @@ -74,25 +78,44 @@ def data(name): return [] """ +class CacheDecorator(object): + def __init__(self,key,timeout=20): + self.cache = SimpleCache(default_timeout=timeout) + self.func = None + self.key = key + + def cachedFunc(self, *args, **kwargs): + key = self.key(args, kwargs) + ret = self.cache.get(key) + if ret is None: + ret = self.func(*args, **kwargs) + self.cache.set(key, ret) + return ret + + def __call__(self, func): + self.func = func + return self.cachedFunc @itty.get("/") def index(req): filename=os.path.join(os.path.dirname(__file__), "static", "index.html") return open(filename,"r").read() -json_cache = SimpleCache(default_timeout=20) -@itty.get('/json/') -def json(req,name): - value = json_cache.get(name) - if value is None: - value = deny.Response(dump_json(dict(map(lambda r: (r, {"label":r, "data":data(r)}), name.split(",")))), mimetype='application/json') - json_cache.set(name, value) - return value +@itty.get('/json/(?P[^/]+)') +@itty.get('/json/(?P[^/]+)/from_date/(?P.+)') +@CacheDecorator(key=lambda args,kwargs: tuple(kwargs.keys()) + tuple(kwargs.values()), + timeout=20) +def json(req,name=None,from_date=None): + print "name=%s from_date=%s" % (name,from_date) + return itty.Response(dump_json( {"label":name, "data":data(name)} ), + content_type="application/json") + @itty.get("/static/(?P.+)") def static(request,filename): file = itty.static_file(filename, root=os.path.join(os.path.dirname(__file__), 'static')) return itty.Response(file, content_type=itty.content_type(file)) -if __name__ == '__main__': - sys.exit(itty.run_itty(host="0.0.0.0",port=5000)) + +def run(): + return itty.run_itty(host="0.0.0.0",port=5000) -- cgit v1.2.1