annotate piecrust/appconfig.py @ 651:cc2d212c3ba1 2.0.0b5

cm: Regenerate the CHANGELOG.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 16 Feb 2016 22:32:58 -0800
parents 25df894f9ab9
children 3ceeca7bb71c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import re
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import copy
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import json
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import urllib
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 import logging
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 import hashlib
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 import collections
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 import yaml
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 from piecrust import (
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 APP_VERSION, CACHE_VERSION,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 DEFAULT_FORMAT, DEFAULT_TEMPLATE_ENGINE, DEFAULT_POSTS_FS,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 DEFAULT_DATE_FORMAT, DEFAULT_THEME_SOURCE)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 from piecrust.cache import NullCache
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 from piecrust.configuration import (
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 Configuration, ConfigurationError, ConfigurationLoader,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 merge_dicts, visit_dict)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 from piecrust.sources.base import REALM_USER, REALM_THEME
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 logger = logging.getLogger(__name__)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 class VariantNotFoundError(Exception):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 def __init__(self, variant_path, message=None):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 super(VariantNotFoundError, self).__init__(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 message or ("No such configuration variant: %s" %
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 variant_path))
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 class PieCrustConfiguration(Configuration):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 def __init__(self, paths=None, cache=None, values=None, validate=True):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 super(PieCrustConfiguration, self).__init__(values, validate)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 self.paths = paths
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 self.cache = cache or NullCache()
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 self.fixups = []
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 def applyVariant(self, variant_path, raise_if_not_found=True):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 variant = self.get(variant_path)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 if variant is None:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 if raise_if_not_found:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 raise VariantNotFoundError(variant_path)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 return
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 if not isinstance(variant, dict):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 raise VariantNotFoundError(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 variant_path,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 "Configuration variant '%s' is not an array. "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 "Check your configuration file." % variant_path)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 self.merge(variant)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 def _load(self):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 if self.paths is None:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 self._values = self._validateAll({})
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 return
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 path_times = [os.path.getmtime(p) for p in self.paths]
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 cache_key_hash = hashlib.md5(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 ("version=%s&cache=%d" % (
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 APP_VERSION, CACHE_VERSION)).encode('utf8'))
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 for p in self.paths:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 cache_key_hash.update(("&path=%s" % p).encode('utf8'))
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 cache_key = cache_key_hash.hexdigest()
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 if self.cache.isValid('config.json', path_times):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 logger.debug("Loading configuration from cache...")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 config_text = self.cache.read('config.json')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 self._values = json.loads(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 config_text,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 object_pairs_hook=collections.OrderedDict)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 actual_cache_key = self._values.get('__cache_key')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 if actual_cache_key == cache_key:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 self._values['__cache_valid'] = True
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 return
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 logger.debug("Outdated cache key '%s' (expected '%s')." % (
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 actual_cache_key, cache_key))
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 logger.debug("Loading configuration from: %s" % self.paths)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 values = {}
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
81 try:
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
82 for i, p in enumerate(self.paths):
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
83 with open(p, 'r', encoding='utf-8') as fp:
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
84 loaded_values = yaml.load(
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
85 fp.read(),
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
86 Loader=ConfigurationLoader)
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
87 if loaded_values is None:
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
88 loaded_values = {}
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
89 for fixup in self.fixups:
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
90 fixup(i, loaded_values)
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
91 merge_dicts(values, loaded_values)
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
92
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 for fixup in self.fixups:
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
94 fixup(len(self.paths), values)
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
96 self._values = self._validateAll(values)
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
97 except Exception as ex:
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
98 raise Exception("Error loading configuration from: %s" %
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
99 ', '.join(self.paths)) from ex
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 logger.debug("Caching configuration...")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 self._values['__cache_key'] = cache_key
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 config_text = json.dumps(self._values)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 self.cache.write('config.json', config_text)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 self._values['__cache_valid'] = False
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 def _validateAll(self, values):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 if values is None:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 values = {}
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 # Add the loaded values to the default configuration.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 values = merge_dicts(copy.deepcopy(default_configuration), values)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 # Figure out if we need to generate the configuration for the
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 # default content model.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 sitec = values.setdefault('site', {})
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 if (
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 ('sources' not in sitec and
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 'routes' not in sitec and
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 'taxonomies' not in sitec) or
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
122 sitec.get('use_default_content')):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 logger.debug("Generating default content model...")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 values = self._generateDefaultContentModel(values)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 # Add a section for our cached information.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 cachec = collections.OrderedDict()
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 values['__cache'] = cachec
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 cache_writer = _ConfigCacheWriter(cachec)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 globs = globals()
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 def _visitor(path, val, parent_val, parent_key):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 callback_name = '_validate_' + path.replace('/', '_')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 callback = globs.get(callback_name)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135 if callback:
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
136 try:
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
137 val2 = callback(val, values, cache_writer)
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
138 except Exception as ex:
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
139 raise Exception("Error raised in validator '%s'." %
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
140 callback_name) from ex
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141 if val2 is None:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 raise Exception("Validator '%s' isn't returning a "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
143 "coerced value." % callback_name)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144 parent_val[parent_key] = val2
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
145
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
146 visit_dict(values, _visitor)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148 return values
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
150 def _generateDefaultContentModel(self, values):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151 dcmcopy = copy.deepcopy(default_content_model_base)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152 values = merge_dicts(dcmcopy, values)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
153
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
154 blogsc = values['site'].get('blogs')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155 if blogsc is None:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156 blogsc = ['posts']
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
157 values['site']['blogs'] = blogsc
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
158
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159 is_only_blog = (len(blogsc) == 1)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
160 for blog_name in blogsc:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
161 blog_cfg = get_default_content_model_for_blog(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
162 blog_name, is_only_blog, values)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
163 values = merge_dicts(blog_cfg, values)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
164
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
165 dcm = get_default_content_model(values)
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
166 values = merge_dicts(dcm, values)
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
167
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
168 return values
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
171 class _ConfigCacheWriter(object):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172 def __init__(self, cache_dict):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173 self._cache_dict = cache_dict
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
174
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
175 def write(self, name, val):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
176 logger.debug("Caching configuration item '%s' = %s" % (name, val))
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177 self._cache_dict[name] = val
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
179
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180 default_configuration = collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
181 'site': collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
182 'title': "Untitled PieCrust website",
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
183 'root': '/',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
184 'default_format': DEFAULT_FORMAT,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
185 'default_template_engine': DEFAULT_TEMPLATE_ENGINE,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
186 'enable_gzip': True,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187 'pretty_urls': False,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188 'trailing_slash': False,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
189 'date_format': DEFAULT_DATE_FORMAT,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
190 'auto_formats': collections.OrderedDict([
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191 ('html', ''),
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192 ('md', 'markdown'),
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193 ('textile', 'textile')]),
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194 'default_auto_format': 'md',
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
195 'default_pagination_source': None,
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196 'pagination_suffix': '/%num%',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197 'slugify_mode': 'encode',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 'themes_sources': [DEFAULT_THEME_SOURCE],
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199 'cache_time': 28800,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200 'enable_debug_info': True,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
201 'show_debug_info': False,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
202 'use_default_content': True
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203 }),
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
204 'baker': collections.OrderedDict({
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
205 'no_bake_setting': 'draft',
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
206 'workers': None,
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
207 'batch_size': None
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208 })
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209 })
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212 default_content_model_base = collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213 'site': collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214 'posts_fs': DEFAULT_POSTS_FS,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215 'date_format': DEFAULT_DATE_FORMAT,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
216 'default_page_layout': 'default',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
217 'default_post_layout': 'post',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
218 'post_url': '%year%/%month%/%day%/%slug%',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
219 'tag_url': 'tag/%tag%',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
220 'category_url': '%category%',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221 'posts_per_page': 5
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
222 })
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223 })
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
224
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 def get_default_content_model(values):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227 default_layout = values['site']['default_page_layout']
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228 return collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229 'site': collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
230 'sources': collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
231 'pages': {
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232 'type': 'default',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
233 'ignore_missing_dir': True,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
234 'data_endpoint': 'site.pages',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
235 'default_layout': default_layout,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
236 'item_name': 'page'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
237 }
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
238 }),
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
239 'routes': [
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
240 {
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
241 'url': '/%path:slug%',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
242 'source': 'pages',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
243 'func': 'pcurl(slug)'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
244 }
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
245 ],
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
246 'taxonomies': collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
247 'tags': {
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
248 'multiple': True,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
249 'term': 'tag'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
250 },
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
251 'categories': {
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
252 'term': 'category'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
253 }
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
254 })
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
255 })
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
256 })
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
257
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
258
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
259 def get_default_content_model_for_blog(blog_name, is_only_blog, values):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
260 posts_fs = values['site']['posts_fs']
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
261 blog_cfg = values.get(blog_name, {})
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
262
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
263 if is_only_blog:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
264 url_prefix = ''
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
265 tax_page_prefix = ''
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
266 fs_endpoint = 'posts'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
267 data_endpoint = 'blog'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
268 item_name = 'post'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
269 else:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
270 url_prefix = blog_name + '/'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
271 tax_page_prefix = blog_name + '/'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
272 fs_endpoint = 'posts/%s' % blog_name
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
273 data_endpoint = blog_name
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
274 item_name = '%s-post' % blog_name
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
275
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
276 items_per_page = blog_cfg.get(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
277 'posts_per_page', values['site']['posts_per_page'])
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
278 date_format = blog_cfg.get(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
279 'date_format', values['site']['date_format'])
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
280 default_layout = blog_cfg.get(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
281 'default_layout', values['site']['default_post_layout'])
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
282
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
283 post_url = '/' + blog_cfg.get(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
284 'post_url',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
285 url_prefix + values['site']['post_url']).lstrip('/')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
286 tag_url = '/' + blog_cfg.get(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
287 'tag_url',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
288 url_prefix + values['site']['tag_url']).lstrip('/')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
289 category_url = '/' + blog_cfg.get(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
290 'category_url',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
291 url_prefix + values['site']['category_url']).lstrip('/')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
292
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
293 return collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
294 'site': collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
295 'sources': collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
296 blog_name: collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
297 'type': 'posts/%s' % posts_fs,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
298 'fs_endpoint': fs_endpoint,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
299 'data_endpoint': data_endpoint,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
300 'item_name': item_name,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
301 'ignore_missing_dir': True,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
302 'data_type': 'blog',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
303 'items_per_page': items_per_page,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
304 'date_format': date_format,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
305 'default_layout': default_layout,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
306 'taxonomy_pages': collections.OrderedDict({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
307 'tags': ('pages:%s_tag.%%ext%%;'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
308 'theme_pages:_tag.%%ext%%' %
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
309 tax_page_prefix),
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
310 'categories': ('pages:%s_category.%%ext%%;'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
311 'theme_pages:_category.%%ext%%' %
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
312 tax_page_prefix)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
313 })
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
314 })
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
315 }),
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
316 'routes': [
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
317 {
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
318 'url': post_url,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
319 'source': blog_name,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
320 'func': 'pcposturl(year,month,day,slug)'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
321 },
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
322 {
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
323 'url': tag_url,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
324 'source': blog_name,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
325 'taxonomy': 'tags',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
326 'func': 'pctagurl(tag)'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
327 },
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
328 {
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
329 'url': category_url,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
330 'source': blog_name,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
331 'taxonomy': 'categories',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
332 'func': 'pccaturl(category)'
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
333 }
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
334 ]
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
335 })
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
336 })
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
337
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
338
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
339 # Configuration value validators.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
340 #
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
341 # Make sure we have basic site stuff.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
342 def _validate_site(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
343 sources = v.get('sources')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
344 if not sources:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
345 raise ConfigurationError("No sources were defined.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
346 routes = v.get('routes')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
347 if not routes:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
348 raise ConfigurationError("No routes were defined.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
349 taxonomies = v.get('taxonomies')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
350 if taxonomies is None:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
351 v['taxonomies'] = {}
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
352 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
353
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
354 # Make sure the site root starts and ends with a slash.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
355 def _validate_site_root(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
356 if not v.startswith('/'):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
357 raise ConfigurationError("The `site/root` setting must start "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
358 "with a slash.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
359 root_url = urllib.parse.quote(v.rstrip('/') + '/')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
360 return root_url
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
361
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
362
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
363 # Cache auto-format regexes, check that `.html` is in there.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
364 def _validate_site_auto_formats(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
365 if not isinstance(v, dict):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
366 raise ConfigurationError("The 'site/auto_formats' setting must be "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
367 "a dictionary.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
368
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
369 v.setdefault('html', values['site']['default_format'])
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
370 auto_formats_re = r"\.(%s)$" % (
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
371 '|'.join(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
372 [re.escape(i) for i in list(v.keys())]))
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
373 cache.write('auto_formats_re', auto_formats_re)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
374 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
375
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
376
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
377 # Check that the default auto-format is known.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
378 def _validate_site_default_auto_format(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
379 if v not in values['site']['auto_formats']:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
380 raise ConfigurationError(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
381 "Default auto-format '%s' is not declared." % v)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
382 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
383
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
384
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
385 # Cache pagination suffix regex and format.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
386 def _validate_site_pagination_suffix(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
387 if len(v) == 0 or v[0] != '/':
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
388 raise ConfigurationError("The 'site/pagination_suffix' setting "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
389 "must start with a slash.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
390 if '%num%' not in v:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
391 raise ConfigurationError("The 'site/pagination_suffix' setting "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
392 "must contain the '%num%' placeholder.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
393
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
394 pgn_suffix_fmt = v.replace('%num%', '%(num)d')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
395 cache.write('pagination_suffix_format', pgn_suffix_fmt)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
396
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
397 pgn_suffix_re = re.escape(v)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
398 pgn_suffix_re = (pgn_suffix_re.replace("\\%num\\%", "(?P<num>\\d+)") +
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
399 '$')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
400 cache.write('pagination_suffix_re', pgn_suffix_re)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
401 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
402
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
403
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
404 # Make sure theme sources is a list.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
405 def _validate_site_theme_sources(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
406 if not isinstance(v, list):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
407 v = [v]
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
408 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
409
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
410
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
411 def _validate_site_sources(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
412 # Basic checks.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
413 if not v:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
414 raise ConfigurationError("There are no sources defined.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
415 if not isinstance(v, dict):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
416 raise ConfigurationError("The 'site/sources' setting must be a "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
417 "dictionary.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
418
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
419 # Add the theme page source if no sources were defined in the theme
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
420 # configuration itself.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
421 has_any_theme_source = False
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
422 for sn, sc in v.items():
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
423 if sc.get('realm') == REALM_THEME:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
424 has_any_theme_source = True
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
425 break
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
426 if not has_any_theme_source:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
427 v['theme_pages'] = {
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
428 'theme_source': True,
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
429 'fs_endpoint': 'pages',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
430 'data_endpoint': 'site/pages',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
431 'item_name': 'page',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
432 'realm': REALM_THEME}
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
433 values['site']['routes'].append({
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
434 'url': '/%path:slug%',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
435 'source': 'theme_pages',
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
436 'func': 'pcurl(slug)'})
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
437
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
438 # Sources have the `default` scanner by default, duh. Also, a bunch
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
439 # of other default values for other configuration stuff.
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
440 reserved_endpoints = set(['piecrust', 'site', 'page', 'route',
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
441 'assets', 'pagination', 'siblings',
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
442 'family'])
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
443 for sn, sc in v.items():
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
444 if not isinstance(sc, dict):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
445 raise ConfigurationError("All sources in 'site/sources' must "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
446 "be dictionaries.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
447 sc.setdefault('type', 'default')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
448 sc.setdefault('fs_endpoint', sn)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
449 sc.setdefault('ignore_missing_dir', False)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
450 sc.setdefault('data_endpoint', sn)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
451 sc.setdefault('data_type', 'iterator')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
452 sc.setdefault('item_name', sn)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
453 sc.setdefault('items_per_page', 5)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
454 sc.setdefault('date_format', DEFAULT_DATE_FORMAT)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
455 sc.setdefault('realm', REALM_USER)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
456
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
457 # Validate endpoints.
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
458 endpoint = sc['data_endpoint']
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
459 if endpoint in reserved_endpoints:
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
460 raise ConfigurationError(
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
461 "Source '%s' is using a reserved endpoint name: %s" %
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
462 (sn, endpoint))
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
463
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
464 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
465
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
466
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
467 def _validate_site_routes(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
468 if not v:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
469 raise ConfigurationError("There are no routes defined.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
470 if not isinstance(v, list):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
471 raise ConfigurationError("The 'site/routes' setting must be a "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
472 "list.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
473
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
474 # Check routes are referencing correct sources, have default
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
475 # values, etc.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
476 for rc in v:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
477 if not isinstance(rc, dict):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
478 raise ConfigurationError("All routes in 'site/routes' must be "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
479 "dictionaries.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
480 rc_url = rc.get('url')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
481 if not rc_url:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
482 raise ConfigurationError("All routes in 'site/routes' must "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
483 "have an 'url'.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
484 if rc_url[0] != '/':
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
485 raise ConfigurationError("Route URLs must start with '/'.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
486 if rc.get('source') is None:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
487 raise ConfigurationError("Routes must specify a source.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
488 if rc['source'] not in list(values['site']['sources'].keys()):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
489 raise ConfigurationError("Route is referencing unknown "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
490 "source: %s" % rc['source'])
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
491 rc.setdefault('taxonomy', None)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
492 rc.setdefault('page_suffix', '/%num%')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
493
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
494 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
495
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
496
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
497 def _validate_site_taxonomies(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
498 for tn, tc in v.items():
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
499 tc.setdefault('multiple', False)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
500 tc.setdefault('term', tn)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
501 tc.setdefault('page', '_%s.%%ext%%' % tc['term'])
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
502
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
503 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
504
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
505
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
506 def _validate_site_plugins(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
507 if isinstance(v, str):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
508 v = v.split(',')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
509 elif not isinstance(v, list):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
510 raise ConfigurationError(
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
511 "The 'site/plugins' setting must be an array, or a "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
512 "comma-separated list.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
513 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
514