summaryrefslogtreecommitdiff
path: root/query/__init__.py
blob: dc3339c71993d4af3305cb6ac1677b21d79ab033 (plain)
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
import time
import BaseHTTPServer
import urlparse
import json
import os

__path__ = os.path.abspath(os.path.dirname(__file__))

server_registry = dict()

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    #hide log messages to stdout by default
    def log_message(self, format, *args): pass

    def do_HEAD(s):
        s.send_response(200)
        s.send_header("Content-type", "text/html")
        s.end_headers()

    def do_GET(s):
        """Respond to a GET request."""

        #extract the path and set default
        o = urlparse.urlparse(s.path)
        args = server_registry[s.server]
        path = o.path

        #handle json requests
        if path.endswith('.json'):
            s.send_response(200)
            s.send_header("Content-type", "application/json")
            s.end_headers()
            if path == '/args.json':
                arg_strs = dict((str(k), str(v)) for k, v in args.iteritems())
                s.wfile.write(json.dumps(arg_strs))
            else:
                query_args = dict([(k,v) for k,v in urlparse.parse_qs(o.query).iteritems()])
                query_args['path'] = path
                json_args = json.dumps(query_args)
                s.wfile.write(args['top_block'].query(json_args))
            return

        #clean up path for filesystem
        if path.startswith('/'): path = path[1:]
        if not path: path = 'main.html'
        target = os.path.join(__path__, path)

        #get files from the local file system
        if os.path.exists(target):
            s.send_response(200)
            if target.endswith('.js'): s.send_header("Content-type", "text/javascript")
            elif target.endswith('.css'): s.send_header("Content-type", "text/css")
            else: s.send_header("Content-type", "text")
            s.end_headers()
            s.wfile.write(open(target).read())
        #otherwise not found do 404
        else:
            s.send_response(404)
            s.send_header("Content-type", "text/html")
            s.end_headers()
            s.wfile.write("<p>not found</p>")

import select

class http_server(object):
    def __init__(self, args, **kwargs):
        server_class = BaseHTTPServer.HTTPServer
        self._httpd = server_class(args, MyHandler)
        server_registry[self._httpd] = kwargs

    def serve_forever(self):
        while True:
            try: self._httpd.serve_forever()
            except select.error: pass