comparison piecrust/dataproviders/blog.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 d231a10d18f9
comparison
equal deleted inserted replaced
853:f070a4fc033c 854:08e02c2a2a1a
1 import time 1 import time
2 import collections.abc 2 import collections.abc
3 from piecrust.dataproviders.base import DataProvider 3 from piecrust.dataproviders.base import DataProvider
4 from piecrust.generation.taxonomy import Taxonomy 4 from piecrust.dataproviders.pageiterator import PageIterator
5 from piecrust.sources.taxonomy import Taxonomy
5 6
6 7
7 class BlogDataProvider(DataProvider, collections.abc.Mapping): 8 class BlogDataProvider(DataProvider, collections.abc.Mapping):
8 PROVIDER_NAME = 'blog' 9 PROVIDER_NAME = 'blog'
9 10
10 debug_render_doc = """Provides a list of blog posts and yearly/monthly 11 debug_render_doc = """Provides a list of blog posts and yearly/monthly
11 archives.""" 12 archives."""
12 debug_render_dynamic = (['_debugRenderTaxonomies'] + 13 debug_render_dynamic = (['_debugRenderTaxonomies'] +
13 DataProvider.debug_render_dynamic) 14 DataProvider.debug_render_dynamic)
14 15
15 def __init__(self, source, page, override): 16 def __init__(self, source, page):
16 super(BlogDataProvider, self).__init__(source, page, override) 17 super().__init__(source, page)
17 self._yearly = None 18 self._yearly = None
18 self._monthly = None 19 self._monthly = None
19 self._taxonomies = {} 20 self._taxonomies = {}
20 self._ctx_set = False 21 self._ctx_set = False
21 22
70 year = post.datetime.strftime('%Y') 71 year = post.datetime.strftime('%Y')
71 72
72 posts_this_year = yearly_index.get(year) 73 posts_this_year = yearly_index.get(year)
73 if posts_this_year is None: 74 if posts_this_year is None:
74 timestamp = time.mktime( 75 timestamp = time.mktime(
75 (post.datetime.year, 1, 1, 0, 0, 0, 0, 0, -1)) 76 (post.datetime.year, 1, 1, 0, 0, 0, 0, 0, -1))
76 posts_this_year = BlogArchiveEntry(self._page, year, timestamp) 77 posts_this_year = BlogArchiveEntry(self._page, year, timestamp)
77 self._yearly.append(posts_this_year) 78 self._yearly.append(posts_this_year)
78 yearly_index[year] = posts_this_year 79 yearly_index[year] = posts_this_year
79 80
80 posts_this_year._data_source.append(post) 81 posts_this_year._data_source.append(post)
81 self._yearly = sorted(self._yearly, 82 self._yearly = sorted(self._yearly,
82 key=lambda e: e.timestamp, 83 key=lambda e: e.timestamp,
83 reverse=True) 84 reverse=True)
84 self._onIteration() 85 self._onIteration()
85 return self._yearly 86 return self._yearly
86 87
87 def _buildMonthlyArchive(self): 88 def _buildMonthlyArchive(self):
88 if self._monthly is not None: 89 if self._monthly is not None:
91 self._monthly = [] 92 self._monthly = []
92 for post in self._source.getPages(): 93 for post in self._source.getPages():
93 month = post.datetime.strftime('%B %Y') 94 month = post.datetime.strftime('%B %Y')
94 95
95 posts_this_month = next( 96 posts_this_month = next(
96 filter(lambda m: m.name == month, self._monthly), 97 filter(lambda m: m.name == month, self._monthly),
97 None) 98 None)
98 if posts_this_month is None: 99 if posts_this_month is None:
99 timestamp = time.mktime( 100 timestamp = time.mktime(
100 (post.datetime.year, post.datetime.month, 1, 101 (post.datetime.year, post.datetime.month, 1,
101 0, 0, 0, 0, 0, -1)) 102 0, 0, 0, 0, 0, -1))
102 posts_this_month = BlogArchiveEntry(self._page, month, timestamp) 103 posts_this_month = BlogArchiveEntry(
104 self._page, month, timestamp)
103 self._monthly.append(posts_this_month) 105 self._monthly.append(posts_this_month)
104 106
105 posts_this_month._data_source.append(post) 107 posts_this_month._data_source.append(post)
106 self._monthly = sorted(self._monthly, 108 self._monthly = sorted(self._monthly,
107 key=lambda e: e.timestamp, 109 key=lambda e: e.timestamp,
108 reverse=True) 110 reverse=True)
109 self._onIteration() 111 self._onIteration()
110 return self._monthly 112 return self._monthly
111 113
112 def _buildTaxonomy(self, tax_name): 114 def _buildTaxonomy(self, tax_name):
113 if tax_name in self._taxonomies: 115 if tax_name in self._taxonomies: