annotate bindings/python/examples/server.py @ 770:c54bc2ffbf92 tip

update tags
author convert-repo
date Fri, 16 Dec 2011 11:34:01 +0000
parents 2eaea1afd6b3
children
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@639 39 results = db.status()
mas01mj@626 40 return json.dumps(dict(status = "ok", data = results))
mas01mj@626 41
mas01mj@626 42 class query:
mas01mj@626 43 def GET(self):
mas01mj@627 44 web.header("Content-Type", "application/json")
mas01mj@627 45 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 46 results = dict()
mas01mj@631 47 db = Pyadb(path = dbPath, mode = "r")
mas01mj@637 48
mas01mj@631 49 if not params.includeKeys == []:
mas01mj@631 50 db.configQuery["includeKeys"] = map(str, params.includeKeys)
mas01mj@631 51
mas01mj@631 52 if params.excludeKeys:
mas01mj@631 53 foo = map(str, params.excludeKeys)
mas01mj@631 54 db.configQuery["excludeKeys"] = foo
mas01mj@626 55
mas01mj@631 56 db.configQuery["ntracks"] = int(params.ntracks)
mas01mj@631 57 db.configQuery["npoints"] = int(params.npoints)
mas01mj@631 58 db.configQuery["seqStart"] = int(params.seqStart)
mas01mj@631 59 db.configQuery["seqLength"] = int(params.seqLength)
mas01mj@631 60 db.configQuery["hopSize"] = int(params.hopSize)
mas01mj@631 61 db.configQuery["radius"] = float(params.radius)
mas01mj@631 62 db.configQuery["absThres"] = float(params.absThres)
mas01mj@631 63 db.configQuery["relThres"] = float(params.relThres)
mas01mj@631 64 db.configQuery["durRatio"] = float(params.durRatio)
mas01mj@637 65 db.configQuery["resFmt"] = "list"
mas01mj@627 66
mas01mj@627 67
mas01mj@626 68
mas01mj@626 69 try:
mas01mj@631 70 results = db.query(key = params.key)
mas01mj@626 71 except Exception as inst:
mas01mj@626 72 return json.dumps(dict(status = "error", message=str(inst)))
mas01mj@631 73 return json.dumps(dict(status = "ok", data = results.rawData))
mas01mj@626 74
mas01mj@626 75 if __name__ == "__main__":
mas01mj@626 76 app.run()