forked from maximilianh/pubMunch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubServeAnnot
executable file
·77 lines (65 loc) · 2.28 KB
/
pubServeAnnot
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
76
77
#!/usr/bin/env python
# run python's built in webserver, accept data via a POST request, run annotators on it and return
# the results
import SimpleHTTPServer as shs
# load default python packages
import SocketServer, cgi, json, os
import logging, optparse, sys
# add <scriptDir>/lib/ to package search path
progFile = os.path.abspath(sys.argv[0])
progDir = os.path.dirname(progFile)
pubToolsLibDir = os.path.join(progDir, "lib")
sys.path.insert(0, pubToolsLibDir)
# now load our own libraries
import pubAlg, pubStore, pubGeneric
# ====
PORT = 8000
class ServerHandler(shs.SimpleHTTPRequestHandler):
def do_GET(self):
#print "headers", self.headers
#SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
html = \
"""
<html>
<body>
<form action="" method="POST">
<h3>PubTools annotation server</h3>
Text:<p>
<textarea name="text" rows="20" cols="90"></textarea><p>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
"""
#shs.SimpleHTTPRequestHandler.send_head(self)
self.wfile.write(html)
def do_POST(self):
print "headers", (self.headers)
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
dataDict = {}
for param in form:
dataDict[param] = form.getfirst(param)
#shs.SimpleHTTPRequestHandler.do_GET(self)
reader = pubStore.PubReaderTest(None, dataDict["text"])
alg = pubAlg.getAlg("bandSearch", "Annotate")
retData = list(pubAlg.runAnnotateDict(reader, alg, {}))
retStr = json.dumps(retData, indent=4, separators=(',', ': '))
#shs.SimpleHTTPRequestHandler.send_head(self)
self.wfile.write("\n")
self.wfile.write(retStr)
def main():
# === COMMAND LINE INTERFACE, OPTIONS AND HELP ===
parser = optparse.OptionParser("""usage: %prog [options] - serve annotation algorithms via http""")
pubGeneric.addGeneralOptions(parser)
(options, args) = parser.parse_args()
pubGeneric.setupLogging(progFile, options)
Handler = ServerHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
main()