forked from karpathy/ulogme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulogme_serve.py
74 lines (62 loc) · 2.13 KB
/
ulogme_serve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import SocketServer
import SimpleHTTPServer
import sys
import cgi
import os
from export_events import updateEvents
from rewind7am import rewindTime
# Port settings
IP = ""
if len(sys.argv) > 1:
PORT = int(sys.argv[1])
else:
PORT = 8123
# serve render/ folder, not current folder
rootdir = os.getcwd()
os.chdir('render')
# Custom handler
class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
# default behavior
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
form = cgi.FieldStorage(
fp = self.rfile,
headers = self.headers,
environ = {'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type']})
result = 'NOT_UNDERSTOOD'
if self.path == '/refresh':
# recompute jsons. We have to pop out to root from render directory
# temporarily. It's a little ugly
refresh_time = form.getvalue('time')
os.chdir(rootdir) # pop out
updateEvents() # defined in export_events.py
os.chdir('render') # pop back to render directory
result = 'OK'
if self.path == '/addnote':
# add note at specified time and refresh
note = form.getvalue('note')
note_time = form.getvalue('time')
os.chdir(rootdir) # pop out
os.system('echo %s | ./note.sh %s' % (note, note_time))
updateEvents() # defined in export_events.py
os.chdir('render') # go back to render
result = 'OK'
if self.path == '/blog':
# add note at specified time and refresh
post = form.getvalue('post')
if post is None: post = ''
post_time = int(form.getvalue('time'))
os.chdir(rootdir) # pop out
trev = rewindTime(post_time)
open('logs/blog_%d.txt' % (post_time, ), 'w').write(post)
updateEvents() # defined in export_events.py
os.chdir('render') # go back to render
result = 'OK'
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(result)
httpd = SocketServer.ThreadingTCPServer((IP, PORT), CustomHandler)
print 'Serving ulogme, see it on http://localhost:' + `PORT`
httpd.serve_forever()