comparison piecrust/sources/posts.py @ 853:f070a4fc033c

core: Continue PieCrust3 refactor, simplify pages. The asset pipeline is still the only function pipeline at this point. * No more `QualifiedPage`, and several other pieces of code deleted. * Data providers are simpler and more focused. For instance, the page iterator doesn't try to support other types of items. * Route parameters are proper known source metadata to remove the confusion between the two. * Make the baker and pipeline more correctly manage records and record histories. * Add support for record collapsing and deleting stale outputs in the asset pipeline.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 21 May 2017 00:06:59 -0700
parents 4850f8c21b6e
children 08e02c2a2a1a
comparison
equal deleted inserted replaced
852:4850f8c21b6e 853:f070a4fc033c
8 from piecrust.sources.base import REL_ASSETS, ContentItem 8 from piecrust.sources.base import REL_ASSETS, ContentItem
9 from piecrust.sources.fs import ( 9 from piecrust.sources.fs import (
10 FSContentSource, InvalidFileSystemEndpointError) 10 FSContentSource, InvalidFileSystemEndpointError)
11 from piecrust.sources.interfaces import ( 11 from piecrust.sources.interfaces import (
12 IPreparingSource, IInteractiveSource, InteractiveField) 12 IPreparingSource, IInteractiveSource, InteractiveField)
13 from piecrust.sources.mixins import ( 13 from piecrust.sources.mixins import SimpleAssetsSubDirMixin
14 SimplePaginationSourceMixin, SimpleAssetsSubDirMixin)
15 from piecrust.uriutil import uri_to_title 14 from piecrust.uriutil import uri_to_title
16 15
17 16
18 logger = logging.getLogger(__name__) 17 logger = logging.getLogger(__name__)
19 18
22 SimpleAssetsSubDirMixin, 21 SimpleAssetsSubDirMixin,
23 IPreparingSource, IInteractiveSource): 22 IPreparingSource, IInteractiveSource):
24 PATH_FORMAT = None 23 PATH_FORMAT = None
25 24
26 def __init__(self, app, name, config): 25 def __init__(self, app, name, config):
27 FSContentSource.__init__(self, app, name, config) 26 super().__init__(app, name, config)
27
28 config.setdefault('data_type', 'page_iterator')
29
28 self.auto_formats = app.config.get('site/auto_formats') 30 self.auto_formats = app.config.get('site/auto_formats')
29 self.default_auto_format = app.config.get('site/default_auto_format') 31 self.default_auto_format = app.config.get('site/default_auto_format')
30 self.supported_extensions = list(self.auto_formats) 32 self.supported_extensions = list(self.auto_formats)
31 33
32 @property 34 @property
132 return [ 134 return [
133 RouteParameter('slug', RouteParameter.TYPE_STRING), 135 RouteParameter('slug', RouteParameter.TYPE_STRING),
134 RouteParameter('day', RouteParameter.TYPE_INT2), 136 RouteParameter('day', RouteParameter.TYPE_INT2),
135 RouteParameter('month', RouteParameter.TYPE_INT2), 137 RouteParameter('month', RouteParameter.TYPE_INT2),
136 RouteParameter('year', RouteParameter.TYPE_INT4)] 138 RouteParameter('year', RouteParameter.TYPE_INT4)]
137
138 def getSourceIterator(self):
139 if self._source_it_cache is None:
140 it = SimplePaginationSourceMixin.getSourceIterator(self)
141 self._source_it_cache = list(it)
142 return self._source_it_cache
143 139
144 def setupPrepareParser(self, parser, app): 140 def setupPrepareParser(self, parser, app):
145 parser.add_argument( 141 parser.add_argument(
146 '-d', '--date', help="The date of the post, " 142 '-d', '--date', help="The date of the post, "
147 "in `year/month/day` format (defaults to today).") 143 "in `year/month/day` format (defaults to today).")
199 return False 195 return False
200 raise InvalidFileSystemEndpointError(self.name, 196 raise InvalidFileSystemEndpointError(self.name,
201 self.fs_endpoint_path) 197 self.fs_endpoint_path)
202 return True 198 return True
203 199
204 def _makeContentItem(self, path, slug, year, month, day): 200 def _makeContentItem(self, rel_path, slug, year, month, day):
205 path = path.replace('\\', '/') 201 path = os.path.join(self.fs_endpoint_path, rel_path)
206 timestamp = datetime.date(year, month, day) 202 timestamp = datetime.date(year, month, day)
207 metadata = { 203 metadata = {
208 'slug': slug, 204 'route_params': {
209 'year': year, 205 'slug': slug,
210 'month': month, 206 'year': year,
211 'day': day, 207 'month': month,
212 'date': timestamp} 208 'day': day},
209 'date': timestamp
210 }
211
212 _, ext = os.path.splitext(path)
213 if ext:
214 fmt = self.auto_formats.get(ext.lstrip('.'))
215 if fmt:
216 metadata['config'] = {'format': fmt}
217
213 return ContentItem(path, metadata) 218 return ContentItem(path, metadata)
214 219
215 220
216 class FlatPostsSource(PostsSource): 221 class FlatPostsSource(PostsSource):
217 SOURCE_NAME = 'posts/flat' 222 SOURCE_NAME = 'posts/flat'