Skip to content

Commit 835c5f1

Browse files
Daniel Holthdjc
authored andcommitted
Add predicate argument to loader function
1 parent 5f3b201 commit 835c5f1

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

couchdb/loader.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,16 @@
6767
import codecs
6868
import json
6969

70-
def load_design_doc(directory, strip=False):
70+
def load_design_doc(directory, strip=False, predicate=lambda x: True):
7171
"""
7272
Load a design document from the filesystem.
7373
7474
strip: remove leading and trailing whitespace from file contents,
7575
like couchdbkit.
76+
77+
predicate: function that is passed the full path to each file or directory.
78+
Each entry is only added to the document if predicate returns True.
79+
Can be used to ignore backup files etc.
7680
"""
7781
objects = {}
7882

@@ -87,6 +91,7 @@ def load_design_doc(directory, strip=False):
8791
for name in filenames:
8892
fkey = os.path.splitext(name)[0]
8993
fullname = os.path.join(dirpath, name)
94+
if not predicate(fullname): continue
9095
with codecs.open(fullname, 'r', 'utf-8') as f:
9196
contents = f.read()
9297
if name.endswith('.json'):
@@ -98,7 +103,9 @@ def load_design_doc(directory, strip=False):
98103
for name in dirnames:
99104
if name == '_attachments':
100105
raise NotImplementedError("_attachments are not supported")
101-
subkey, subthing = objects[os.path.join(dirpath, name)]
106+
fullpath = os.path.join(dirpath, name)
107+
if not predicate(fullpath): continue
108+
subkey, subthing = objects[fullpath]
102109
ob[subkey] = subthing
103110

104111
return ob

couchdb/tests/loader.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class LoaderTestCase(unittest.TestCase):
2222

2323
def test_loader(self):
2424
directory = os.path.join(os.path.dirname(__file__), '_loader')
25-
doc = loader.load_design_doc(directory, strip=True)
25+
doc = loader.load_design_doc(directory,
26+
strip=True,
27+
predicate=lambda x: \
28+
not x.endswith('.xml'))
2629
self.assertEqual(doc, expected)
2730

2831
def test_bad_directory(self):

0 commit comments

Comments
 (0)