comparison piecrust/app.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
56 def _load(self): 56 def _load(self):
57 if self.paths is None: 57 if self.paths is None:
58 self._values = self._validateAll({}) 58 self._values = self._validateAll({})
59 return 59 return
60 60
61 path_times = map(lambda p: os.path.getmtime(p), self.paths) 61 path_times = [os.path.getmtime(p) for p in self.paths]
62 cache_key = hashlib.md5("version=%s&cache=%d" % ( 62 cache_key = hashlib.md5("version=%s&cache=%d" % (
63 APP_VERSION, CACHE_VERSION)).hexdigest() 63 APP_VERSION, CACHE_VERSION)).hexdigest()
64 64
65 if self.cache.isValid('config.json', path_times): 65 if self.cache.isValid('config.json', path_times):
66 logger.debug("Loading configuration from cache...") 66 logger.debug("Loading configuration from cache...")
120 'enable_debug_info': True 120 'enable_debug_info': True
121 } 121 }
122 sitec = values.get('site') 122 sitec = values.get('site')
123 if sitec is None: 123 if sitec is None:
124 sitec = {} 124 sitec = {}
125 for key, val in default_sitec.iteritems(): 125 for key, val in default_sitec.items():
126 sitec.setdefault(key, val) 126 sitec.setdefault(key, val)
127 values['site'] = sitec 127 values['site'] = sitec
128 128
129 # Add a section for our cached information. 129 # Add a section for our cached information.
130 cachec = {} 130 cachec = {}
133 # Cache auto-format regexes. 133 # Cache auto-format regexes.
134 if not isinstance(sitec['auto_formats'], dict): 134 if not isinstance(sitec['auto_formats'], dict):
135 raise ConfigurationError("The 'site/auto_formats' setting must be a dictionary.") 135 raise ConfigurationError("The 'site/auto_formats' setting must be a dictionary.")
136 cachec['auto_formats_re'] = r"\.(%s)$" % ( 136 cachec['auto_formats_re'] = r"\.(%s)$" % (
137 '|'.join( 137 '|'.join(
138 map(lambda i: re.escape(i), sitec['auto_formats'].keys()))) 138 [re.escape(i) for i in list(sitec['auto_formats'].keys())]))
139 if sitec['default_auto_format'] not in sitec['auto_formats']: 139 if sitec['default_auto_format'] not in sitec['auto_formats']:
140 raise ConfigurationError("Default auto-format '%s' is not declared." % sitec['default_auto_format']) 140 raise ConfigurationError("Default auto-format '%s' is not declared." % sitec['default_auto_format'])
141 141
142 # Cache pagination suffix regex. 142 # Cache pagination suffix regex.
143 pgn_suffix = re.escape(sitec['pagination_suffix']) 143 pgn_suffix = re.escape(sitec['pagination_suffix'])
252 raise ConfigurationError("The 'site/routes' setting must be a list.") 252 raise ConfigurationError("The 'site/routes' setting must be a list.")
253 253
254 # Add the theme page source if no sources were defined in the theme 254 # Add the theme page source if no sources were defined in the theme
255 # configuration itself. 255 # configuration itself.
256 has_any_theme_source = False 256 has_any_theme_source = False
257 for sn, sc in sourcesc.iteritems(): 257 for sn, sc in sourcesc.items():
258 if sc.get('realm') == REALM_THEME: 258 if sc.get('realm') == REALM_THEME:
259 has_any_theme_source = True 259 has_any_theme_source = True
260 break 260 break
261 if not has_any_theme_source: 261 if not has_any_theme_source:
262 sitec['sources']['theme_pages'] = { 262 sitec['sources']['theme_pages'] = {
270 'source': 'theme_pages', 270 'source': 'theme_pages',
271 'func': 'pcurl(path)'}) 271 'func': 'pcurl(path)'})
272 272
273 # Sources have the `default` scanner by default, duh. Also, a bunch 273 # Sources have the `default` scanner by default, duh. Also, a bunch
274 # of other default values for other configuration stuff. 274 # of other default values for other configuration stuff.
275 for sn, sc in sourcesc.iteritems(): 275 for sn, sc in sourcesc.items():
276 if not isinstance(sc, dict): 276 if not isinstance(sc, dict):
277 raise ConfigurationError("All sources in 'site/sources' must be dictionaries.") 277 raise ConfigurationError("All sources in 'site/sources' must be dictionaries.")
278 sc.setdefault('type', 'default') 278 sc.setdefault('type', 'default')
279 sc.setdefault('fs_endpoint', sn) 279 sc.setdefault('fs_endpoint', sn)
280 sc.setdefault('data_endpoint', sn) 280 sc.setdefault('data_endpoint', sn)
294 raise ConfigurationError("All routes in 'site/routes' must have an 'url'.") 294 raise ConfigurationError("All routes in 'site/routes' must have an 'url'.")
295 if rc_url[0] != '/': 295 if rc_url[0] != '/':
296 raise ConfigurationError("Route URLs must start with '/'.") 296 raise ConfigurationError("Route URLs must start with '/'.")
297 if rc.get('source') is None: 297 if rc.get('source') is None:
298 raise ConfigurationError("Routes must specify a source.") 298 raise ConfigurationError("Routes must specify a source.")
299 if rc['source'] not in sourcesc.keys(): 299 if rc['source'] not in list(sourcesc.keys()):
300 raise ConfigurationError("Route is referencing unknown source: %s" % 300 raise ConfigurationError("Route is referencing unknown source: %s" %
301 rc['source']) 301 rc['source'])
302 rc.setdefault('taxonomy', None) 302 rc.setdefault('taxonomy', None)
303 rc.setdefault('page_suffix', '/%num%') 303 rc.setdefault('page_suffix', '/%num%')
304 304
305 # Validate taxonomies. 305 # Validate taxonomies.
306 sitec.setdefault('taxonomies', {}) 306 sitec.setdefault('taxonomies', {})
307 taxonomiesc = sitec.get('taxonomies') 307 taxonomiesc = sitec.get('taxonomies')
308 for tn, tc in taxonomiesc.iteritems(): 308 for tn, tc in taxonomiesc.items():
309 tc.setdefault('multiple', False) 309 tc.setdefault('multiple', False)
310 tc.setdefault('term', tn) 310 tc.setdefault('term', tn)
311 tc.setdefault('page', '_%s.%%ext%%' % tc['term']) 311 tc.setdefault('page', '_%s.%%ext%%' % tc['term'])
312 312
313 # Validate endpoints, and make sure the theme has a default source. 313 # Validate endpoints, and make sure the theme has a default source.
314 reserved_endpoints = set(['piecrust', 'site', 'page', 'route', 314 reserved_endpoints = set(['piecrust', 'site', 'page', 'route',
315 'assets', 'pagination', 'siblings', 315 'assets', 'pagination', 'siblings',
316 'family']) 316 'family'])
317 for name, src in sitec['sources'].iteritems(): 317 for name, src in sitec['sources'].items():
318 endpoint = src['data_endpoint'] 318 endpoint = src['data_endpoint']
319 if endpoint in reserved_endpoints: 319 if endpoint in reserved_endpoints:
320 raise ConfigurationError( 320 raise ConfigurationError(
321 "Source '%s' is using a reserved endpoint name: %s" % 321 "Source '%s' is using a reserved endpoint name: %s" %
322 (name, endpoint)) 322 (name, endpoint))
364 if sitec is None: 364 if sitec is None:
365 return 365 return
366 tplc = sitec.get('templates_dirs') 366 tplc = sitec.get('templates_dirs')
367 if tplc is None: 367 if tplc is None:
368 return 368 return
369 if isinstance(tplc, types.StringTypes): 369 if isinstance(tplc, str):
370 tplc = [tplc] 370 tplc = [tplc]
371 sitec['templates_dirs'] = filter(tplc, 371 sitec['templates_dirs'] = list(filter(tplc,
372 lambda p: os.path.join(self.theme_dir, p)) 372 lambda p: os.path.join(self.theme_dir, p)))
373 config.fixups.append(_fixupThemeTemplatesDir) 373 config.fixups.append(_fixupThemeTemplatesDir)
374 374
375 # We'll also need to flag all page sources as coming from 375 # We'll also need to flag all page sources as coming from
376 # the theme. 376 # the theme.
377 def _fixupThemeSources(index, config): 377 def _fixupThemeSources(index, config):
381 if sitec is None: 381 if sitec is None:
382 sitec = {} 382 sitec = {}
383 config['site'] = sitec 383 config['site'] = sitec
384 srcc = sitec.get('sources') 384 srcc = sitec.get('sources')
385 if srcc is not None: 385 if srcc is not None:
386 for sn, sc in srcc.iteritems(): 386 for sn, sc in srcc.items():
387 sc['realm'] = REALM_THEME 387 sc['realm'] = REALM_THEME
388 config.fixups.append(_fixupThemeSources) 388 config.fixups.append(_fixupThemeSources)
389 389
390 return config 390 return config
391 391
423 defs = {} 423 defs = {}
424 for cls in self.plugin_loader.getSources(): 424 for cls in self.plugin_loader.getSources():
425 defs[cls.SOURCE_NAME] = cls 425 defs[cls.SOURCE_NAME] = cls
426 426
427 sources = [] 427 sources = []
428 for n, s in self.config.get('site/sources').iteritems(): 428 for n, s in self.config.get('site/sources').items():
429 cls = defs.get(s['type']) 429 cls = defs.get(s['type'])
430 if cls is None: 430 if cls is None:
431 raise ConfigurationError("No such page source type: %s" % s['type']) 431 raise ConfigurationError("No such page source type: %s" % s['type'])
432 src = cls(self, n, s) 432 src = cls(self, n, s)
433 sources.append(src) 433 sources.append(src)
442 return routes 442 return routes
443 443
444 @cached_property 444 @cached_property
445 def taxonomies(self): 445 def taxonomies(self):
446 taxonomies = [] 446 taxonomies = []
447 for tn, tc in self.config.get('site/taxonomies').iteritems(): 447 for tn, tc in self.config.get('site/taxonomies').items():
448 tax = Taxonomy(self, tn, tc) 448 tax = Taxonomy(self, tn, tc)
449 taxonomies.append(tax) 449 taxonomies.append(tax)
450 return taxonomies 450 return taxonomies
451 451
452 def getSource(self, source_name): 452 def getSource(self, source_name):
489 dirs = [] 489 dirs = []
490 490
491 # Add custom directories from the configuration. 491 # Add custom directories from the configuration.
492 conf_dirs = self.config.get(conf_name) 492 conf_dirs = self.config.get(conf_name)
493 if conf_dirs is not None: 493 if conf_dirs is not None:
494 if isinstance(conf_dirs, types.StringTypes): 494 if isinstance(conf_dirs, str):
495 dirs.append(os.path.join(self.root_dir, conf_dirs)) 495 dirs.append(os.path.join(self.root_dir, conf_dirs))
496 else: 496 else:
497 dirs += filter(lambda p: os.path.join(self.root_dir, p), 497 dirs += [p for p in conf_dirs if os.path.join(self.root_dir, p)]
498 conf_dirs)
499 498
500 # Add the default directory if it exists. 499 # Add the default directory if it exists.
501 default_dir = os.path.join(self.root_dir, default_rel_dir) 500 default_dir = os.path.join(self.root_dir, default_rel_dir)
502 if os.path.isdir(default_dir): 501 if os.path.isdir(default_dir):
503 dirs.append(default_dir) 502 dirs.append(default_dir)