Mercurial > piecrust2
comparison piecrust/sources/fs.py @ 977:84fc72a17f7a
sources: Changes in related contents management.
- Remove `getParentGroup` method, use related contents instead.
- Return only a single group when asked for the parent.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 17 Oct 2017 01:11:54 -0700 |
parents | 7e7fc7926307 |
children | 45ad976712ec |
comparison
equal
deleted
inserted
replaced
976:b9374b3682f0 | 977:84fc72a17f7a |
---|---|
5 import logging | 5 import logging |
6 from piecrust import osutil | 6 from piecrust import osutil |
7 from piecrust.routing import RouteParameter | 7 from piecrust.routing import RouteParameter |
8 from piecrust.sources.base import ( | 8 from piecrust.sources.base import ( |
9 ContentItem, ContentGroup, ContentSource, | 9 ContentItem, ContentGroup, ContentSource, |
10 REL_LOGICAL_PARENT_ITEM, REL_LOGICAl_CHILD_GROUP) | 10 REL_PARENT_GROUP, REL_LOGICAL_PARENT_ITEM, REL_LOGICAl_CHILD_GROUP) |
11 | 11 |
12 | 12 |
13 logger = logging.getLogger(__name__) | 13 logger = logging.getLogger(__name__) |
14 | 14 |
15 | 15 |
108 metadata = self._createItemMetadata(path) | 108 metadata = self._createItemMetadata(path) |
109 items.append(ContentItem(path, metadata)) | 109 items.append(ContentItem(path, metadata)) |
110 self._finalizeContent(group, items, groups) | 110 self._finalizeContent(group, items, groups) |
111 return items + groups | 111 return items + groups |
112 | 112 |
113 def getParentGroup(self, item): | |
114 parent_dir = os.path.dirname(item.spec) | |
115 if len(parent_dir) >= len(self.fs_endpoint_path): | |
116 metadata = self._createGroupMetadata(parent_dir) | |
117 return ContentGroup(parent_dir, metadata) | |
118 | |
119 # Don't return a group for paths that are outside of our | |
120 # endpoint directory. | |
121 return None | |
122 | |
123 def _filterIgnored(self, path): | 113 def _filterIgnored(self, path): |
124 rel_path = os.path.relpath(path, self.fs_endpoint_path) | 114 rel_path = os.path.relpath(path, self.fs_endpoint_path) |
125 for g in self._ignore_globs: | 115 for g in self._ignore_globs: |
126 if fnmatch.fnmatch(rel_path, g): | 116 if fnmatch.fnmatch(rel_path, g): |
127 return True | 117 return True |
145 metadata = self._createGroupMetadata(path) | 135 metadata = self._createGroupMetadata(path) |
146 return ContentGroup(path, metadata) | 136 return ContentGroup(path, metadata) |
147 return None | 137 return None |
148 | 138 |
149 def getRelatedContents(self, item, relationship): | 139 def getRelatedContents(self, item, relationship): |
140 if relationship == REL_PARENT_GROUP: | |
141 parent_dir = os.path.dirname(item.spec) | |
142 if len(parent_dir) >= len(self.fs_endpoint_path): | |
143 metadata = self._createGroupMetadata(parent_dir) | |
144 return ContentGroup(parent_dir, metadata) | |
145 | |
146 # Don't return a group for paths that are outside of our | |
147 # endpoint directory. | |
148 return None | |
149 | |
150 if relationship == REL_LOGICAL_PARENT_ITEM: | 150 if relationship == REL_LOGICAL_PARENT_ITEM: |
151 # If we want the logical parent item of a folder, we find a | 151 # If we want the logical parent item of a folder, we find a |
152 # page file with the same name as the folder. | 152 # page file with the same name as the folder. |
153 if not item.is_group: | 153 if not item.is_group: |
154 raise ValueError() | 154 raise ValueError() |
165 if item.is_group: | 165 if item.is_group: |
166 raise ValueError() | 166 raise ValueError() |
167 dir_path, _ = os.path.splitext(item.spec) | 167 dir_path, _ = os.path.splitext(item.spec) |
168 if os.path.isdir(dir_path): | 168 if os.path.isdir(dir_path): |
169 metadata = self._createGroupMetadata(dir_path) | 169 metadata = self._createGroupMetadata(dir_path) |
170 return [ContentGroup(dir_path, metadata)] | 170 return ContentGroup(dir_path, metadata) |
171 return None | 171 return None |
172 | 172 |
173 return None | 173 return None |
174 | 174 |
175 def getSupportedRouteParameters(self): | 175 def getSupportedRouteParameters(self): |