annotate bindings/python/examples/server.py @ 637:be94366c6aa2

Rejigged /status to use flags on Pyadb object.
author mas01mj
date Tue, 29 Sep 2009 17:05:15 +0000
parents 6ec50185b348
children 2eaea1afd6b3
rev   line source
mas01mj@626 1 #!/usr/bin/python
mas01mj@626 2
mas01mj@631 3 from pyadb import *
mas01mj@626 4 import web
mas01mj@626 5 import json
mas01mj@626 6 import sys
mas01mj@626 7 import getopt
mas01mj@626 8
mas01mj@626 9 # DB Path goes here for now!
mas01mj@626 10 dbPath = "9.adb"
mas01mj@626 11
mas01mj@626 12 urls = (
mas01mj@626 13 '/', 'index',
mas01mj@626 14 '/status', 'status',
mas01mj@626 15 '/query', 'query'
mas01mj@626 16 )
mas01mj@626 17
mas01mj@626 18 app = web.application(urls, globals())
mas01mj@626 19 class index:
mas01mj@626 20 def GET(self):
mas01mj@626 21 return """
mas01mj@626 22 <html>
mas01mj@627 23 <head><title>audioDB server</title></head>
mas01mj@626 24 <body>
mas01mj@626 25 <ul>
mas01mj@627 26 <h2>Path: """+dbPath+"""</h2>
mas01mj@626 27 <li><a href="/status">Status</a></li>
mas01mj@626 28 <li><a href="/query">Query</a></li>
mas01mj@626 29 </ul>
mas01mj@626 30 </body>
mas01mj@626 31 </html>"""
mas01mj@626 32
mas01mj@626 33
mas01mj@626 34 class status:
mas01mj@626 35 def GET(self):
mas01mj@627 36 web.header("Content-Type", "application/json")
mas01mj@637 37
mas01mj@631 38 db = Pyadb(path = dbPath, mode = "r")
mas01mj@637 39 results = dict(zip(
mas01mj@637 40 ["numFiles", "dims", "dudCount", "nullCount", "length", "data_region_size", "l2Normed", "hasPower", "hasTimes", "usesRefs"],
mas01mj@637 41 [db.numFiles, db.dims, db.dudCount, db.nullCount, db.length, db.data_region_size, db.l2Normed, db.hasPower, db.hasTimes, db.usesRefs]))
mas01mj@626 42 return json.dumps(dict(status = "ok", data = results))
mas01mj@626 43
mas01mj@626 44 class query:
mas01mj@626 45 def GET(self):
mas01mj@627 46 web.header("Content-Type", "application/json")
mas01mj@627 47 params = web.input(key="", ntracks=100, seqStart=0, seqLength=16, npoints=1, radius=1.0, hopSize=1, exhaustive=False, falsePositives=False, accumulation="db", distance="dot", absThres=0, relThres=0, durRatio=0, includeKeys=[], excludeKeys=[])
mas01mj@626 48 results = dict()
mas01mj@631 49 db = Pyadb(path = dbPath, mode = "r")
mas01mj@637 50
mas01mj@631 51 if not params.includeKeys == []:
mas01mj@631 52 db.configQuery["includeKeys"] = map(str, params.includeKeys)
mas01mj@631 53
mas01mj@631 54 if params.excludeKeys:
mas01mj@631 55 foo = map(str, params.excludeKeys)
mas01mj@631 56 db.configQuery["excludeKeys"] = foo
mas01mj@626 57
mas01mj@631 58 db.configQuery["ntracks"] = int(params.ntracks)
mas01mj@631 59 db.configQuery["npoints"] = int(params.npoints)
mas01mj@631 60 db.configQuery["seqStart"] = int(params.seqStart)
mas01mj@631 61 db.configQuery["seqLength"] = int(params.seqLength)
mas01mj@631 62 db.configQuery["hopSize"] = int(params.hopSize)
mas01mj@631 63 db.configQuery["radius"] = float(params.radius)
mas01mj@631 64 db.configQuery["absThres"] = float(params.absThres)
mas01mj@631 65 db.configQuery["relThres"] = float(params.relThres)
mas01mj@631 66 db.configQuery["durRatio"] = float(params.durRatio)
mas01mj@637 67 db.configQuery["resFmt"] = "list"
mas01mj@627 68
mas01mj@627 69
mas01mj@626 70
mas01mj@626 71 try:
mas01mj@631 72 results = db.query(key = params.key)
mas01mj@626 73 except Exception as inst:
mas01mj@626 74 return json.dumps(dict(status = "error", message=str(inst)))
mas01mj@631 75 return json.dumps(dict(status = "ok", data = results.rawData))
mas01mj@626 76
mas01mj@626 77 if __name__ == "__main__":
mas01mj@626 78 app.run()