comparison piecrust/app.py @ 681:894d286b348f

internal: Refactor config loading some more. * Remove fixup code in the app to make the app config class more standalone. * Remove support for old-style variants... maybe bring it back later. * Try and fix various bugs introduced by subtle config value overriding order changes.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 08 Mar 2016 01:07:34 -0800
parents 3df808b133f8
children 4285b2c9b872
comparison
equal deleted inserted replaced
680:c2ea75e37540 681:894d286b348f
51 @cached_property 51 @cached_property
52 def config(self): 52 def config(self):
53 logger.debug("Creating site configuration...") 53 logger.debug("Creating site configuration...")
54 start_time = time.perf_counter() 54 start_time = time.perf_counter()
55 55
56 paths = []
57 if not self.theme_site: 56 if not self.theme_site:
58 if self.theme_dir: 57 path = os.path.join(self.root_dir, CONFIG_PATH)
59 paths.append(os.path.join(self.theme_dir, THEME_CONFIG_PATH))
60 paths.append(os.path.join(self.root_dir, CONFIG_PATH))
61 else: 58 else:
62 paths.append(os.path.join(self.root_dir, THEME_CONFIG_PATH)) 59 path = os.path.join(self.root_dir, THEME_CONFIG_PATH)
63 preview_path = os.path.join( 60
64 self.root_dir, 'configs', 'theme_preview.yml') 61 theme_path = None
65 if os.path.isfile(preview_path): 62 if not self.theme_site and self.theme_dir:
66 paths.append(preview_path) 63 theme_path = os.path.join(self.theme_dir, THEME_CONFIG_PATH)
67 64
68 config_cache = self.cache.getCache('app') 65 config_cache = self.cache.getCache('app')
69 config = PieCrustConfiguration( 66 config = PieCrustConfiguration(
70 paths, config_cache, theme_config=self.theme_site) 67 path=path, theme_path=theme_path,
71 if not self.theme_site and self.theme_dir: 68 cache=config_cache, theme_config=self.theme_site)
72 config.addFixup(_fixup_theme_config) 69
70 if self.theme_site:
71 variant_path = os.path.join(
72 self.root_dir, 'configs', 'theme_preview.yml')
73 config.addVariant(variant_path, raise_if_not_found=False)
73 74
74 self.env.stepTimer('SiteConfigLoad', time.perf_counter() - start_time) 75 self.env.stepTimer('SiteConfigLoad', time.perf_counter() - start_time)
75 return config 76 return config
76 77
77 @cached_property 78 @cached_property
199 default_dir = os.path.join(self.root_dir, default_rel_dir) 200 default_dir = os.path.join(self.root_dir, default_rel_dir)
200 if os.path.isdir(default_dir): 201 if os.path.isdir(default_dir):
201 dirs.append(default_dir) 202 dirs.append(default_dir)
202 203
203 return dirs 204 return dirs
204
205
206 def _fixup_theme_config(index, config):
207 if index != 0:
208 # We only want to affect the theme config, which is first.
209 return
210
211 # See if we want to generate the default theme content model.
212 sitec = config.setdefault('site', {})
213 gen_default_model = sitec.setdefault('use_default_theme_content', True)
214 if gen_default_model:
215 # Create a default `theme_pages` source.
216 srcc = sitec.setdefault('sources', {})
217 if not isinstance(srcc, dict):
218 raise Exception("Theme configuration has invalid `site/sources`. "
219 "Must be a dictionary.")
220 default_theme_sources = {
221 'theme_pages': {
222 'type': 'default',
223 'ignore_missing_dir': True,
224 'fs_endpoint': 'pages',
225 'data_endpoint': 'site.pages',
226 'default_layout': 'default',
227 'item_name': 'page'
228 }
229 }
230 sitec['sources'] = merge_dicts(default_theme_sources, srcc)
231
232 sitec.setdefault('theme_tag_page', 'theme_pages:_tag.%ext%')
233 sitec.setdefault('theme_category_page', 'theme_pages:_category.%ext%')
234
235 # Create a default route for `theme_pages`.
236 rtc = sitec.setdefault('routes', [])
237 if not isinstance(rtc, list):
238 raise Exception("Theme configuration has invalid `site/routes`. "
239 "Must be a list.")
240 rtc.append({
241 'url': '/%path:slug%',
242 'source': 'theme_pages',
243 'func': 'pcurl(slug)'})
244
245 # Make all sources belong to the "theme" realm.
246 srcc = sitec.get('sources')
247 if srcc and isinstance(srcc, dict):
248 for sn, sc in srcc.items():
249 sc['realm'] = REALM_THEME
250 205
251 206
252 def apply_variant_and_values(app, config_variant=None, config_values=None): 207 def apply_variant_and_values(app, config_variant=None, config_values=None):
253 if config_variant is not None: 208 if config_variant is not None:
254 logger.debug("Adding configuration variant '%s'." % config_variant) 209 logger.debug("Adding configuration variant '%s'." % config_variant)