Mercurial > piecrust2
comparison piecrust/sources/fs.py @ 854:08e02c2a2a1a
core: Keep refactoring, this time to prepare for generator sources.
- Make a few APIs simpler.
- Content pipelines create their own jobs, so that generator sources can
keep aborting in `getContents`, but rely on their pipeline to generate
pages for baking.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 04 Jun 2017 23:34:28 -0700 |
parents | f070a4fc033c |
children | 448710d84121 |
comparison
equal
deleted
inserted
replaced
853:f070a4fc033c | 854:08e02c2a2a1a |
---|---|
1 import os.path | 1 import os.path |
2 import re | 2 import re |
3 import glob | |
3 import fnmatch | 4 import fnmatch |
4 import logging | 5 import logging |
5 from piecrust import osutil | 6 from piecrust import osutil |
6 from piecrust.routing import RouteParameter | 7 from piecrust.routing import RouteParameter |
7 from piecrust.sources.base import ContentItem, ContentGroup, ContentSource | 8 from piecrust.sources.base import ( |
9 ContentItem, ContentGroup, ContentSource, | |
10 REL_PARENT_GROUP, REL_LOGICAL_PARENT_ITEM, REL_LOGICAl_CHILD_GROUP) | |
8 | 11 |
9 | 12 |
10 logger = logging.getLogger(__name__) | 13 logger = logging.getLogger(__name__) |
11 | 14 |
12 | 15 |
71 ig, ir = _parse_ignores(config.get('ignore')) | 74 ig, ir = _parse_ignores(config.get('ignore')) |
72 self._ignore_globs = ig | 75 self._ignore_globs = ig |
73 self._ignore_regexes = ir | 76 self._ignore_regexes = ir |
74 | 77 |
75 def getContents(self, group): | 78 def getContents(self, group): |
76 logger.debug("Scanning for content in: %s" % self.fs_endpoint_path) | |
77 if not self._checkFSEndpoint(): | 79 if not self._checkFSEndpoint(): |
78 return None | 80 return None |
79 | 81 |
80 parent_path = self.fs_endpoint_path | 82 parent_path = self.fs_endpoint_path |
81 if group is not None: | 83 if group is not None: |
120 | 122 |
121 def _finalizeContent(self, parent_group, items, groups): | 123 def _finalizeContent(self, parent_group, items, groups): |
122 pass | 124 pass |
123 | 125 |
124 def getRelatedContents(self, item, relationship): | 126 def getRelatedContents(self, item, relationship): |
127 if relationship == REL_PARENT_GROUP: | |
128 parent_dir = os.path.dirname(item.spec) | |
129 if len(parent_dir) >= len(self.fs_endpoint_path): | |
130 metadata = self._createGroupMetadata(parent_dir) | |
131 return ContentGroup(parent_dir, metadata) | |
132 | |
133 # Don't return a group for paths that are outside of our | |
134 # endpoint directory. | |
135 return None | |
136 | |
137 if relationship == REL_LOGICAL_PARENT_ITEM: | |
138 # If we want the logical parent item of a folder, we find a | |
139 # page file with the same name as the folder. | |
140 if not item.is_group: | |
141 raise ValueError() | |
142 parent_glob = os.path.join(item.spec, '*') | |
143 for n in glob.iglob(parent_glob): | |
144 if os.path.isfile(n): | |
145 metadata = self._createItemMetadata(n) | |
146 return ContentItem(n, metadata) | |
147 return None | |
148 | |
149 if relationship == REL_LOGICAl_CHILD_GROUP: | |
150 # If we want the children items of an item, we look for | |
151 # a directory that has the same name as the item's file. | |
152 if item.is_group: | |
153 raise ValueError() | |
154 dir_path, _ = os.path.splitext(item.spec) | |
155 if os.path.isdir(dir_path): | |
156 metadata = self._createGroupMetadata(dir_path) | |
157 return [ContentGroup(dir_path, metadata)] | |
158 return None | |
159 | |
125 return None | 160 return None |
126 | 161 |
127 def findContent(self, route_params): | 162 def findContent(self, route_params): |
128 rel_path = route_params['path'] | 163 rel_path = route_params['path'] |
129 path = os.path.join(self.fs_endpoint_path, rel_path) | 164 path = os.path.join(self.fs_endpoint_path, rel_path) |