From 1eca8867751df644a62752fbbfbc6a6de849de74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Simon?= Date: Mon, 11 May 2015 20:00:04 -0400 Subject: Add visualizer. Lasciate ogni speranza voi ch'entrate: I am the bone of my javascript DOM is my body and JQuery is my blood I have created over a thousand lines Unknown to death Nor known to life Have withstood pain to create many functions Yet those hands shall never type anything So, as I pray, Unlimited Openlayers Works --- visualizer/HTTPServer.py | 128 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100755 visualizer/HTTPServer.py (limited to 'visualizer/HTTPServer.py') diff --git a/visualizer/HTTPServer.py b/visualizer/HTTPServer.py new file mode 100755 index 0000000..e71bef3 --- /dev/null +++ b/visualizer/HTTPServer.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python + +import os +import sys +import urllib +import SimpleHTTPServer +import SocketServer +from cStringIO import StringIO + +import h5py + +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
'%sub)) + else: + content.append(Path(train_data.extract(int(sub)), '%s
'%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
'%i)) + else: + content.append(Path(train_data.extract(i), '%d
'%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() -- cgit v1.2.3