Mercurial > piecrust2
comparison piecrust/baking/records.py @ 711:ab5c6a8ae90a
bake: Replace hard-coded taxonomy support with "generator" system.
* Taxonomies are now implemented one or more `TaxonomyGenerator`s.
* A `BlogArchivesGenerator` stub is there but non-functional.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 26 May 2016 19:52:47 -0700 |
parents | 33ab9badfd7a |
children | 234d0c7c02cf |
comparison
equal
deleted
inserted
replaced
710:e85f29b28b84 | 711:ab5c6a8ae90a |
---|---|
6 | 6 |
7 | 7 |
8 logger = logging.getLogger(__name__) | 8 logger = logging.getLogger(__name__) |
9 | 9 |
10 | 10 |
11 def _get_transition_key(path, taxonomy_info=None): | 11 def _get_transition_key(path, extra_key=None): |
12 key = path | 12 key = path |
13 if taxonomy_info: | 13 if extra_key: |
14 key += '+%s:%s=' % (taxonomy_info.source_name, | 14 key += '+%s' % extra_key |
15 taxonomy_info.taxonomy_name) | |
16 if isinstance(taxonomy_info.term, tuple): | |
17 key += '/'.join(taxonomy_info.term) | |
18 else: | |
19 key += taxonomy_info.term | |
20 return hashlib.md5(key.encode('utf8')).hexdigest() | 15 return hashlib.md5(key.encode('utf8')).hexdigest() |
21 | 16 |
22 | 17 |
23 class BakeRecord(Record): | 18 class BakeRecord(Record): |
24 RECORD_VERSION = 18 | 19 RECORD_VERSION = 18 |
67 | 62 |
68 def copyRenderInfo(self): | 63 def copyRenderInfo(self): |
69 return copy.deepcopy(self.render_info) | 64 return copy.deepcopy(self.render_info) |
70 | 65 |
71 | 66 |
72 class TaxonomyInfo(object): | |
73 def __init__(self, taxonomy_name, source_name, term): | |
74 self.taxonomy_name = taxonomy_name | |
75 self.source_name = source_name | |
76 self.term = term | |
77 | |
78 | |
79 class BakeRecordEntry(object): | 67 class BakeRecordEntry(object): |
80 """ An entry in the bake record. | 68 """ An entry in the bake record. |
81 | |
82 The `taxonomy_info` attribute should be a tuple of the form: | |
83 (taxonomy name, term, source name) | |
84 """ | 69 """ |
85 FLAG_NONE = 0 | 70 FLAG_NONE = 0 |
86 FLAG_NEW = 2**0 | 71 FLAG_NEW = 2**0 |
87 FLAG_SOURCE_MODIFIED = 2**1 | 72 FLAG_SOURCE_MODIFIED = 2**1 |
88 FLAG_OVERRIDEN = 2**2 | 73 FLAG_OVERRIDEN = 2**2 |
89 | 74 |
90 def __init__(self, source_name, path, taxonomy_info=None): | 75 def __init__(self, source_name, path, extra_key=None): |
91 self.source_name = source_name | 76 self.source_name = source_name |
92 self.path = path | 77 self.path = path |
93 self.taxonomy_info = taxonomy_info | 78 self.extra_key = extra_key |
94 self.flags = self.FLAG_NONE | 79 self.flags = self.FLAG_NONE |
95 self.config = None | 80 self.config = None |
96 self.errors = [] | 81 self.errors = [] |
97 self.subs = [] | 82 self.subs = [] |
98 | 83 |
143 for pinfo in o.render_info: | 128 for pinfo in o.render_info: |
144 if pinfo: | 129 if pinfo: |
145 res |= pinfo.used_source_names | 130 res |= pinfo.used_source_names |
146 return res | 131 return res |
147 | 132 |
148 def getAllUsedTaxonomyTerms(self): | |
149 res = set() | |
150 for o in self.subs: | |
151 for pinfo in o.render_info: | |
152 if pinfo: | |
153 res |= pinfo.used_taxonomy_terms | |
154 return res | |
155 | |
156 | 133 |
157 class TransitionalBakeRecord(TransitionalRecord): | 134 class TransitionalBakeRecord(TransitionalRecord): |
158 def __init__(self, previous_path=None): | 135 def __init__(self, previous_path=None): |
159 super(TransitionalBakeRecord, self).__init__(BakeRecord, | 136 super(TransitionalBakeRecord, self).__init__(BakeRecord, |
160 previous_path) | 137 previous_path) |
166 entry.flags |= BakeRecordEntry.FLAG_SOURCE_MODIFIED | 143 entry.flags |= BakeRecordEntry.FLAG_SOURCE_MODIFIED |
167 self.dirty_source_names.add(entry.source_name) | 144 self.dirty_source_names.add(entry.source_name) |
168 super(TransitionalBakeRecord, self).addEntry(entry) | 145 super(TransitionalBakeRecord, self).addEntry(entry) |
169 | 146 |
170 def getTransitionKey(self, entry): | 147 def getTransitionKey(self, entry): |
171 return _get_transition_key(entry.path, entry.taxonomy_info) | 148 return _get_transition_key(entry.path, entry.extra_key) |
172 | 149 |
173 def getPreviousAndCurrentEntries(self, path, taxonomy_info=None): | 150 def getPreviousAndCurrentEntries(self, path, extra_key=None): |
174 key = _get_transition_key(path, taxonomy_info) | 151 key = _get_transition_key(path, extra_key) |
175 pair = self.transitions.get(key) | 152 pair = self.transitions.get(key) |
176 return pair | 153 return pair |
177 | 154 |
178 def getOverrideEntry(self, path, uri): | 155 def getOverrideEntry(self, path, uri): |
179 for pair in self.transitions.values(): | 156 for pair in self.transitions.values(): |
182 for o in cur.subs: | 159 for o in cur.subs: |
183 if o.out_uri == uri: | 160 if o.out_uri == uri: |
184 return cur | 161 return cur |
185 return None | 162 return None |
186 | 163 |
187 def getPreviousEntry(self, path, taxonomy_info=None): | 164 def getPreviousEntry(self, path, extra_key=None): |
188 pair = self.getPreviousAndCurrentEntries(path, taxonomy_info) | 165 pair = self.getPreviousAndCurrentEntries(path, extra_key) |
189 if pair is not None: | 166 if pair is not None: |
190 return pair[0] | 167 return pair[0] |
191 return None | 168 return None |
192 | 169 |
193 def getCurrentEntry(self, path, taxonomy_info=None): | 170 def getCurrentEntry(self, path, extra_key=None): |
194 pair = self.getPreviousAndCurrentEntries(path, taxonomy_info) | 171 pair = self.getPreviousAndCurrentEntries(path, extra_key) |
195 if pair is not None: | 172 if pair is not None: |
196 return pair[1] | 173 return pair[1] |
197 return None | 174 return None |
198 | 175 |
199 def collapseEntry(self, prev_entry): | 176 def collapseEntry(self, prev_entry): |