67
67
import codecs
68
68
import json
69
69
70
- def load_design_doc (directory , strip = False ):
70
+ def load_design_doc (directory , strip = False , predicate = lambda x : True ):
71
71
"""
72
72
Load a design document from the filesystem.
73
73
74
74
strip: remove leading and trailing whitespace from file contents,
75
75
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.
76
80
"""
77
81
objects = {}
78
82
@@ -87,6 +91,7 @@ def load_design_doc(directory, strip=False):
87
91
for name in filenames :
88
92
fkey = os .path .splitext (name )[0 ]
89
93
fullname = os .path .join (dirpath , name )
94
+ if not predicate (fullname ): continue
90
95
with codecs .open (fullname , 'r' , 'utf-8' ) as f :
91
96
contents = f .read ()
92
97
if name .endswith ('.json' ):
@@ -98,7 +103,9 @@ def load_design_doc(directory, strip=False):
98
103
for name in dirnames :
99
104
if name == '_attachments' :
100
105
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 ]
102
109
ob [subkey ] = subthing
103
110
104
111
return ob
0 commit comments