Mercurial > piecrust2
comparison piecrust/data/builder.py @ 3:f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
- Serving works, with debug window.
- Baking works, multi-threading, with dependency handling.
- Various things not implemented yet.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 10 Aug 2014 23:43:16 -0700 |
parents | |
children | 474c9882decf |
comparison
equal
deleted
inserted
replaced
2:40fa08b261b9 | 3:f485ba500df3 |
---|---|
1 import time | |
2 import logging | |
3 from piecrust.configuration import merge_dicts | |
4 from piecrust.data.assetor import Assetor | |
5 from piecrust.data.debug import build_debug_info | |
6 from piecrust.data.linker import Linker | |
7 from piecrust.data.paginator import Paginator | |
8 | |
9 | |
10 logger = logging.getLogger(__name__) | |
11 | |
12 | |
13 class DataBuildingContext(object): | |
14 def __init__(self, page, uri, page_num=1): | |
15 self.page = page | |
16 self.uri = uri | |
17 self.page_num = page_num | |
18 self.pagination_source = None | |
19 self.pagination_filter = None | |
20 | |
21 | |
22 def build_page_data(ctx): | |
23 page = ctx.page | |
24 app = page.app | |
25 | |
26 pgn_source = ctx.pagination_source or get_default_pagination_source(page) | |
27 paginator = Paginator(page, pgn_source, ctx.uri, ctx.page_num, | |
28 ctx.pagination_filter) | |
29 assetor = Assetor(page, ctx.uri) | |
30 linker = Linker(page) | |
31 data = { | |
32 'piecrust': build_piecrust_data(), | |
33 'page': dict(page.config.get()), | |
34 'assets': assetor, | |
35 'pagination': paginator, | |
36 'siblings': linker, | |
37 'family': linker | |
38 } | |
39 page_data = data['page'] | |
40 page_data['url'] = ctx.uri | |
41 page_data['timestamp'] = time.mktime(page.datetime.timetuple()) | |
42 date_format = app.config.get('site/date_format') | |
43 if date_format: | |
44 page_data['date'] = page.datetime.strftime(date_format) | |
45 | |
46 #TODO: handle slugified taxonomy terms. | |
47 | |
48 site_data = build_site_data(page) | |
49 merge_dicts(data, site_data) | |
50 | |
51 # Do this at the end because we want all the data to be ready to be | |
52 # displayed in the debugger window. | |
53 if (app.debug and app.config.get('site/enable_debug_info') and | |
54 not app.config.get('baker/is_baking')): | |
55 data['piecrust']['debug_info'] = build_debug_info(page, data) | |
56 | |
57 return data | |
58 | |
59 | |
60 def build_layout_data(page, page_data, contents): | |
61 data = dict(page_data) | |
62 for name, txt in contents.iteritems(): | |
63 if name in data: | |
64 logger.warning("Content segment '%s' will hide existing data." % | |
65 name) | |
66 data[name] = txt | |
67 return data | |
68 | |
69 | |
70 try: | |
71 from piecrust.__version__ import VERSION | |
72 except ImportError: | |
73 from piecrust import APP_VERSION as VERSION | |
74 | |
75 | |
76 def build_piecrust_data(): | |
77 data = { | |
78 'version': VERSION, | |
79 'url': 'http://bolt80.com/piecrust/', | |
80 'branding': 'Baked with <em><a href="%s">PieCrust</a> %s</em>.' % ( | |
81 'http://bolt80.com/piecrust/', VERSION) | |
82 } | |
83 return data | |
84 | |
85 | |
86 def build_site_data(page): | |
87 app = page.app | |
88 data = dict(app.config.get()) | |
89 for source in app.sources: | |
90 endpoint_bits = source.data_endpoint.split('/') | |
91 endpoint = data | |
92 for e in endpoint_bits[:-1]: | |
93 if e not in endpoint: | |
94 endpoint[e] = {} | |
95 endpoint = endpoint[e] | |
96 user_data = endpoint.get(endpoint_bits[-1]) | |
97 provider = source.buildDataProvider(page, user_data) | |
98 if endpoint_bits[-1] in endpoint: | |
99 provider.user_data = endpoint[endpoint_bits[-1]] | |
100 endpoint[endpoint_bits[-1]] = provider | |
101 return data | |
102 | |
103 | |
104 def get_default_pagination_source(page): | |
105 app = page.app | |
106 source_name = page.config.get('source') or page.config.get('blog') | |
107 logger.debug("Got source name %s for page %s" % (source_name, page.path)) | |
108 if source_name is None: | |
109 blog_names = app.config.get('site/blogs') | |
110 if blog_names is not None: | |
111 source_name = blog_names[0] | |
112 else: | |
113 source_name = app.sources[0].name | |
114 source = app.getSource(source_name) | |
115 return source | |
116 |