Mercurial > piecrust2
comparison piecrust/sources/fs.py @ 1042:895f49c9833d
sources: Adds support for whitelist filtering to file-system sources.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 20 Jan 2018 17:23:34 -0800 |
parents | 8adc27285d93 |
children | 3678cddf99f9 |
comparison
equal
deleted
inserted
replaced
1041:717ac3c4ee77 | 1042:895f49c9833d |
---|---|
75 def __init__(self, app, name, config): | 75 def __init__(self, app, name, config): |
76 super().__init__(app, name, config) | 76 super().__init__(app, name, config) |
77 | 77 |
78 config.setdefault('data_type', 'asset_iterator') | 78 config.setdefault('data_type', 'asset_iterator') |
79 | 79 |
80 ig, ir = _parse_ignores(config.get('ignore')) | 80 self._ignore = _parse_patterns(config.get('ignore')) |
81 self._ignore_globs = ig | 81 self._filter = _parse_patterns(config.get('filter')) |
82 self._ignore_regexes = ir | |
83 | 82 |
84 def getContents(self, group): | 83 def getContents(self, group): |
85 if not self._checkFSEndpoint(): | 84 if not self._checkFSEndpoint(): |
86 return None | 85 return None |
87 | 86 |
89 if group is not None: | 88 if group is not None: |
90 parent_path = group.spec | 89 parent_path = group.spec |
91 | 90 |
92 names = filter(_filter_crap_files, osutil.listdir(parent_path)) | 91 names = filter(_filter_crap_files, osutil.listdir(parent_path)) |
93 | 92 |
94 final_names = [] | 93 items = [] |
94 groups = [] | |
95 for name in names: | 95 for name in names: |
96 path = os.path.join(parent_path, name) | 96 path = os.path.join(parent_path, name) |
97 if not self._filterIgnored(path): | 97 if self._filterPath(path): |
98 final_names.append(name) | 98 if os.path.isdir(path): |
99 | 99 metadata = self._createGroupMetadata(path) |
100 items = [] | 100 groups.append(ContentGroup(path, metadata)) |
101 groups = [] | 101 else: |
102 for name in final_names: | 102 metadata = self._createItemMetadata(path) |
103 path = os.path.join(parent_path, name) | 103 items.append(ContentItem(path, metadata)) |
104 if os.path.isdir(path): | |
105 metadata = self._createGroupMetadata(path) | |
106 groups.append(ContentGroup(path, metadata)) | |
107 else: | |
108 metadata = self._createItemMetadata(path) | |
109 items.append(ContentItem(path, metadata)) | |
110 self._finalizeContent(group, items, groups) | 104 self._finalizeContent(group, items, groups) |
111 return items + groups | 105 return items + groups |
112 | 106 |
113 def _filterIgnored(self, path): | 107 def _filterPath(self, path): |
114 rel_path = os.path.relpath(path, self.fs_endpoint_path) | 108 rel_path = os.path.relpath(path, self.fs_endpoint_path) |
115 for g in self._ignore_globs: | 109 |
116 if fnmatch.fnmatch(rel_path, g): | 110 if self._ignore is not None: |
111 if _matches_patterns(self._ignore, rel_path): | |
112 return False | |
113 | |
114 if self._filter is not None: | |
115 if _matches_patterns(self._filter, rel_path): | |
117 return True | 116 return True |
118 for r in self._ignore_regexes: | 117 return False |
119 if r.search(g): | 118 |
120 return True | 119 return True |
121 return False | |
122 | 120 |
123 def _createGroupMetadata(self, path): | 121 def _createGroupMetadata(self, path): |
124 return {} | 122 return {} |
125 | 123 |
126 def _createItemMetadata(self, path): | 124 def _createItemMetadata(self, path): |
177 def getSupportedRouteParameters(self): | 175 def getSupportedRouteParameters(self): |
178 return [ | 176 return [ |
179 RouteParameter('path', RouteParameter.TYPE_PATH)] | 177 RouteParameter('path', RouteParameter.TYPE_PATH)] |
180 | 178 |
181 | 179 |
182 def _parse_ignores(patterns): | 180 def _parse_patterns(patterns): |
181 if not patterns: | |
182 return None | |
183 | |
183 globs = [] | 184 globs = [] |
184 regexes = [] | 185 regexes = [] |
185 if patterns: | 186 for pat in patterns: |
186 for pat in patterns: | 187 if len(pat) > 2 and pat[0] == '/' and pat[-1] == '/': |
187 if len(pat) > 2 and pat[0] == '/' and pat[-1] == '/': | 188 regexes.append(re.compile(pat[1:-1])) |
188 regexes.append(re.compile(pat[1:-1])) | 189 else: |
189 else: | 190 globs.append(pat) |
190 globs.append(pat) | |
191 return globs, regexes | 191 return globs, regexes |
192 | |
193 | |
194 def _matches_patterns(patterns, subj): | |
195 globs, regexes = patterns | |
196 if globs: | |
197 for g in globs: | |
198 if fnmatch.fnmatch(subj, g): | |
199 return True | |
200 if regexes: | |
201 for r in regexes: | |
202 if r.search(subj): | |
203 return True | |
204 return False |