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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#!/usr/bin/env python
import os
import sys
import urllib
import SimpleHTTPServer
import SocketServer
from cStringIO import StringIO
import data
from data.hdf5 import TaxiDataset
from visualizer import Vlist, Path
visualizer_path = os.path.join(data.path, 'visualizer')
source_path = os.path.split(os.path.realpath(__file__))[0]
test_data = None
train_data = None
class VisualizerHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def send_head(self):
spath = self.path.split('?')[0]
path = spath.split('/')[1:]
if len(path) == 1:
if path[0] == '':
path[0] = 'index.html'
file_path = os.path.join(source_path, path[0])
return self.send_file(file_path)
elif path[0] == 'ls':
return self.send_datalist()
elif path[0] == 'get':
return self.send_file(os.path.join(visualizer_path, spath[5:]))
elif path[0] == 'extract':
return self.send_extract(spath[9:])
def send_file(self, file_path):
file_path = urllib.unquote(file_path)
ctype = self.guess_type(file_path)
try:
f = open(file_path, 'rb')
except IOError:
self.send_error(404, 'File not found')
return None
try:
self.send_response(200)
self.send_header('Content-type', ctype)
fs = os.fstat(f.fileno())
self.send_header('Content-Length', str(fs[6]))
self.send_header('Last-Modified', self.date_time_string(fs.st_mtime))
self.end_headers()
return f
except:
f.close()
raise
def send_datalist(self):
l = []
for path, subs, files in os.walk(visualizer_path):
for file in files:
mtime = os.stat('%s/%s' % (path, file))[8]
l.append('{"path":["%s"],"name":"%s","mtime":%d}' % ('","'.join(path[len(visualizer_path):].split('/')), file, mtime))
l.sort()
f = StringIO()
f.write("[")
f.write(','.join(l))
f.write("]")
length = f.tell()
f.seek(0)
self.send_response(200)
encoding = sys.getfilesystemencoding()
self.send_header("Content-type", "text/html; charset=%s" % encoding)
self.send_header("Content-Length", str(length))
self.end_headers()
return f
def send_extract(self, query):
f = StringIO()
query = urllib.unquote(query)
content = Vlist()
for (i,sub) in enumerate(query.split(',')):
r = sub.split('-')
if len(r)==1:
if sub.strip()[0].lower()=='t':
sub=sub.strip()[1:]
content.append(Path(test_data.extract(int(sub)), 'T%s<br>'%sub))
else:
content.append(Path(train_data.extract(int(sub)), '%s<br>'%sub))
elif len(r)==2:
test = False
if r[0].strip()[0].lower()=='t':
test = True
r[0]=r[0].strip()[1:]
if r[1].strip()[0].lower()=='t':
r[1]=r[1].strip()[1:]
for i in xrange(int(r[0]), int(r[1])+1):
if test:
content.append(Path(test_data.extract(i), 'T%d<br>'%i))
else:
content.append(Path(train_data.extract(i), '%d<br>'%i))
elif len(r)>2:
self.send_error(404, 'File not found')
return None
content.write(f)
length = f.tell()
f.seek(0)
self.send_response(200)
encoding = sys.getfilesystemencoding()
self.send_header("Content-type", "text/html; charset=%s" % encoding)
self.send_header("Content-Length", str(length))
self.end_headers()
return f
if __name__ == '__main__':
if len(sys.argv) != 2:
print >>sys.stderr, 'Usage: %s port' % sys.argv[0]
print >>sys.stderr, 'Loading dataset...',
path = os.path.join(data.path, 'data.hdf5')
train_data = TaxiDataset('train')
test_data = TaxiDataset('test')
print >>sys.stderr, 'done'
httpd = SocketServer.TCPServer(('', int(sys.argv[1])), VisualizerHTTPRequestHandler)
httpd.serve_forever()
|