Skip to content

Commit 360d7cf

Browse files
committed
Automatically dedent map/reduce code in ViewDefinition.
--HG-- extra : convert_revision : svn%3A7a298fb0-333a-0410-83e7-658617cd9cf3/trunk%40120
1 parent ff301f2 commit 360d7cf

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

ChangeLog.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ http://couchdb-python.googlecode.com/svn/tags/0.4.1
2424
triggering an internal server error (issue 31).
2525
* Added a new `couchdb.design` module that provides functionality for
2626
managing views in design documents, so that they can be defined in the
27-
Python application code, and kept in sync with the design documents
28-
actually stored in the database.
27+
Python application code, and the design documents actually stored in the
28+
database can be kept in sync with the definitions in the code.
2929

3030

3131
Version 0.4

couchdb/design.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
from copy import deepcopy
1212
from itertools import groupby
1313
from operator import attrgetter
14+
from textwrap import dedent
1415

1516

1617
class ViewDefinition(object):
17-
"""Definition of a view stored in a specific design document.
18+
r"""Definition of a view stored in a specific design document.
1819
1920
An instance of this class can be used to access the results of the view,
2021
as well as to keep the view definition in the design document up to date
@@ -53,6 +54,10 @@ def __init__(self, design, name, map_fun, reduce_fun=None,
5354
language='javascript', wrapper=None):
5455
"""Initialize the view definition.
5556
57+
Note that the code in `map_fun` and `reduce_fun` is automatically
58+
dedented, that is, any common leading whitespace is removed from each
59+
line.
60+
5661
:param design: the name of the design document
5762
:param name: the name of the view
5863
:param map_fun: the map function code
@@ -65,7 +70,9 @@ def __init__(self, design, name, map_fun, reduce_fun=None,
6570
design = design[8:]
6671
self.design = design
6772
self.name = name
68-
self.map_fun = map_fun
73+
self.map_fun = dedent(map_fun.lstrip('\n\r'))
74+
if reduce_fun:
75+
reduce_fun = dedent(reduce_fun.lstrip('\n\r'))
6976
self.reduce_fun = reduce_fun
7077
self.language = language
7178
self.wrapper = wrapper
@@ -121,7 +128,8 @@ def sync_many(db, views, remove_missing=False, callback=None):
121128
instances should be removed
122129
:param callback: a callback function that is invoked when a design
123130
document gets updated; the callback gets passed the
124-
design document as only parameter
131+
design document as only parameter, before that doc
132+
has actually been saved back to the database
125133
"""
126134
docs = []
127135

couchdb/schema.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,24 @@ def _to_json(self, value):
174174

175175

176176
class View(object):
177-
"""Descriptor that can be used to bind a view definition to a property of
177+
r"""Descriptor that can be used to bind a view definition to a property of
178178
a `Document` class.
179179
180180
>>> class Person(Document):
181181
... name = TextField()
182182
... age = IntegerField()
183-
... by_name = View('people', '''function(doc) {
184-
... emit(doc.name, doc.age);
185-
... }''')
183+
... by_name = View('people', '''\
184+
... function(doc) {
185+
... emit(doc.name, doc.age);
186+
... }''')
186187
>>> Person.by_name
187188
<ViewDefinition '_view/people/by_name'>
188189
190+
>>> print Person.by_name.map_fun
191+
function(doc) {
192+
emit(doc.name, doc.age);
193+
}
194+
189195
That property can be used as a function, which will execute the view.
190196
191197
>>> from couchdb import Database

0 commit comments

Comments
 (0)