annotate piecrust/sources/blogarchives.py @ 856:9bb22bbe093c

refactor: Make the blog archives functional again. The blog archives are using the same pattern as the taxonomy support.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 06 Jun 2017 01:23:25 -0700
parents 08e02c2a2a1a
children d231a10d18f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import logging
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import datetime
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from piecrust.data.filters import PaginationFilter, IFilterClause
856
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
4 from piecrust.dataproviders.pageiterator import (
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
5 PageIterator, HardCodedFilterIterator, DateSortIterator)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
6 from piecrust.page import Page
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
7 from piecrust.pipelines._pagebaker import PageBaker
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
8 from piecrust.pipelines._pagerecords import PagePipelineRecordEntry
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
9 from piecrust.pipelines.base import (
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
10 ContentPipeline, get_record_name_for_source)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 from piecrust.routing import RouteParameter
856
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
12 from piecrust.sources.base import ContentItem
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
13 from piecrust.sources.generator import GeneratorSourceBase
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 logger = logging.getLogger(__name__)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
856
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
19 _year_index = """---
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
20 layout: %(template)s
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
21 ---
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
22 """
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
23
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
24
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
25 class BlogArchivesSource(GeneratorSourceBase):
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 SOURCE_NAME = 'blog_archives'
854
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
27 DEFAULT_PIPELINE_NAME = 'blog_archives'
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 def __init__(self, app, name, config):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 super().__init__(app, name, config)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31
856
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
32 tpl_name = config.get('template', '_year.html')
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
33 self._raw_item = _year_index % {'template': tpl_name}
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
34
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
35 def getSupportedRouteParameters(self):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
36 return [RouteParameter('year', RouteParameter.TYPE_INT4)]
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
37
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
38 def findContent(self, route_params):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
39 year = route_params['year']
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
40 spec = '_index[%04d]' % year
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
41 metadata = {'route_params': {'year': year}}
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
42 return ContentItem(spec, metadata)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 def prepareRenderContext(self, ctx):
856
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
45 ctx.pagination_source = self.inner_source
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46
856
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
47 route_params = ctx.page.source_metadata['route_params']
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
48 year = route_params.get('year')
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 if year is None:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 raise Exception(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 "Can't find the archive year in the route metadata")
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 if type(year) is not int:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 raise Exception(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 "The route for generator '%s' should specify an integer "
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 "parameter for 'year'." % self.name)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 flt = PaginationFilter()
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 flt.addClause(IsFromYearFilterClause(year))
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 ctx.pagination_filter = flt
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 ctx.custom_data['year'] = year
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 flt2 = PaginationFilter()
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 flt2.addClause(IsFromYearFilterClause(year))
856
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
65 it = PageIterator(self.inner_source)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
66 it._simpleNonSortedWrap(HardCodedFilterIterator, flt2)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
67 it._wrapAsSort(DateSortIterator, reverse=False)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 ctx.custom_data['archives'] = it
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 def _bakeDirtyYears(self, ctx, all_years, dirty_years):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 route = self.app.getGeneratorRoute(self.name)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 if route is None:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 raise Exception(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 "No routes have been defined for generator: %s" %
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 self.name)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 logger.debug("Using archive page: %s" % self.page_ref)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 fac = self.page_ref.getFactory()
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 for y in dirty_years:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 extra_route_metadata = {'year': y}
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 logger.debug("Queuing: %s [%s]" % (fac.ref_spec, y))
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 ctx.queueBakeJob(fac, route, extra_route_metadata, str(y))
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 ctx.runJobQueue()
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 class IsFromYearFilterClause(IFilterClause):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 def __init__(self, year):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 self.year = year
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 def pageMatches(self, fil, page):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 return (page.datetime.year == self.year)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 def _date_sorter(it):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 return sorted(it, key=lambda x: x.datetime)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98
854
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
99
856
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
100 class BlogArchivesPipelineRecordEntry(PagePipelineRecordEntry):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
101 def __init__(self):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
102 super().__init__()
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
103 self.year = None
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
104
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
105
854
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
106 class BlogArchivesPipeline(ContentPipeline):
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
107 PIPELINE_NAME = 'blog_archives'
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
108 PASS_NUM = 1
856
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
109 RECORD_ENTRY_CLASS = BlogArchivesPipelineRecordEntry
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
110
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
111 def __init__(self, source, ctx):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
112 if not isinstance(source, BlogArchivesSource):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
113 raise Exception("The blog archives pipeline only supports blog "
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
114 "archives content sources.")
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
115
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
116 super().__init__(source, ctx)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
117 self.inner_source = source.inner_source
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
118 self._tpl_name = source.config['template']
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
119 self._all_years = None
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
120 self._dirty_years = None
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
121 self._pagebaker = None
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
122
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
123 def initialize(self):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
124 self._pagebaker = PageBaker(self.app,
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
125 self.ctx.out_dir,
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
126 force=self.ctx.force)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
127 self._pagebaker.startWriterQueue()
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
128
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
129 def shutdown(self):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
130 self._pagebaker.stopWriterQueue()
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
131
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
132 def createJobs(self, ctx):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
133 logger.debug("Building blog archives for: %s" %
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
134 self.inner_source.name)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
135 self._buildDirtyYears(ctx)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
136 logger.debug("Got %d dirty years out of %d." %
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
137 (len(self._dirty_years), len(self._all_years)))
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
138
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
139 jobs = []
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
140 for y in self._dirty_years:
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
141 item = ContentItem(
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
142 '_index[%04d]' % y,
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
143 {'route_params': {'year': y}})
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
144 jobs.append(self.createJob(item))
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
145 if len(jobs) > 0:
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
146 return jobs
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
147 return None
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
148
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
149 def run(self, job, ctx, result):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
150 page = Page(self.source, job.content_item)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
151 prev_entry = ctx.previous_entry
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
152 cur_entry = result.record_entry
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
153 cur_entry.year = job.content_item.metadata['route_params']['year']
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
154 self._pagebaker.bake(page, prev_entry, cur_entry, [])
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
155
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
156 def postJobRun(self, ctx):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
157 # Create bake entries for the years that were *not* dirty.
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
158 # Otherwise, when checking for deleted pages, we would not find any
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
159 # outputs and would delete those files.
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
160 all_str_years = [str(y) for y in self._all_years]
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
161 for prev, cur in ctx.record_history.diffs:
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
162 if prev and not cur:
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
163 y = prev.year
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
164 if y in all_str_years:
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
165 logger.debug(
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
166 "Creating unbaked entry for year %s archive." % y)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
167 cur.year = y
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
168 cur.out_paths = list(prev.out_paths)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
169 cur.errors = list(prev.errors)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
170 else:
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
171 logger.debug(
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
172 "No page references year %s anymore." % y)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
173
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
174 def _buildDirtyYears(self, ctx):
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
175 all_years = set()
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
176 dirty_years = set()
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
177
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
178 record_name = get_record_name_for_source(self.inner_source)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
179 current_records = ctx.record_histories.current
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
180 cur_rec = current_records.getRecord(record_name)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
181 for cur_entry in cur_rec.getEntries():
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
182 dt = datetime.datetime.fromtimestamp(cur_entry.timestamp)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
183 all_years.add(dt.year)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
184 if cur_entry.was_any_sub_baked:
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
185 dirty_years.add(dt.year)
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
186
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
187 self._all_years = all_years
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
188 self._dirty_years = dirty_years
9bb22bbe093c refactor: Make the blog archives functional again.
Ludovic Chabant <ludovic@chabant.com>
parents: 854
diff changeset
189