Merge pull request #5 from cuihantao/master
[pylon.git] / contrib / pylon_webpy.py
blob8005096971ffeee89459f2b88c84c50103b13332
1 import web
2 import json
3 #from django.utils import simplejson
4 import pylon
5 urls = ('/json', 'PylonHandler')
6 app = web.application(urls, globals())
8 class PylonHandler:
9 def __init__(self):
10 self.case = pylon.Case.load("./pylon/test/data/bench30.raw")
13 def json_buses(self, args):
14 print "ARGS:", args
15 return [[getattr(bus, args[0]) for bus in self.case.buses]]
18 def POST(self):
19 # args = simplejson.loads(web.data())
20 args = json.read(web.data())
21 json_func = getattr(self, 'json_%s' % args[u"method"])
22 json_params = args[u"params"]
23 # json_method_id = args[u"id"]
24 result = json_func(json_params)
25 # reuse args to send result back
26 args.pop(u"method")
27 args["result"] = result[0]
28 args["error"] = None # IMPORTANT!!
29 web.header("Content-Type","text/html; charset=utf-8")
30 return json.write(args)
31 # return simplejson.dumps(args)
33 if __name__ == "__main__":
34 app.run()