comparison piecrust/appconfigdefaults.py @ 805:fd694f1297c7

config: Cleanup config loading code. Add support for a `local.yml` config.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 10 Oct 2016 21:41:59 -0700
parents
children 4850f8c21b6e
comparison
equal deleted inserted replaced
804:08e6484a2600 805:fd694f1297c7
1 import collections
2 from piecrust import (
3 DEFAULT_FORMAT, DEFAULT_TEMPLATE_ENGINE, DEFAULT_POSTS_FS,
4 DEFAULT_DATE_FORMAT, DEFAULT_THEME_SOURCE)
5 from piecrust.configuration import (
6 get_dict_values, try_get_dict_values)
7 from piecrust.sources.base import REALM_THEME
8
9
10 default_configuration = collections.OrderedDict({
11 'site': collections.OrderedDict({
12 'title': "Untitled PieCrust website",
13 'root': '/',
14 'default_format': DEFAULT_FORMAT,
15 'default_template_engine': DEFAULT_TEMPLATE_ENGINE,
16 'enable_gzip': True,
17 'pretty_urls': False,
18 'trailing_slash': False,
19 'date_format': DEFAULT_DATE_FORMAT,
20 'auto_formats': collections.OrderedDict([
21 ('html', ''),
22 ('md', 'markdown'),
23 ('textile', 'textile')]),
24 'default_auto_format': 'md',
25 'default_pagination_source': None,
26 'pagination_suffix': '/%num%',
27 'slugify_mode': 'encode',
28 'themes_sources': [DEFAULT_THEME_SOURCE],
29 'cache_time': 28800,
30 'enable_debug_info': True,
31 'show_debug_info': False,
32 'use_default_content': True,
33 'use_default_theme_content': True,
34 'theme_site': False
35 }),
36 'baker': collections.OrderedDict({
37 'no_bake_setting': 'draft',
38 'workers': None,
39 'batch_size': None
40 })
41 })
42
43
44 default_theme_content_model_base = collections.OrderedDict({
45 'site': collections.OrderedDict({
46 'sources': collections.OrderedDict({
47 'theme_pages': {
48 'type': 'default',
49 'ignore_missing_dir': True,
50 'fs_endpoint': 'pages',
51 'data_endpoint': 'site.pages',
52 'default_layout': 'default',
53 'item_name': 'page',
54 'realm': REALM_THEME
55 }
56 }),
57 'routes': [
58 {
59 'url': '/%slug%',
60 'source': 'theme_pages',
61 'func': 'pcurl'
62 }
63 ],
64 'theme_tag_page': 'theme_pages:_tag.%ext%',
65 'theme_category_page': 'theme_pages:_category.%ext%',
66 'theme_month_page': 'theme_pages:_month.%ext%',
67 'theme_year_page': 'theme_pages:_year.%ext%'
68 })
69 })
70
71
72 default_content_model_base = collections.OrderedDict({
73 'site': collections.OrderedDict({
74 'posts_fs': DEFAULT_POSTS_FS,
75 'default_page_layout': 'default',
76 'default_post_layout': 'post',
77 'post_url': '/%year%/%month%/%day%/%slug%',
78 'year_url': '/archives/%year%',
79 'tag_url': '/tag/%tag%',
80 'category_url': '/%category%',
81 'posts_per_page': 5
82 })
83 })
84
85
86 def get_default_content_model(site_values, values):
87 default_layout = get_dict_values(
88 (site_values, 'site/default_page_layout'),
89 (values, 'site/default_page_layout'))
90 return collections.OrderedDict({
91 'site': collections.OrderedDict({
92 'sources': collections.OrderedDict({
93 'pages': {
94 'type': 'default',
95 'ignore_missing_dir': True,
96 'data_endpoint': 'site.pages',
97 'default_layout': default_layout,
98 'item_name': 'page'
99 }
100 }),
101 'routes': [
102 {
103 'url': '/%slug%',
104 'source': 'pages',
105 'func': 'pcurl'
106 }
107 ],
108 'taxonomies': collections.OrderedDict([
109 ('tags', {
110 'multiple': True,
111 'term': 'tag'
112 }),
113 ('categories', {
114 'term': 'category',
115 'func_name': 'pccaturl'
116 })
117 ])
118 })
119 })
120
121
122 def get_default_content_model_for_blog(blog_name, is_only_blog,
123 site_values, values,
124 theme_site=False):
125 # Get the global (default) values for various things we're interested in.
126 defs = {}
127 names = ['posts_fs', 'posts_per_page', 'date_format',
128 'default_post_layout', 'post_url', 'year_url']
129 for n in names:
130 defs[n] = get_dict_values(
131 (site_values, 'site/%s' % n),
132 (values, 'site/%s' % n))
133
134 # More stuff we need.
135 if is_only_blog:
136 url_prefix = ''
137 page_prefix = ''
138 fs_endpoint = 'posts'
139 data_endpoint = 'blog'
140 item_name = 'post'
141 tpl_func_prefix = 'pc'
142
143 if theme_site:
144 # If this is a theme site, show posts from a `sample` directory
145 # so it's clearer that those won't show up when the theme is
146 # actually applied to a normal site.
147 fs_endpoint = 'sample/posts'
148 else:
149 url_prefix = blog_name + '/'
150 page_prefix = blog_name + '/'
151 data_endpoint = blog_name
152 fs_endpoint = 'posts/%s' % blog_name
153 item_name = try_get_dict_values(
154 (site_values, '%s/item_name' % blog_name),
155 (values, '%s/item_name' % blog_name),
156 default=('%spost' % blog_name))
157 tpl_func_prefix = try_get_dict_values(
158 (site_values, '%s/func_prefix' % blog_name),
159 (values, '%s/func_prefix' % blog_name),
160 default=('pc%s' % blog_name))
161
162 # Figure out the settings values for this blog, specifically.
163 # The value could be set on the blog config itself, globally, or left at
164 # its default. We already handle the "globally vs. default" with the
165 # `defs` map that we computed above.
166 blog_cfg = values.get(blog_name, {})
167 blog_values = {}
168 for n in names:
169 blog_values[n] = blog_cfg.get(n, defs[n])
170
171 posts_fs = blog_values['posts_fs']
172 posts_per_page = blog_values['posts_per_page']
173 date_format = blog_values['date_format']
174 default_layout = blog_values['default_post_layout']
175 post_url = '/' + url_prefix + blog_values['post_url'].lstrip('/')
176 year_url = '/' + url_prefix + blog_values['year_url'].lstrip('/')
177
178 year_archive = 'pages:%s_year.%%ext%%' % page_prefix
179 if not theme_site:
180 theme_year_page = try_get_dict_values(
181 (site_values, 'site/theme_year_page'),
182 (values, 'site/theme_year_page'))
183 if theme_year_page:
184 year_archive += ';' + theme_year_page
185
186 cfg = collections.OrderedDict({
187 'site': collections.OrderedDict({
188 'sources': collections.OrderedDict({
189 blog_name: collections.OrderedDict({
190 'type': 'posts/%s' % posts_fs,
191 'fs_endpoint': fs_endpoint,
192 'data_endpoint': data_endpoint,
193 'item_name': item_name,
194 'ignore_missing_dir': True,
195 'data_type': 'blog',
196 'items_per_page': posts_per_page,
197 'date_format': date_format,
198 'default_layout': default_layout
199 })
200 }),
201 'generators': collections.OrderedDict({
202 ('%s_archives' % blog_name): collections.OrderedDict({
203 'type': 'blog_archives',
204 'source': blog_name,
205 'page': year_archive
206 })
207 }),
208 'routes': [
209 {
210 'url': post_url,
211 'source': blog_name,
212 'func': ('%sposturl' % tpl_func_prefix)
213 },
214 {
215 'url': year_url,
216 'generator': ('%s_archives' % blog_name),
217 'func': ('%syearurl' % tpl_func_prefix)
218 }
219 ]
220 })
221 })
222
223 # Add a generator and a route for each taxonomy.
224 taxonomies_cfg = try_get_dict_values(
225 (site_values, 'site/taxonomies'),
226 (values, 'site/taxonomies'),
227 default={}).copy()
228 for tax_name, tax_cfg in taxonomies_cfg.items():
229 term = tax_cfg.get('term', tax_name)
230
231 # Generator.
232 page_ref = 'pages:%s_%s.%%ext%%' % (page_prefix, term)
233 if not theme_site:
234 theme_page_ref = try_get_dict_values(
235 (site_values, 'site/theme_%s_page' % term),
236 (values, 'site/theme_%s_page' % term))
237 if theme_page_ref:
238 page_ref += ';' + theme_page_ref
239 tax_gen_name = '%s_%s' % (blog_name, tax_name)
240 tax_gen = collections.OrderedDict({
241 'type': 'taxonomy',
242 'source': blog_name,
243 'taxonomy': tax_name,
244 'page': page_ref
245 })
246 cfg['site']['generators'][tax_gen_name] = tax_gen
247
248 # Route.
249 tax_url_cfg_name = '%s_url' % term
250 tax_url = try_get_dict_values(
251 (blog_cfg, tax_url_cfg_name),
252 (site_values, 'site/%s' % tax_url_cfg_name),
253 (values, 'site/%s' % tax_url_cfg_name),
254 default=('%s/%%%s%%' % (term, term)))
255 tax_url = '/' + url_prefix + tax_url.lstrip('/')
256 tax_func_name = try_get_dict_values(
257 (site_values, 'site/taxonomies/%s/func_name' % tax_name),
258 (values, 'site/taxonomies/%s/func_name' % tax_name),
259 default=('%s%surl' % (tpl_func_prefix, term)))
260 tax_route = collections.OrderedDict({
261 'url': tax_url,
262 'generator': tax_gen_name,
263 'taxonomy': tax_name,
264 'func': tax_func_name
265 })
266 cfg['site']['routes'].append(tax_route)
267
268 return cfg
269