diff options
Diffstat (limited to 'python/gras/query/__init__.py')
-rw-r--r-- | python/gras/query/__init__.py | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/python/gras/query/__init__.py b/python/gras/query/__init__.py index 06c7b70..b71b7e0 100644 --- a/python/gras/query/__init__.py +++ b/python/gras/query/__init__.py @@ -1,7 +1,9 @@ import time import BaseHTTPServer +import urlparse import json import os + __path__ = os.path.abspath(os.path.dirname(__file__)) server_registry = dict() @@ -15,23 +17,39 @@ class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): 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 = s.path - if path.startswith('/'): path = path[1:] - if not path: path = 'main.html' + 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': + if path == '/args.json': arg_strs = dict((str(k), str(v)) for k, v in args.iteritems()) s.wfile.write(json.dumps(arg_strs)) - elif path == 'stats.json': - s.wfile.write(args['top_block'].query(s.path)) else: - s.wfile.write(json.dumps({})) + #why the fuck does no OS ever patch boost when there is a bug + #https://svn.boost.org/trac/boost/ticket/6785 + #serialize the path args into xml -- but I just wanted json + def xml_from_qs(k, v): + if not isinstance(v, list): v = [v] + return ''.join(['<%s>%s</%s>'%(k, v_i, k) for v_i in v]) + query_args = [xml_from_qs(k,v) for k,v in urlparse.parse_qs(o.query).iteritems()] + query_args.append(xml_from_qs('path', path)) + xml_args = xml_from_qs('args', ''.join(query_args)) + s.wfile.write(args['top_block'].query(xml_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") @@ -39,6 +57,7 @@ class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): 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") |