comparison piecrust/baking/baker.py @ 5:474c9882decf

Upgrade to Python 3.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 11 Aug 2014 22:36:47 -0700
parents f485ba500df3
children f5ca5c5bed85
comparison
equal deleted inserted replaced
4:7dc71c2dc9a8 5:474c9882decf
1 import time 1 import time
2 import os.path 2 import os.path
3 import codecs 3 import codecs
4 import urllib2 4 import urllib.request, urllib.error, urllib.parse
5 import hashlib 5 import hashlib
6 import logging 6 import logging
7 import threading 7 import threading
8 from Queue import Queue, Empty 8 from queue import Queue, Empty
9 from piecrust.baking.records import TransitionalBakeRecord, BakeRecordPageEntry 9 from piecrust.baking.records import TransitionalBakeRecord, BakeRecordPageEntry
10 from piecrust.chefutil import format_timed 10 from piecrust.chefutil import format_timed
11 from piecrust.data.filters import (PaginationFilter, HasFilterClause, 11 from piecrust.data.filters import (PaginationFilter, HasFilterClause,
12 IsFilterClause, AndBooleanClause) 12 IsFilterClause, AndBooleanClause)
13 from piecrust.processing.base import ProcessorPipeline 13 from piecrust.processing.base import ProcessorPipeline
59 base_uri, ext = os.path.splitext(uri) 59 base_uri, ext = os.path.splitext(uri)
60 return base_uri + suffix + ext 60 return base_uri + suffix + ext
61 61
62 def getOutputPath(self, uri): 62 def getOutputPath(self, uri):
63 bake_path = [self.out_dir] 63 bake_path = [self.out_dir]
64 decoded_uri = urllib2.unquote(uri.lstrip('/')).decode('utf8') 64 decoded_uri = urllib.parse.unquote(uri.lstrip('/')).decode('utf8')
65 if self.pretty_urls: 65 if self.pretty_urls:
66 bake_path.append(decoded_uri) 66 bake_path.append(decoded_uri)
67 bake_path.append('index.html') 67 bake_path.append('index.html')
68 else: 68 else:
69 name, ext = os.path.splitext(decoded_uri) 69 name, ext = os.path.splitext(decoded_uri)
186 186
187 rp = render_page(ctx) 187 rp = render_page(ctx)
188 188
189 out_dir = os.path.dirname(out_path) 189 out_dir = os.path.dirname(out_path)
190 if not os.path.isdir(out_dir): 190 if not os.path.isdir(out_dir):
191 os.makedirs(out_dir, 0755) 191 os.makedirs(out_dir, 0o755)
192 192
193 with codecs.open(out_path, 'w', 'utf-8') as fp: 193 with codecs.open(out_path, 'w', 'utf-8') as fp:
194 fp.write(rp.content.decode('utf-8')) 194 fp.write(rp.content.decode('utf-8'))
195 195
196 return ctx, rp 196 return ctx, rp
226 self.app.config.set('baker/is_baking', True) 226 self.app.config.set('baker/is_baking', True)
227 self.app.env.base_asset_url_format = '%site_root%%uri%' 227 self.app.env.base_asset_url_format = '%site_root%%uri%'
228 228
229 # Make sure the output directory exists. 229 # Make sure the output directory exists.
230 if not os.path.isdir(self.out_dir): 230 if not os.path.isdir(self.out_dir):
231 os.makedirs(self.out_dir, 0755) 231 os.makedirs(self.out_dir, 0o755)
232 232
233 # Load/create the bake record. 233 # Load/create the bake record.
234 record = TransitionalBakeRecord() 234 record = TransitionalBakeRecord()
235 record_cache = self.app.cache.getCache('bake_r') 235 record_cache = self.app.cache.getCache('bake_r')
236 record_name = hashlib.md5(self.out_dir).hexdigest() + '.record' 236 record_name = hashlib.md5(self.out_dir).hexdigest() + '.record'
311 for tn in tax_names: 311 for tn in tax_names:
312 source_taxonomies[tn] = set() 312 source_taxonomies[tn] = set()
313 313
314 # Now see which ones are 'dirty' based on our bake record. 314 # Now see which ones are 'dirty' based on our bake record.
315 logger.debug("Gathering dirty taxonomy terms") 315 logger.debug("Gathering dirty taxonomy terms")
316 for prev_entry, cur_entry in record.transitions.itervalues(): 316 for prev_entry, cur_entry in record.transitions.values():
317 for tax in self.app.taxonomies: 317 for tax in self.app.taxonomies:
318 changed_terms = None 318 changed_terms = None
319 # Re-bake all taxonomy pages that include new or changed 319 # Re-bake all taxonomy pages that include new or changed
320 # pages. 320 # pages.
321 if not prev_entry and cur_entry and cur_entry.was_baked: 321 if not prev_entry and cur_entry and cur_entry.was_baked:
341 set(changed_terms)) 341 set(changed_terms))
342 342
343 # Re-bake the combination pages for terms that are 'dirty'. 343 # Re-bake the combination pages for terms that are 'dirty'.
344 known_combinations = set() 344 known_combinations = set()
345 logger.debug("Gathering dirty term combinations") 345 logger.debug("Gathering dirty term combinations")
346 for prev_entry, cur_entry in record.transitions.itervalues(): 346 for prev_entry, cur_entry in record.transitions.values():
347 if cur_entry: 347 if cur_entry:
348 known_combinations |= cur_entry.used_taxonomy_terms 348 known_combinations |= cur_entry.used_taxonomy_terms
349 elif prev_entry: 349 elif prev_entry:
350 known_combinations |= prev_entry.used_taxonomy_terms 350 known_combinations |= prev_entry.used_taxonomy_terms
351 for sn, tn, terms in known_combinations: 351 for sn, tn, terms in known_combinations:
353 if not changed_terms.isdisjoint(set(terms)): 353 if not changed_terms.isdisjoint(set(terms)):
354 changed_terms.add(terms) 354 changed_terms.add(terms)
355 355
356 # Start baking those terms. 356 # Start baking those terms.
357 pool, queue, abort = self._createWorkerPool(record, self.num_workers) 357 pool, queue, abort = self._createWorkerPool(record, self.num_workers)
358 for source_name, source_taxonomies in buckets.iteritems(): 358 for source_name, source_taxonomies in buckets.items():
359 for tax_name, terms in source_taxonomies.iteritems(): 359 for tax_name, terms in source_taxonomies.items():
360 if len(terms) == 0: 360 if len(terms) == 0:
361 continue 361 continue
362 362
363 logger.debug("Baking '%s' for source '%s': %s" % 363 logger.debug("Baking '%s' for source '%s': %s" %
364 (tax_name, source_name, terms)) 364 (tax_name, source_name, terms))