urwidtrees 1.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- urwidtrees/__init__.py +8 -0
- urwidtrees/decoration.py +509 -0
- urwidtrees/lru_cache.py +141 -0
- urwidtrees/nested.py +367 -0
- urwidtrees/tree.py +253 -0
- urwidtrees/version.py +1 -0
- urwidtrees/widgets.py +249 -0
- urwidtrees-1.0.3.dist-info/METADATA +20 -0
- urwidtrees-1.0.3.dist-info/RECORD +12 -0
- urwidtrees-1.0.3.dist-info/WHEEL +5 -0
- urwidtrees-1.0.3.dist-info/licenses/LICENSE.md +674 -0
- urwidtrees-1.0.3.dist-info/top_level.txt +1 -0
urwidtrees/nested.py
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
# Copyright (C) 2013 Patrick Totzke <patricktotzke@gmail.com>
|
|
2
|
+
# This file is released under the GNU GPL, version 3 or a later revision.
|
|
3
|
+
from .tree import Tree
|
|
4
|
+
from .decoration import DecoratedTree, CollapseMixin
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class NestedTree(Tree):
|
|
8
|
+
"""
|
|
9
|
+
A Tree that wraps around Trees that may contain list walkers or other
|
|
10
|
+
trees. The wrapped tree may contain normal widgets as well. List walkers
|
|
11
|
+
and subtree contents will be expanded into the tree presented by this
|
|
12
|
+
wrapper.
|
|
13
|
+
|
|
14
|
+
This wrapper's positions are tuples of positions of the original and
|
|
15
|
+
subtrees: For example, `(X,Y,Z)` points at position Z in tree/list at
|
|
16
|
+
position Y in tree/list at position X in the original tree.
|
|
17
|
+
|
|
18
|
+
NestedTree transparently behaves like a collapsible DecoratedTree.
|
|
19
|
+
"""
|
|
20
|
+
@property
|
|
21
|
+
def root(self):
|
|
22
|
+
root = (self._tree.root,)
|
|
23
|
+
rcontent = self._tree[self._tree.root]
|
|
24
|
+
if isinstance(rcontent, Tree):
|
|
25
|
+
root = root + (rcontent.root,)
|
|
26
|
+
return root
|
|
27
|
+
|
|
28
|
+
def _sanitize_position(self, pos, tree=None):
|
|
29
|
+
"""
|
|
30
|
+
Ensure a position tuple until the result does not
|
|
31
|
+
point to a :class:`Tree` any more.
|
|
32
|
+
"""
|
|
33
|
+
if pos is not None:
|
|
34
|
+
tree = tree or self._tree
|
|
35
|
+
entry = self._lookup_entry(tree, pos)
|
|
36
|
+
if isinstance(entry, Tree):
|
|
37
|
+
pos = pos + self._sanitize_position((entry.root,), tree=entry)
|
|
38
|
+
return pos
|
|
39
|
+
|
|
40
|
+
def __init__(self, tree, interpret_covered=False):
|
|
41
|
+
self._tree = tree
|
|
42
|
+
self._interpret_covered = interpret_covered
|
|
43
|
+
|
|
44
|
+
def _lookup_entry(self, tree, pos):
|
|
45
|
+
if len(pos) == 0:
|
|
46
|
+
entry = tree[tree.root]
|
|
47
|
+
else:
|
|
48
|
+
entry = tree[pos[0]]
|
|
49
|
+
if len(pos) > 1 and isinstance(entry, Tree):
|
|
50
|
+
subtree = entry
|
|
51
|
+
entry = self._lookup_entry(subtree, pos[1:])
|
|
52
|
+
return entry
|
|
53
|
+
|
|
54
|
+
def _depth(self, tree, pos, outmost_only=True):
|
|
55
|
+
depth = self._tree.depth(pos[1:])
|
|
56
|
+
if not outmost_only:
|
|
57
|
+
entry = self._tree[pos[0]]
|
|
58
|
+
if isinstance(entry, Tree) and len(pos) > 1:
|
|
59
|
+
depth += self._depth(entry, pos[1:], outmost_only=False)
|
|
60
|
+
return depth
|
|
61
|
+
|
|
62
|
+
def depth(self, pos, outmost=True):
|
|
63
|
+
return self._depth(self._tree, pos)
|
|
64
|
+
|
|
65
|
+
def __getitem__(self, pos):
|
|
66
|
+
return self._lookup_entry(self._tree, pos)
|
|
67
|
+
|
|
68
|
+
# DecoratedTree API
|
|
69
|
+
def _get_decorated_entry(self, tree, pos, widget=None, is_first=True):
|
|
70
|
+
entry = tree[pos[0]]
|
|
71
|
+
if entry is None:
|
|
72
|
+
return None
|
|
73
|
+
if len(pos) > 1 and isinstance(entry, Tree):
|
|
74
|
+
subtree = entry
|
|
75
|
+
entry = self._get_decorated_entry(
|
|
76
|
+
subtree, pos[1:], widget, is_first)
|
|
77
|
+
else:
|
|
78
|
+
entry = widget or entry
|
|
79
|
+
if isinstance(tree, (DecoratedTree, NestedTree)): # has decorate-API
|
|
80
|
+
isf = len(pos) < 2
|
|
81
|
+
if not isf and isinstance(tree[pos[0]], Tree):
|
|
82
|
+
isf = (tree[pos[0]].parent_position(pos[1])
|
|
83
|
+
is None) or not is_first
|
|
84
|
+
entry = tree.decorate(pos[0], entry, is_first=isf)
|
|
85
|
+
return entry
|
|
86
|
+
|
|
87
|
+
def get_decorated(self, pos):
|
|
88
|
+
return self._get_decorated_entry(self._tree, pos)
|
|
89
|
+
|
|
90
|
+
def decorate(self, pos, widget, is_first=True):
|
|
91
|
+
return self._get_decorated_entry(self._tree, pos, widget, is_first)
|
|
92
|
+
|
|
93
|
+
# Collapse API
|
|
94
|
+
def _get_subtree_for(self, pos):
|
|
95
|
+
"""returns Tree that manages pos[-1]"""
|
|
96
|
+
res = self._tree
|
|
97
|
+
candidate = self._lookup_entry(self._tree, pos[:-1])
|
|
98
|
+
if isinstance(candidate, Tree):
|
|
99
|
+
res = candidate
|
|
100
|
+
return res
|
|
101
|
+
|
|
102
|
+
def collapsible(self, pos):
|
|
103
|
+
res = False
|
|
104
|
+
subtree = self._get_subtree_for(pos)
|
|
105
|
+
if isinstance(subtree, (CollapseMixin, NestedTree)):
|
|
106
|
+
res = subtree.collapsible(pos[-1])
|
|
107
|
+
return res
|
|
108
|
+
|
|
109
|
+
def is_collapsed(self, pos):
|
|
110
|
+
res = False
|
|
111
|
+
subtree = self._get_subtree_for(pos)
|
|
112
|
+
if isinstance(subtree, (CollapseMixin, NestedTree)):
|
|
113
|
+
res = subtree.is_collapsed(pos[-1])
|
|
114
|
+
return res
|
|
115
|
+
|
|
116
|
+
def toggle_collapsed(self, pos):
|
|
117
|
+
subtree = self._get_subtree_for(pos)
|
|
118
|
+
if isinstance(subtree, (CollapseMixin, NestedTree)):
|
|
119
|
+
subtree.toggle_collapsed(pos)
|
|
120
|
+
|
|
121
|
+
def collapse(self, pos):
|
|
122
|
+
subtree = self._get_subtree_for(pos)
|
|
123
|
+
if isinstance(subtree, (CollapseMixin, NestedTree)):
|
|
124
|
+
subtree.collapse(pos[-1])
|
|
125
|
+
|
|
126
|
+
def collapse_all(self):
|
|
127
|
+
self._collapse_all(self._tree, self.root)
|
|
128
|
+
|
|
129
|
+
def _collapse_all(self, tree, pos=None):
|
|
130
|
+
if pos is not None:
|
|
131
|
+
if isinstance(tree, (CollapseMixin, NestedTree)):
|
|
132
|
+
tree.expand_all()
|
|
133
|
+
|
|
134
|
+
if len(pos) > 1:
|
|
135
|
+
self._collapse_all(tree[pos[0]], pos[1:])
|
|
136
|
+
nextpos = tree.next_position(pos[0])
|
|
137
|
+
if nextpos is not None:
|
|
138
|
+
nentry = tree[nextpos]
|
|
139
|
+
if isinstance(nentry, Tree):
|
|
140
|
+
self._collapse_all(nentry, (nentry.root,))
|
|
141
|
+
self._collapse_all(tree, (nextpos,))
|
|
142
|
+
if isinstance(tree, (CollapseMixin, NestedTree)):
|
|
143
|
+
tree.collapse_all()
|
|
144
|
+
|
|
145
|
+
def expand(self, pos):
|
|
146
|
+
subtree = self._get_subtree_for(pos)
|
|
147
|
+
if isinstance(subtree, (CollapseMixin, NestedTree)):
|
|
148
|
+
subtree.expand(pos[-1])
|
|
149
|
+
|
|
150
|
+
def expand_all(self):
|
|
151
|
+
self._expand_all(self._tree, self.root)
|
|
152
|
+
|
|
153
|
+
def _expand_all(self, tree, pos=None):
|
|
154
|
+
if pos is not None:
|
|
155
|
+
if isinstance(tree, (CollapseMixin, NestedTree)):
|
|
156
|
+
tree.expand_all()
|
|
157
|
+
if len(pos) > 1:
|
|
158
|
+
self._expand_all(tree[pos[0]], pos[1:])
|
|
159
|
+
nextpos = tree.next_position(pos[0])
|
|
160
|
+
if nextpos is not None:
|
|
161
|
+
nentry = tree[nextpos]
|
|
162
|
+
if isinstance(nentry, Tree):
|
|
163
|
+
self._expand_all(nentry, (nentry.root,))
|
|
164
|
+
self._expand_all(tree, (nextpos,))
|
|
165
|
+
if isinstance(tree, (CollapseMixin, NestedTree)):
|
|
166
|
+
tree.expand_all()
|
|
167
|
+
|
|
168
|
+
def is_leaf(self, pos, outmost_only=False):
|
|
169
|
+
return self.first_child_position(pos, outmost_only) is None
|
|
170
|
+
|
|
171
|
+
################################################
|
|
172
|
+
# Tree API
|
|
173
|
+
################################################
|
|
174
|
+
def parent_position(self, pos):
|
|
175
|
+
candidate_pos = self._parent_position(self._tree, pos)
|
|
176
|
+
# return sanitized path (ensure it points to content, not a subtree)
|
|
177
|
+
return self._sanitize_position(candidate_pos)
|
|
178
|
+
|
|
179
|
+
def _parent_position(self, tree, pos):
|
|
180
|
+
candidate_pos = None
|
|
181
|
+
if len(pos) > 1:
|
|
182
|
+
# get the deepest subtree
|
|
183
|
+
subtree_pos = pos[:-1]
|
|
184
|
+
subtree = self._lookup_entry(tree, subtree_pos)
|
|
185
|
+
# get parent for our position in this subtree
|
|
186
|
+
least_pos = pos[-1]
|
|
187
|
+
subparent_pos = subtree.parent_position(least_pos)
|
|
188
|
+
if subparent_pos is not None:
|
|
189
|
+
# in case there is one, we are done, the position we look for
|
|
190
|
+
# is the path up to the subtree plus the local parent position.
|
|
191
|
+
candidate_pos = subtree_pos + (subparent_pos,)
|
|
192
|
+
else:
|
|
193
|
+
# otherwise we recur and look for subtree's parent in the next
|
|
194
|
+
# outer tree
|
|
195
|
+
candidate_pos = self._parent_position(self._tree, subtree_pos)
|
|
196
|
+
else:
|
|
197
|
+
# there is only one position in the path, we return its parent in
|
|
198
|
+
# the outmost tree
|
|
199
|
+
outer_parent = self._tree.parent_position(pos[0])
|
|
200
|
+
if outer_parent is not None:
|
|
201
|
+
# result needs to be valid position (tuple of local positions)
|
|
202
|
+
candidate_pos = outer_parent,
|
|
203
|
+
return candidate_pos
|
|
204
|
+
|
|
205
|
+
def first_child_position(self, pos, outmost_only=False):
|
|
206
|
+
childpos = self._first_child_position(self._tree, pos, outmost_only)
|
|
207
|
+
return self._sanitize_position(childpos, self._tree)
|
|
208
|
+
|
|
209
|
+
def _first_child_position(self, tree, pos, outmost_only=False):
|
|
210
|
+
childpos = None
|
|
211
|
+
# get content at first path element in outmost tree
|
|
212
|
+
entry = tree[pos[0]]
|
|
213
|
+
if isinstance(entry, Tree) and not outmost_only and len(pos) > 1:
|
|
214
|
+
# this points to a tree and we don't check the outmost tree only
|
|
215
|
+
# recur: get first child in the subtree for remaining path
|
|
216
|
+
subchild = self._first_child_position(entry, pos[1:])
|
|
217
|
+
if subchild is not None:
|
|
218
|
+
# found a childposition, re-append the path up to this subtree
|
|
219
|
+
childpos = (pos[0],) + subchild
|
|
220
|
+
return childpos
|
|
221
|
+
else:
|
|
222
|
+
# continue in the next outer tree only if we do not drop
|
|
223
|
+
# "covered" parts and the position path points to a parent-less
|
|
224
|
+
# position in the subtree.
|
|
225
|
+
if (entry.parent_position(pos[1]) is not None or not
|
|
226
|
+
self._interpret_covered):
|
|
227
|
+
return None
|
|
228
|
+
|
|
229
|
+
# return the first child of the outmost tree
|
|
230
|
+
outerchild = tree.first_child_position(pos[0])
|
|
231
|
+
if outerchild is not None:
|
|
232
|
+
childpos = outerchild,
|
|
233
|
+
return childpos
|
|
234
|
+
|
|
235
|
+
def last_child_position(self, pos, outmost_only=False):
|
|
236
|
+
childpos = self._last_child_position(self._tree, pos, outmost_only)
|
|
237
|
+
return self._sanitize_position(childpos, self._tree)
|
|
238
|
+
|
|
239
|
+
def _last_child_position(self, tree, pos, outmost_only=False):
|
|
240
|
+
childpos = None
|
|
241
|
+
# get content at first path element in outmost tree
|
|
242
|
+
entry = tree[pos[0]]
|
|
243
|
+
if isinstance(entry, Tree) and not outmost_only and len(pos) > 1:
|
|
244
|
+
# this points to a tree and we don't check the outmost tree only
|
|
245
|
+
|
|
246
|
+
# get last child in the outmost tree if we do not drop "covered"
|
|
247
|
+
# parts and the position path points to a root of the subtree.
|
|
248
|
+
if self._interpret_covered and entry.parent_position(pos[1]) is None:
|
|
249
|
+
# return the last child of the outmost tree
|
|
250
|
+
outerchild = tree.last_child_position(pos[0])
|
|
251
|
+
if outerchild is not None:
|
|
252
|
+
childpos = outerchild,
|
|
253
|
+
|
|
254
|
+
# continue as if we have not found anything yet
|
|
255
|
+
if childpos is None:
|
|
256
|
+
# recur: get last child in the subtree for remaining path
|
|
257
|
+
subchild = self._last_child_position(entry, pos[1:])
|
|
258
|
+
if subchild is not None:
|
|
259
|
+
# found a childposition, re-prepend path up to this subtree
|
|
260
|
+
childpos = (pos[0],) + subchild
|
|
261
|
+
else:
|
|
262
|
+
# outmost position element does not point to a tree:
|
|
263
|
+
# return the last child of the outmost tree
|
|
264
|
+
outerchild = tree.last_child_position(pos[0])
|
|
265
|
+
if outerchild is not None:
|
|
266
|
+
childpos = outerchild,
|
|
267
|
+
return childpos
|
|
268
|
+
|
|
269
|
+
def _next_sibling_position(self, tree, pos):
|
|
270
|
+
candidate = None
|
|
271
|
+
if len(pos) > 1:
|
|
272
|
+
# if position path does not point to position in outmost tree,
|
|
273
|
+
# first get the subtree as pointed out by first dimension, recur
|
|
274
|
+
# and check if some inner tree already returns a sibling
|
|
275
|
+
subtree = tree[pos[0]]
|
|
276
|
+
subsibling_pos = self._next_sibling_position(subtree, pos[1:])
|
|
277
|
+
if subsibling_pos is not None:
|
|
278
|
+
# we found our sibling, prepend the path up to the subtree
|
|
279
|
+
candidate = pos[:1] + subsibling_pos
|
|
280
|
+
else:
|
|
281
|
+
# no deeper tree has sibling. If inner position is root node
|
|
282
|
+
# the sibling in the outer tree is a valid candidate
|
|
283
|
+
subparent = subtree.parent_position(pos[1])
|
|
284
|
+
if subparent is None:
|
|
285
|
+
# check if outer tree defines sibling
|
|
286
|
+
next_sib = tree.next_sibling_position(pos[0])
|
|
287
|
+
if next_sib is not None:
|
|
288
|
+
# it has, we found our candidate
|
|
289
|
+
candidate = next_sib,
|
|
290
|
+
# if the inner position has depth 1, then the first child
|
|
291
|
+
# of its parent in the outer tree can be seen as candidate for
|
|
292
|
+
# this position next sibling. Those live in the shadow of the
|
|
293
|
+
# inner tree and are hidden unless requested otherwise
|
|
294
|
+
elif subtree.parent_position(subparent) is None and \
|
|
295
|
+
self._interpret_covered:
|
|
296
|
+
# we respect "covered" stuff and inner position has depth 1
|
|
297
|
+
# get (possibly nested) first child in outer tree
|
|
298
|
+
candidate = self._first_child_position(tree, pos[:1])
|
|
299
|
+
|
|
300
|
+
else:
|
|
301
|
+
# the position path points to the outmost tree
|
|
302
|
+
# just return its next sibling in the outmost tree
|
|
303
|
+
next_sib = tree.next_sibling_position(pos[0])
|
|
304
|
+
if next_sib is not None:
|
|
305
|
+
candidate = next_sib,
|
|
306
|
+
return candidate
|
|
307
|
+
|
|
308
|
+
def next_sibling_position(self, pos):
|
|
309
|
+
candidate = self._next_sibling_position(self._tree, pos)
|
|
310
|
+
return self._sanitize_position(candidate, self._tree)
|
|
311
|
+
|
|
312
|
+
def _prev_sibling_position(self, tree, pos):
|
|
313
|
+
candidate = None
|
|
314
|
+
if len(pos) > 1:
|
|
315
|
+
# if position path does not point to position in outmost tree,
|
|
316
|
+
# first get the subtree as pointed out by first dimension, recur
|
|
317
|
+
# and check if some inner tree already returns a sibling
|
|
318
|
+
subtree = tree[pos[0]]
|
|
319
|
+
subsibling_pos = self._prev_sibling_position(subtree, pos[1:])
|
|
320
|
+
if subsibling_pos is not None:
|
|
321
|
+
# we found our sibling, prepend the path up to the subtree
|
|
322
|
+
candidate = pos[:1] + subsibling_pos
|
|
323
|
+
else:
|
|
324
|
+
# no deeper tree has sibling. If inner position is root node
|
|
325
|
+
# the sibling in the outer tree is a valid candidate
|
|
326
|
+
subparent = subtree.parent_position(pos[1])
|
|
327
|
+
if subparent is None:
|
|
328
|
+
prev_sib = tree.prev_sibling_position(pos[0])
|
|
329
|
+
if prev_sib is not None:
|
|
330
|
+
candidate = prev_sib,
|
|
331
|
+
return candidate
|
|
332
|
+
# my position could be "hidden" by being child of a
|
|
333
|
+
# position pointing to a Tree object (which is then unfolded).
|
|
334
|
+
if self._interpret_covered:
|
|
335
|
+
# we respect "covered" stuff:
|
|
336
|
+
# if parent is Tree, return last child of its (last) root
|
|
337
|
+
parent_pos = self._parent_position(tree, pos)
|
|
338
|
+
if parent_pos is not None:
|
|
339
|
+
parent = self._lookup_entry(self._tree, parent_pos)
|
|
340
|
+
if isinstance(parent, Tree):
|
|
341
|
+
sib = parent.last_sibling_position(parent.root)
|
|
342
|
+
candidate = parent.last_child_position(sib)
|
|
343
|
+
if candidate is not None:
|
|
344
|
+
candidate = parent_pos + (candidate,)
|
|
345
|
+
else:
|
|
346
|
+
# pos points to position in outmost tree
|
|
347
|
+
prev_sib = tree.prev_sibling_position(pos[0])
|
|
348
|
+
if prev_sib is not None:
|
|
349
|
+
candidate = prev_sib,
|
|
350
|
+
# In case our new candidate points to a Tree, pick its last root node
|
|
351
|
+
if candidate is not None:
|
|
352
|
+
entry = self._lookup_entry(tree, candidate)
|
|
353
|
+
if isinstance(entry, Tree):
|
|
354
|
+
candidate = (candidate) + (entry.last_sibling_position(entry.root),)
|
|
355
|
+
return candidate
|
|
356
|
+
|
|
357
|
+
def prev_sibling_position(self, pos):
|
|
358
|
+
candidate = self._prev_sibling_position(self._tree, pos)
|
|
359
|
+
return self._sanitize_position(candidate, self._tree)
|
|
360
|
+
|
|
361
|
+
def last_decendant(self, pos):
|
|
362
|
+
def lastd(pos):
|
|
363
|
+
c = self.last_child_position(pos)
|
|
364
|
+
if c is not None:
|
|
365
|
+
c = self.last_sibling_position(c)
|
|
366
|
+
return c
|
|
367
|
+
return self._last_in_direction(pos, lastd)
|
urwidtrees/tree.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# Copyright (C) 2013 Patrick Totzke <patricktotzke@gmail.com>
|
|
2
|
+
# This file is released under the GNU GPL, version 3 or a later revision.
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Tree(object):
|
|
6
|
+
"""
|
|
7
|
+
Base class for a tree strucures that can be displayed by :class:`TreeBox`
|
|
8
|
+
widgets. An instance defines a structure by defining local transformations
|
|
9
|
+
on positions. That is, by overwriting
|
|
10
|
+
|
|
11
|
+
* `next_sibling_position`
|
|
12
|
+
* `prev_sibling_position`
|
|
13
|
+
* `parent_position`
|
|
14
|
+
* `first_child_position`
|
|
15
|
+
* `last_child_position`
|
|
16
|
+
|
|
17
|
+
that compute the next position in the respective direction. Also, they need
|
|
18
|
+
to implement method `__getitem__` that returns a :class:`Widget` for a
|
|
19
|
+
given position.
|
|
20
|
+
|
|
21
|
+
The type of objects used as positions may vary in subclasses and is
|
|
22
|
+
deliberately unspecified for the base class.
|
|
23
|
+
|
|
24
|
+
This base class already implements methods based on the local
|
|
25
|
+
transformations above. These include :meth:`depth`, :meth:`last_decendant`
|
|
26
|
+
and :meth:`[next|prev]_position <next_position>` that computes
|
|
27
|
+
next/previous positions in depth-first order.
|
|
28
|
+
"""
|
|
29
|
+
root = None
|
|
30
|
+
|
|
31
|
+
# local helper
|
|
32
|
+
def _get(self, pos):
|
|
33
|
+
"""loads widget at given position; handling invalid arguments"""
|
|
34
|
+
res = None, None
|
|
35
|
+
if pos is not None:
|
|
36
|
+
try:
|
|
37
|
+
res = self[pos], pos
|
|
38
|
+
except (IndexError, KeyError):
|
|
39
|
+
pass
|
|
40
|
+
return res
|
|
41
|
+
|
|
42
|
+
def _next_of_kin(self, pos):
|
|
43
|
+
"""
|
|
44
|
+
looks up the next sibling of the closest ancestor with not-None next
|
|
45
|
+
siblings.
|
|
46
|
+
"""
|
|
47
|
+
candidate = None
|
|
48
|
+
parent = self.parent_position(pos)
|
|
49
|
+
if parent is not None:
|
|
50
|
+
candidate = self.next_sibling_position(parent)
|
|
51
|
+
if candidate is None:
|
|
52
|
+
candidate = self._next_of_kin(parent)
|
|
53
|
+
return candidate
|
|
54
|
+
|
|
55
|
+
@staticmethod
|
|
56
|
+
def _last_in_direction(starting_pos, direction):
|
|
57
|
+
"""
|
|
58
|
+
move in the tree in given direction and return the last position.
|
|
59
|
+
|
|
60
|
+
:param starting_pos: position to start at
|
|
61
|
+
:param direction: callable that transforms a position into a position.
|
|
62
|
+
"""
|
|
63
|
+
cur_pos = None
|
|
64
|
+
next_pos = starting_pos
|
|
65
|
+
while next_pos is not None:
|
|
66
|
+
cur_pos = next_pos
|
|
67
|
+
next_pos = direction(cur_pos)
|
|
68
|
+
return cur_pos
|
|
69
|
+
|
|
70
|
+
def depth(self, pos):
|
|
71
|
+
"""determine depth of node at pos"""
|
|
72
|
+
parent = self.parent_position(pos)
|
|
73
|
+
if parent is None:
|
|
74
|
+
return 0
|
|
75
|
+
else:
|
|
76
|
+
return self.depth(parent) + 1
|
|
77
|
+
|
|
78
|
+
def is_leaf(self, pos):
|
|
79
|
+
"""checks if given position has no children"""
|
|
80
|
+
return self.first_child_position(pos) is None
|
|
81
|
+
|
|
82
|
+
def first_ancestor(self, pos):
|
|
83
|
+
"""
|
|
84
|
+
position of pos's ancestor with depth 0. Usually, this should return
|
|
85
|
+
the root node, but a :class:`Tree` might represent a forrest - have
|
|
86
|
+
multiple nodes without parent.
|
|
87
|
+
"""
|
|
88
|
+
return self._last_in_direction(pos, self.parent_position)
|
|
89
|
+
|
|
90
|
+
def last_decendant(self, pos):
|
|
91
|
+
"""position of last (in DFO) descendant of pos"""
|
|
92
|
+
return self._last_in_direction(pos, self.last_child_position)
|
|
93
|
+
|
|
94
|
+
def last_sibling_position(self, pos):
|
|
95
|
+
"""position of last sibling of pos"""
|
|
96
|
+
return self._last_in_direction(pos, self.next_sibling_position)
|
|
97
|
+
|
|
98
|
+
def first_sibling_position(self, pos):
|
|
99
|
+
"""position of first sibling of pos"""
|
|
100
|
+
return self._last_in_direction(pos, self.prev_sibling_position)
|
|
101
|
+
|
|
102
|
+
def next_position(self, pos):
|
|
103
|
+
"""returns the next position in depth-first order"""
|
|
104
|
+
candidate = None
|
|
105
|
+
if pos is not None:
|
|
106
|
+
candidate = self.first_child_position(pos)
|
|
107
|
+
if candidate is None:
|
|
108
|
+
candidate = self.next_sibling_position(pos)
|
|
109
|
+
if candidate is None:
|
|
110
|
+
candidate = self._next_of_kin(pos)
|
|
111
|
+
return candidate
|
|
112
|
+
|
|
113
|
+
def prev_position(self, pos):
|
|
114
|
+
"""returns the previous position in depth-first order"""
|
|
115
|
+
candidate = None
|
|
116
|
+
if pos is not None:
|
|
117
|
+
prevsib = self.prev_sibling_position(pos) # is None if first
|
|
118
|
+
if prevsib is not None:
|
|
119
|
+
candidate = self.last_decendant(prevsib)
|
|
120
|
+
else:
|
|
121
|
+
parent = self.parent_position(pos)
|
|
122
|
+
if parent is not None:
|
|
123
|
+
candidate = parent
|
|
124
|
+
return candidate
|
|
125
|
+
|
|
126
|
+
def positions(self, reverse=False):
|
|
127
|
+
"""returns a generator that walks the positions of this tree in DFO"""
|
|
128
|
+
def Posgen(reverse):
|
|
129
|
+
if reverse:
|
|
130
|
+
lastrootsib = self.last_sibling_position(self.root)
|
|
131
|
+
current = self.last_decendant(lastrootsib)
|
|
132
|
+
while current is not None:
|
|
133
|
+
yield current
|
|
134
|
+
current = self.prev_position(current)
|
|
135
|
+
else:
|
|
136
|
+
current = self.root
|
|
137
|
+
while current is not None:
|
|
138
|
+
yield current
|
|
139
|
+
current = self.next_position(current)
|
|
140
|
+
return Posgen(reverse)
|
|
141
|
+
|
|
142
|
+
####################################################################
|
|
143
|
+
# End of high-level helper implementation. The following need to be
|
|
144
|
+
# overwritten by subclasses
|
|
145
|
+
####################################################################
|
|
146
|
+
def parent_position(pos):
|
|
147
|
+
"""returns the position of the parent node of the node at `pos`
|
|
148
|
+
or `None` if none exists."""
|
|
149
|
+
return None
|
|
150
|
+
|
|
151
|
+
def first_child_position(self, pos):
|
|
152
|
+
"""returns the position of the first child of the node at `pos`,
|
|
153
|
+
or `None` if none exists."""
|
|
154
|
+
return None
|
|
155
|
+
|
|
156
|
+
def last_child_position(self, pos):
|
|
157
|
+
"""returns the position of the last child of the node at `pos`,
|
|
158
|
+
or `None` if none exists."""
|
|
159
|
+
return None
|
|
160
|
+
|
|
161
|
+
def next_sibling_position(self, pos):
|
|
162
|
+
"""returns the position of the next sibling of the node at `pos`,
|
|
163
|
+
or `None` if none exists."""
|
|
164
|
+
return None
|
|
165
|
+
|
|
166
|
+
def prev_sibling_position(self, pos):
|
|
167
|
+
"""returns the position of the previous sibling of the node at `pos`,
|
|
168
|
+
or `None` if none exists."""
|
|
169
|
+
return None
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
class SimpleTree(Tree):
|
|
173
|
+
"""
|
|
174
|
+
Walks on a given fixed acyclic structure given as a list of nodes; every
|
|
175
|
+
node is a tuple `(content, children)`, where `content` is a `urwid.Widget`
|
|
176
|
+
to be displayed at that position and `children` is either `None` or a list
|
|
177
|
+
of nodes.
|
|
178
|
+
|
|
179
|
+
Positions are lists of integers determining a path from the root node with
|
|
180
|
+
position `(0,)`.
|
|
181
|
+
"""
|
|
182
|
+
def __init__(self, treelist):
|
|
183
|
+
self._treelist = treelist
|
|
184
|
+
self.root = (0,) if treelist else None
|
|
185
|
+
Tree.__init__(self)
|
|
186
|
+
|
|
187
|
+
# a few local helper methods
|
|
188
|
+
def _get_substructure(self, treelist, pos):
|
|
189
|
+
"""recursive helper to look up node-tuple for `pos` in `treelist`"""
|
|
190
|
+
subtree = None
|
|
191
|
+
if len(pos) > 1:
|
|
192
|
+
subtree = self._get_substructure(treelist[pos[0]][1], pos[1:])
|
|
193
|
+
else:
|
|
194
|
+
try:
|
|
195
|
+
subtree = treelist[pos[0]]
|
|
196
|
+
except (IndexError, TypeError):
|
|
197
|
+
pass
|
|
198
|
+
return subtree
|
|
199
|
+
|
|
200
|
+
def _get_node(self, treelist, pos):
|
|
201
|
+
"""
|
|
202
|
+
look up widget at `pos` of `treelist`; default to None if
|
|
203
|
+
nonexistent.
|
|
204
|
+
"""
|
|
205
|
+
node = None
|
|
206
|
+
if pos is not None:
|
|
207
|
+
subtree = self._get_substructure(treelist, pos)
|
|
208
|
+
if subtree is not None:
|
|
209
|
+
node = subtree[0]
|
|
210
|
+
return node
|
|
211
|
+
|
|
212
|
+
def _confirm_pos(self, pos):
|
|
213
|
+
"""look up widget for pos and default to None"""
|
|
214
|
+
candidate = None
|
|
215
|
+
if self._get_node(self._treelist, pos) is not None:
|
|
216
|
+
candidate = pos
|
|
217
|
+
return candidate
|
|
218
|
+
|
|
219
|
+
# Tree API
|
|
220
|
+
def __getitem__(self, pos):
|
|
221
|
+
return self._get_node(self._treelist, pos)
|
|
222
|
+
|
|
223
|
+
@staticmethod
|
|
224
|
+
def parent_position(pos):
|
|
225
|
+
parent = None
|
|
226
|
+
if pos is not None and len(pos) > 1:
|
|
227
|
+
parent = pos[:-1]
|
|
228
|
+
return parent
|
|
229
|
+
|
|
230
|
+
def first_child_position(self, pos):
|
|
231
|
+
return self._confirm_pos(pos + (0,))
|
|
232
|
+
|
|
233
|
+
def last_child_position(self, pos):
|
|
234
|
+
candidate = None
|
|
235
|
+
subtree = self._get_substructure(self._treelist, pos)
|
|
236
|
+
if subtree is not None:
|
|
237
|
+
children = subtree[1]
|
|
238
|
+
if children is not None:
|
|
239
|
+
candidate = pos + (len(children) - 1,)
|
|
240
|
+
return candidate
|
|
241
|
+
|
|
242
|
+
def next_sibling_position(self, pos):
|
|
243
|
+
return self._confirm_pos(pos[:-1] + (pos[-1] + 1,))
|
|
244
|
+
|
|
245
|
+
@staticmethod
|
|
246
|
+
def prev_sibling_position(pos):
|
|
247
|
+
return pos[:-1] + (pos[-1] - 1,) if (pos[-1] > 0) else None
|
|
248
|
+
|
|
249
|
+
# optimizations
|
|
250
|
+
@staticmethod
|
|
251
|
+
def depth(pos):
|
|
252
|
+
"""more performant implementation due to specific structure of pos"""
|
|
253
|
+
return len(pos) - 1
|
urwidtrees/version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = '1.0.3'
|