My favorites | Sign in
Project Home
New issue   Search
for
  Advanced search   Search tips   Subscriptions

Issue 78 attachment: show_and_list.patch.txt (4.8 KB)

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Index: couchdb/view.py
===================================================================
--- couchdb/view.py (revision 150)
+++ couchdb/view.py (working copy)
@@ -75,6 +75,49 @@
output.write(json.dumps({'log': e.args[0]}))
return results

+ def maybe_wrap_response(result):
+ if isinstance(result, basestring):
+ return dict(body=result)
+ return result
+
+ def show_doc(fun, doc, req):
+ code = BOM_UTF8 + fun.encode('utf-8')
+ globals_ = {}
+ try:
+ exec code in {'log': _log}, globals_
+ except Exception, e:
+ return {'error': {
+ 'id': 'show_compilation_error',
+ 'reason': e.args[0]
+ }}
+ err = {'error': {
+ 'id': 'show_compilation_error',
+ 'reason': 'string must eval to a function '
+ '(ex: "def(doc, req): return {body: str}")'
+ }}
+ if len(globals_) != 1:
+ return err
+ function = globals_.values()[0]
+ if type(function) is not FunctionType:
+ return err
+ result = function(doc, req)
+ return maybe_wrap_response(result)
+
+ def list_begin(head, req):
+ function = functions[0]
+ result = function(head, None, req, None)
+ return maybe_wrap_response(result)
+
+ def list_row(row, req, row_info=None):
+ function = functions[0]
+ result = function(None, row, req, row_info)
+ return maybe_wrap_response(result)
+
+ def list_tail(req, row_info=None):
+ function = functions[0]
+ result = function(None, None, req, row_info)
+ return maybe_wrap_response(result)
+
def reduce(*cmd, **kwargs):
code = BOM_UTF8 + cmd[0][0].encode('utf-8')
args = cmd[1:][0]
@@ -114,7 +157,9 @@
return reduce(*cmd, **{'rereduce': True})

handlers = {'reset': reset, 'add_fun': add_fun, 'map_doc': map_doc,
- 'reduce': reduce, 'rereduce': rereduce}
+ 'reduce': reduce, 'rereduce': rereduce,
+ 'show_doc': show_doc,
+ 'list_begin': list_begin, 'list_row': list_row, 'list_tail': list_tail}

try:
while True:
Index: couchdb/tests/view.py
===================================================================
--- couchdb/tests/view.py (revision 150)
+++ couchdb/tests/view.py (working copy)
@@ -84,8 +84,46 @@
view.run(input=input, output=output)
self.assertEqual(output.getvalue(), '[true, [6]]\n')

+ def test_list(self):
+ input = StringIO('["reset"]\n')
+ output = StringIO()
+ view.run(input=input, output=output)
+ self.assertEquals(output.getvalue(), 'true\n')
+ input = StringIO('["add_fun", "def fun(head,row,req,row_info):\\n if head:\\n return \'Asteroids:\'\\n elif row:\\n return row[\'value\']\\n else:\\n return {\'body\': \'EOL\'}"]\n'
+ '["list_begin",{"total_rows":2,"offset":0},{"info": 1}]\n'
+ '["list_row",{"id":"1","key":"1","value":"Ceres"},{"info": 1},{"crap": "row_info?"}]\n'
+ '["list_row",{"id":"4","key":"4","value":"Vesta"},{"info": 1}]\n'
+ '["list_tail",{"info": 1}]\n')
+ output = StringIO()
+ view.run(input=input, output=output)
+ self.assertEquals(output.getvalue(),
+ 'true\n'
+ '{"body": "Asteroids:"}\n'
+ '{"body": "Ceres"}\n'
+ '{"body": "Vesta"}\n'
+ '{"body": "EOL"}\n')

+ def test_show(self):
+ input = StringIO('["reset"]\n')
+ output = StringIO()
+ view.run(input=input, output=output)
+ self.assertEquals(output.getvalue(), 'true\n')

+ # function returns a correct dictionary with body and headers
+ input = StringIO('["show_doc","def fun(doc, req):\\n return {\'body\': \'<doc>\'+doc[\'name\']+\'</doc>\', \'headers\': {\'Content-Type\' : \'application/xml\'}}\\n",{"_id":"foo","_rev":"2","name":"bar"},{"docId":"foo","info": 1}]\n')
+ output = StringIO()
+ view.run(input=input, output=output)
+ self.assertEquals(output.getvalue(),
+ '{"body": "<doc>bar</doc>", "headers": {"Content-Type": "application/xml"}}\n')
+
+ # function returns a simple string
+ input = StringIO('["show_doc","def fun(doc, req):\\n return doc[\'_id\']\\n",{"_id":"foo","_rev":"2","name":"bar"},{"docId":"foo","info": 1}]\n')
+ output = StringIO()
+ view.run(input=input, output=output)
+ self.assertEquals(output.getvalue(),
+ '{"body": "foo"}\n')
+
+
def suite():
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(view))
Powered by Google Project Hosting