add 'next actions'; sync to json server
[hyena.git] / server.py
blob2b01296f0e340a4d8ee5787e3212881ca0c1d7b9
1 #!/usr/bin/python2.5
3 import os
5 import simplejson
6 from colubrid import BaseApplication, HttpResponse, execute
7 from colubrid.server import StaticExports
8 from paste import reloader
9 reloader.install()
12 class HyenaApplication(BaseApplication):
14 def process_request(self):
15 if self.request.environ['REQUEST_METHOD'] == 'POST':
16 # write the JSON
17 self.write_json(self.request.form['json'])
18 return HttpResponse('OK')
19 else:
20 # read the JSON
21 response = HttpResponse(self.read_json())
22 response['Content-Type'] = 'text/plain' # 'text/x-json'
23 return response
25 def write_json(self, json):
26 open('data.json', 'w').write(json)
28 def read_json(self):
29 json = self.read_json_from_disk()
30 data = simplejson.loads(json)
31 data['fortune'] = os.popen('fortune').read().replace('\n', '<br />')
32 return simplejson.dumps(data)
34 def read_json_from_disk(self):
35 if os.path.isfile('data.json'):
36 return open('data.json').read()
38 # defaul json
39 return simplejson.dumps({
40 'name': 'srid',
41 'actions': [
42 {'text': 'get my sansonite zip repaired under warranty',
43 'type': 'errand'},
44 {'text': 'guide dushyant on django templates',
45 'type': 'work'},
46 {'text': 'm$ reimbursement',
47 'type': 'errand'}
51 app = HyenaApplication
52 app = StaticExports(app, {
53 '/app': './app',
56 if __name__ == '__main__':
57 execute(app)