diff options
Diffstat (limited to 'ebus/web/controller.py')
-rw-r--r-- | ebus/web/controller.py | 49 |
1 files changed, 36 insertions, 13 deletions
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/<name>') -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<name>[^/]+)') +@itty.get('/json/(?P<name>[^/]+)/from_date/(?P<from_date>.+)') +@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<filename>.+)") 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) |