Mercurial > piecrust2
annotate piecrust/data/base.py @ 763:f6a13dba38d6
publish: Fix stupid typo.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Thu, 30 Jun 2016 22:39:33 -0700 |
| parents | d446029c9478 |
| children | 1d5f02778723 |
| rev | line source |
|---|---|
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
1 import collections.abc |
|
381
4c9eab0e283b
data: Fix problems with using non-existing metadata on a linked page.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
2 |
|
4c9eab0e283b
data: Fix problems with using non-existing metadata on a linked page.
Ludovic Chabant <ludovic@chabant.com>
parents:
369
diff
changeset
|
3 |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
4 class MergedMapping(collections.abc.Mapping): |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
5 """ Provides a dictionary-like object that's really the aggregation of |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
6 multiple dictionary-like objects. |
|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 """ |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
8 def __init__(self, dicts, path=''): |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
9 self._dicts = dicts |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
10 self._path = path |
|
226
e9dc18a275ff
data: Add ability for `IPaginationSource`s to specify how to get settings.
Ludovic Chabant <ludovic@chabant.com>
parents:
158
diff
changeset
|
11 |
|
12
30a42341cfa8
Define page slugs properly, avoid recursions with debug data.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
12 def __getattr__(self, name): |
|
30a42341cfa8
Define page slugs properly, avoid recursions with debug data.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
13 try: |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
14 return self[name] |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
15 except KeyError: |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
16 raise AttributeError("No such attribute: %s" % self._subp(name)) |
|
12
30a42341cfa8
Define page slugs properly, avoid recursions with debug data.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
17 |
|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 def __getitem__(self, name): |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
19 values = [] |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
20 for d in self._dicts: |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
21 try: |
|
713
d446029c9478
data: Support both objects and dictionaries in `MergedMapping`.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
22 val = getattr(d, name) |
|
d446029c9478
data: Support both objects and dictionaries in `MergedMapping`.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
23 values.append(val) |
|
d446029c9478
data: Support both objects and dictionaries in `MergedMapping`.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
24 continue |
|
d446029c9478
data: Support both objects and dictionaries in `MergedMapping`.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
25 except AttributeError: |
|
d446029c9478
data: Support both objects and dictionaries in `MergedMapping`.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
26 pass |
|
d446029c9478
data: Support both objects and dictionaries in `MergedMapping`.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
27 |
|
d446029c9478
data: Support both objects and dictionaries in `MergedMapping`.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
28 try: |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
29 val = d[name] |
|
713
d446029c9478
data: Support both objects and dictionaries in `MergedMapping`.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
30 values.append(val) |
|
d446029c9478
data: Support both objects and dictionaries in `MergedMapping`.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
31 continue |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
32 except KeyError: |
|
713
d446029c9478
data: Support both objects and dictionaries in `MergedMapping`.
Ludovic Chabant <ludovic@chabant.com>
parents:
440
diff
changeset
|
33 pass |
|
236
eaf18442bff8
internal: Add support for "wildcard" loader in `LazyPageConfigData`.
Ludovic Chabant <ludovic@chabant.com>
parents:
228
diff
changeset
|
34 |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
35 if len(values) == 0: |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
36 raise KeyError("No such item: %s" % self._subp(name)) |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
37 if len(values) == 1: |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
38 return values[0] |
|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
40 for val in values: |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
41 if not isinstance(val, (dict, collections.abc.Mapping)): |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
42 raise Exception( |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
43 "Template data for '%s' contains an incompatible mix " |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
44 "of data: %s" % ( |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
45 self._subp(name), |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
46 ', '.join([str(type(v)) for v in values]))) |
|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
48 return MergedMapping(values, self._subp(name)) |
|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
50 def __iter__(self): |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
51 keys = set() |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
52 for d in self._dicts: |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
53 keys |= set(d.keys()) |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
54 return iter(keys) |
|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
56 def __len__(self): |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
57 keys = set() |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
58 for d in self._dicts: |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
59 keys |= set(d.keys()) |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
60 return len(keys) |
|
12
30a42341cfa8
Define page slugs properly, avoid recursions with debug data.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
61 |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
62 def _subp(self, name): |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
63 return '%s/%s' % (self._path, name) |
|
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
64 |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
65 def _prependMapping(self, d): |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
66 self._dicts.insert(0, d) |
|
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
67 |
|
440
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
68 def _appendMapping(self, d): |
|
32c7c2d219d2
performance: Refactor how data is managed to reduce copying.
Ludovic Chabant <ludovic@chabant.com>
parents:
433
diff
changeset
|
69 self._dicts.append(d) |
|
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
70 |
