Skip to content
Newer
Older
100755 100 lines (78 sloc) 2.65 KB
ef0440b Python variant of couchdb external hook.
Robert Newson authored Oct 8, 2009
1 #!/usr/bin/python
2
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
3 import httplib
4 import optparse as op
5 import sys
6 import traceback
9e37cdb url escape query parameters.
Robert Newson authored Oct 8, 2009
7 import urllib
7217c19 simplify Python script.
Robert Newson authored Oct 9, 2009
8
ef0440b Python variant of couchdb external hook.
Robert Newson authored Oct 8, 2009
9 try:
10 import json
11 except:
12 import simplejson as json
13
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
14 __usage__ = "%prog [OPTIONS]"
15
082b7da httplib lower-cases HTTP responses headers -- so fix that ourselves.
Robert Newson authored Dec 23, 2009
16 httpdict = {"etag":"ETag", "content-type":"Content-Type"}
17
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
18 def options():
19 return [
cbfe065 remove registry and indexkey, change output paths
Robert Newson authored Nov 24, 2009
20 op.make_option('--remote-host', dest='remote_host',
21 default="localhost",
b9dc7a9 fix external hook and external documentation.
Robert Newson authored Nov 20, 2009
22 help="Hostname of the couchdb-lucene server. [%default]"),
cbfe065 remove registry and indexkey, change output paths
Robert Newson authored Nov 24, 2009
23 op.make_option('--remote-port', dest='remote_port', type='int',
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
24 default=5985,
b9dc7a9 fix external hook and external documentation.
Robert Newson authored Nov 20, 2009
25 help="Port of the couchdb-lucene server. [%default]"),
4912c84 fix external hook for ini changes.
Robert Newson authored Jan 9, 2010
26 op.make_option('--local-key', dest='key',
27 default="local",
28 help="Configured key name for this couchdb instance. [%default]"),
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
29 ]
30
31 def main():
32 parser = op.OptionParser(usage=__usage__, option_list=options())
33 opts, args = parser.parse_args()
34
35 if len(args):
36 parser.error("Unrecognized arguments: %s" % ' '.join(args))
caccea4 avoid JSON object encoding problem by changing multiple query feature…
Robert Newson authored May 17, 2010
37 for req in requests():
54cc92b don't reuse connection as httplib has buggy persistent connection sup…
Robert Newson authored Dec 29, 2009
38 res = httplib.HTTPConnection(opts.remote_host, opts.remote_port)
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
39 try:
caccea4 avoid JSON object encoding problem by changing multiple query feature…
Robert Newson authored May 17, 2010
40 resp = respond(res, req, opts.key)
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
41 except Exception, e:
42 body = traceback.format_exc()
43 resp = mkresp(500, body, {"Content-Type": "text/plain"})
44
54cc92b don't reuse connection as httplib has buggy persistent connection sup…
Robert Newson authored Dec 29, 2009
45 res.close()
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
46 sys.stdout.write(json.dumps(resp))
47 sys.stdout.write("\n")
48 sys.stdout.flush()
49
ef0440b Python variant of couchdb external hook.
Robert Newson authored Oct 8, 2009
50 def requests():
51 line = sys.stdin.readline()
52 while line:
caccea4 avoid JSON object encoding problem by changing multiple query feature…
Robert Newson authored May 17, 2010
53 yield json.loads(line)
ef0440b Python variant of couchdb external hook.
Robert Newson authored Oct 8, 2009
54 line = sys.stdin.readline()
55
caccea4 avoid JSON object encoding problem by changing multiple query feature…
Robert Newson authored May 17, 2010
56 def respond(res, req, key):
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
57 path = req.get("path", [])
989c04a make Python external hook script work for cache hits too.
Robert Newson authored Nov 14, 2009
58
980372a tidy up python script
Robert Newson authored Dec 21, 2009
59 # Drop name of external hook.
60 del path[1]
18b332e consolidate all servlets into one. this makes URL's more RESTful and …
Robert Newson authored Mar 21, 2010
61
c11d33e handle databases with embedded slashes
Robert Newson authored Dec 21, 2009
62 # URL-escape each part
63 for index, item in enumerate(path):
64 path[index] = urllib.quote(path[index], "")
980372a tidy up python script
Robert Newson authored Dec 21, 2009
65
18b332e consolidate all servlets into one. this makes URL's more RESTful and …
Robert Newson authored Mar 21, 2010
66 path = '/'.join(['', key] + path)
caccea4 avoid JSON object encoding problem by changing multiple query feature…
Robert Newson authored May 17, 2010
67 params = urllib.urlencode(dict([k, v.encode('utf-8')] for k, v in req["query"].items()))
68 path = '?'.join([path, params])
24c528e allow -u/--url= override for couchdb-lucene location.
Robert Newson authored Oct 9, 2009
69
989c04a make Python external hook script work for cache hits too.
Robert Newson authored Nov 14, 2009
70 req_headers = {}
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
71 for h in req.get("headers", []):
72 if h.lower() in ["accept", "if-none-match"]:
989c04a make Python external hook script work for cache hits too.
Robert Newson authored Nov 14, 2009
73 req_headers[h] = req["headers"][h]
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
74
f4799dc handle older Python versions in hook script.
Robert Newson authored Jan 18, 2010
75 # verb renamed to method in 0.11 onwards.
76 if "method" in req:
77 method = req["method"]
78 else:
79 method = req["verb"]
b7c592f more fixes for new _cleanup method.
Robert Newson authored Jan 31, 2010
80
ddfd64d allow either 'verb' or 'method' in req object.
Robert Newson authored Jan 13, 2010
81 res.request(method, path, headers=req_headers)
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
82 resp = res.getresponse()
989c04a make Python external hook script work for cache hits too.
Robert Newson authored Nov 14, 2009
83
84 resp_headers = {}
85 for h, v in resp.getheaders():
082b7da httplib lower-cases HTTP responses headers -- so fix that ourselves.
Robert Newson authored Dec 23, 2009
86 if h.lower() in httpdict:
87 resp_headers[httpdict[h]] = resp.getheader(h, [])
989c04a make Python external hook script work for cache hits too.
Robert Newson authored Nov 14, 2009
88
89 return mkresp(resp.status, resp.read(), resp_headers)
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
90
91 def mkresp(code, body, headers=None):
92 ret = {"code": code, "body": body}
93 if headers is not None:
94 ret["headers"] = headers
95 return ret
ef0440b Python variant of couchdb external hook.
Robert Newson authored Oct 8, 2009
96
97 if __name__ == "__main__":
98 main()
416a059 @davisp Refactored the external hook a bit.
davisp authored Oct 22, 2009
99
Something went wrong with that request. Please try again.