annotate piecrust/data/linker.py @ 227:d6d0e4976beb

data: Make the `Linekr` use the new `getSettingAccessor` API.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 04 Feb 2015 23:50:24 -0800
parents 701591ebfcba
children 879fe1457e48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
172
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
1 import logging
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
2 import collections
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
3 from piecrust.data.base import PaginationData, IPaginationSource
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
4 from piecrust.data.iterators import PageIterator
172
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
5 from piecrust.sources.base import IListableSource, build_pages
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
6
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
7
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
8 logger = logging.getLogger(__name__)
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
9
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
10
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
11 class LinkedPageData(PaginationData):
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
12 debug_render = ['name', 'is_dir', 'is_self']
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
13
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
14 def __init__(self, name, page, is_self=False):
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
15 super(LinkedPageData, self).__init__(page)
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
16 self.name = name
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
17 self.is_self = is_self
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
18
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
19 @property
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
20 def is_dir(self):
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
21 return False
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
227
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
24 class LinkerSource(IPaginationSource):
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
25 def __init__(self, pages):
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
26 self._pages = pages
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
27
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
28 def getItemsPerPage(self):
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
29 raise NotImplementedError()
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
30
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
31 def getSourceIterator(self):
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
32 return self._pages
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
33
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
34 def getSorterIterator(self, it):
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
35 return None
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
36
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
37 def getTailIterator(self, it):
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
38 return None
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
39
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
40 def getPaginationFilter(self, page):
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
41 return None
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
42
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
43 def getSettingAccessor(self):
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
44 return lambda i, n: i.get(n)
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
45
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
46
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
47 class LinkedPageDataIterator(object):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
48 def __init__(self, items):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
49 self._items = list(items)
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
50 self._index = -1
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
51
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
52 def __iter__(self):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
53 return self
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
54
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
55 def __next__(self):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
56 self._index += 1
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
57 if self._index >= len(self._items):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
58 raise StopIteration()
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
59 return self._items[self._index]
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
60
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
61 def sort(self, name):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
62 def key_getter(item):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
63 return item[name]
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
64 self._items = sorted(self._item, key=key_getter)
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
65 return self
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
66
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
67
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 class Linker(object):
172
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
69 debug_render_doc = """Provides access to sibling and children pages."""
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
70
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
71 def __init__(self, source, *, name=None, dir_path=None, page_path=None):
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
72 self.source = source
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
73 self._name = name
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
74 self._dir_path = dir_path
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
75 self._root_page_path = page_path
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
76 self._cache = None
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
77 self._is_listable = None
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
78
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
79 def __iter__(self):
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
80 self._load()
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
81 return LinkedPageDataIterator(self._cache.values())
172
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
82
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
83 def __getattr__(self, name):
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
84 self._load()
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
85 try:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
86 return self._cache[name]
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
87 except KeyError:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
88 raise AttributeError()
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
89
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
90 @property
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
91 def name(self):
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
92 if not self._name:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
93 self._load()
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
94 return self._name
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
95
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
96 @property
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
97 def is_dir(self):
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
98 return True
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
99
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
100 @property
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
101 def is_self(self):
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
102 return False
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
103
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
104 def _load(self):
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
105 if self._cache is not None:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
106 return
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107
172
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
108 self._is_listable = isinstance(self.source, IListableSource)
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
109 if self._is_listable and self._root_page_path is not None:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
110 if self._name is None:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
111 self._name = self.source.getBasename(self._root_page_path)
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
112 if self._dir_path is None:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
113 self._dir_path = self.source.getDirpath(self._root_page_path)
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
114
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
115 self._cache = collections.OrderedDict()
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
116 if not self._is_listable or self._dir_path is None:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
117 return
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
118
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
119 items = self.source.listPath(self._dir_path)
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
120 with self.source.app.env.page_repository.startBatchGet():
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
121 for is_dir, name, data in items:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
122 if is_dir:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
123 self._cache[name] = Linker(self.source,
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
124 name=name, dir_path=data)
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
125 else:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
126 page = data.buildPage()
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
127 is_root_page = (self._root_page_path == data.rel_path)
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
128 self._cache[name] = LinkedPageData(name, page,
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
129 is_root_page)
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
130
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
131
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
132 class RecursiveLinker(Linker):
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
133 def __init__(self, source, *args, **kwargs):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
134 super(RecursiveLinker, self).__init__(source, *args, **kwargs)
172
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
135
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
136 def __iter__(self):
227
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
137 return iter(self.pages)
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
138
227
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
139 def __getattr__(self, name):
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
140 if name == 'pages':
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
141 return self.getpages()
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
142 if name == 'siblings':
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
143 return self.getsiblings()
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
144 raise AttributeError()
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
145
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
146 def getpages(self):
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
147 src = LinkerSource(self._iterateLinkers())
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
148 return PageIterator(src)
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
149
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
150 def getsiblings(self):
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
151 src = LinkerSource(self._iterateLinkers(0))
d6d0e4976beb data: Make the `Linekr` use the new `getSettingAccessor` API.
Ludovic Chabant <ludovic@chabant.com>
parents: 212
diff changeset
152 return PageIterator(src)
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
153
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
154 def frompath(self, rel_path):
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
155 return RecursiveLinker(self.source, name='.', dir_path=rel_path)
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
156
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
157 def _iterateLinkers(self, max_depth=-1):
172
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
158 self._load()
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
159 if not self._is_listable:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
160 return
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
161 yield from walk_linkers(self, 0, max_depth)
172
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
162
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
163
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
164 def walk_linkers(linker, depth=0, max_depth=-1):
172
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
165 linker._load()
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
166 for item in linker._cache.values():
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
167 if item.is_dir:
212
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
168 if max_depth < 0 or depth + 1 <= max_depth:
701591ebfcba data: Improve the Linker and RecursiveLinker features. Add tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 172
diff changeset
169 yield from walk_linkers(item, depth + 1, max_depth)
172
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
170 else:
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
171 yield item
4fc1d306046b linker: Actually implement the `Linker` class, and use it in the page data.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
172