Mercurial > piecrust2
comparison piecrust/baking/records.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 logging | |
2 from piecrust.records import Record | |
3 | |
4 | |
5 logger = logging.getLogger(__name__) | |
6 | |
7 | |
8 def _get_transition_key(source_name, rel_path, taxonomy_name=None, | |
9 taxonomy_term=None): | |
10 key = '%s:%s' % (source_name, rel_path) | |
11 if taxonomy_name and taxonomy_term: | |
12 key += ';%s:' % taxonomy_name | |
13 if isinstance(taxonomy_term, tuple): | |
14 key += '/'.join(taxonomy_term) | |
15 else: | |
16 key += taxonomy_term | |
17 return key | |
18 | |
19 | |
20 class BakeRecord(Record): | |
21 VERSION = 1 | |
22 | |
23 def __init__(self): | |
24 super(BakeRecord, self).__init__() | |
25 self.out_dir = None | |
26 self.bake_time = None | |
27 | |
28 | |
29 class BakeRecordPageEntry(object): | |
30 def __init__(self, page): | |
31 self.path = page.path | |
32 self.rel_path = page.rel_path | |
33 self.source_name = page.source.name | |
34 self.config = page.config.get() | |
35 self.taxonomy_name = None | |
36 self.taxonomy_term = None | |
37 self.out_uris = [] | |
38 self.out_paths = [] | |
39 self.used_source_names = set() | |
40 self.used_taxonomy_terms = set() | |
41 | |
42 @property | |
43 def was_baked(self): | |
44 return len(self.out_paths) > 0 | |
45 | |
46 @property | |
47 def num_subs(self): | |
48 return len(self.out_paths) | |
49 | |
50 @property | |
51 def transition_key(self): | |
52 return _get_transition_key(self.source_name, self.rel_path, | |
53 self.taxonomy_name, self.taxonomy_term) | |
54 | |
55 class TransitionalBakeRecord(object): | |
56 DELETION_MISSING = 1 | |
57 DELETION_CHANGED = 2 | |
58 | |
59 def __init__(self, previous_path=None): | |
60 self.previous = BakeRecord() | |
61 self.current = BakeRecord() | |
62 self.transitions = {} | |
63 if previous_path: | |
64 self.loadPrevious(previous_path) | |
65 self.current.entry_added += self._onCurrentEntryAdded | |
66 | |
67 def loadPrevious(self, previous_path): | |
68 self.previous = BakeRecord.load(previous_path) | |
69 for e in self.previous.entries: | |
70 self.transitions[e.transition_key] = (e, None) | |
71 | |
72 def saveCurrent(self, current_path): | |
73 self.current.save(current_path) | |
74 | |
75 def addEntry(self, entry): | |
76 self.current.addEntry(entry) | |
77 | |
78 def getPreviousEntry(self, page, taxonomy_name=None, taxonomy_term=None): | |
79 key = _get_transition_key(page.source.name, page.rel_path, | |
80 taxonomy_name, taxonomy_term) | |
81 pair = self.transitions.get(key) | |
82 if pair is not None: | |
83 return pair[0] | |
84 return None | |
85 | |
86 def collapseRecords(self): | |
87 for pair in self.transitions.itervalues(): | |
88 prev = pair[0] | |
89 cur = pair[1] | |
90 | |
91 if prev and cur and not cur.was_baked: | |
92 # This page wasn't baked, so the information from last | |
93 # time is still valid (we didn't get any information | |
94 # since we didn't bake). | |
95 cur.out_uris = list(prev.out_uris) | |
96 cur.out_paths = list(prev.out_paths) | |
97 cur.used_source_names = set(prev.used_source_names) | |
98 cur.used_taxonomy_terms = set(prev.used_taxonomy_terms) | |
99 | |
100 def _onCurrentEntryAdded(self, entry): | |
101 key = entry.transition_key | |
102 te = self.transitions.get(key) | |
103 if te is None: | |
104 logger.debug("Adding new record entry: %s" % key) | |
105 self.transitions[key] = (None, entry) | |
106 return | |
107 | |
108 if te[1] is not None: | |
109 raise Exception("A current entry already exists for: %s" % | |
110 key) | |
111 logger.debug("Setting current record entry: %s" % key) | |
112 self.transitions[key] = (te[0], entry) | |
113 |