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/widgets.py
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
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
|
+
import urwid
|
|
5
|
+
from urwid import WidgetWrap, ListBox
|
|
6
|
+
from urwid import signals
|
|
7
|
+
|
|
8
|
+
from .decoration import DecoratedTree, CollapseMixin
|
|
9
|
+
from .nested import NestedTree
|
|
10
|
+
try:
|
|
11
|
+
# lru_cache is part of the stdlib from v3.2 onwards
|
|
12
|
+
from functools import lru_cache
|
|
13
|
+
except ImportError:
|
|
14
|
+
# on older versions we use a backport
|
|
15
|
+
from .lru_cache import lru_cache
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# The following functions are used to check dynamically if a tree offers sub-APIs
|
|
19
|
+
|
|
20
|
+
def implementsDecorateAPI(tree):
|
|
21
|
+
"""determines if given tree offers line decoration"""
|
|
22
|
+
return isinstance(tree, (DecoratedTree, NestedTree))
|
|
23
|
+
|
|
24
|
+
def implementsCollapseAPI(tree):
|
|
25
|
+
"""determines if given tree can collapse positions"""
|
|
26
|
+
return isinstance(tree, (CollapseMixin, NestedTree))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class TreeListWalker(urwid.ListWalker):
|
|
30
|
+
"""
|
|
31
|
+
ListWalker to walk through a class:`Tree`.
|
|
32
|
+
|
|
33
|
+
This translates a :class:`Tree` into a :class:`urwid.ListWalker` that is
|
|
34
|
+
digestible by :class:`urwid.ListBox`.
|
|
35
|
+
It uses :meth:`Tree.[next|prev]_position <Tree.next_position>` to determine
|
|
36
|
+
the next/previous position in depth first order.
|
|
37
|
+
"""
|
|
38
|
+
def __init__(self, tree, focus=None):
|
|
39
|
+
"""
|
|
40
|
+
:param tree: the tree to be displayed
|
|
41
|
+
:type tree: Tree
|
|
42
|
+
:param focus: position of node to be focussed initially.
|
|
43
|
+
This has to be a valid position in the Tree.
|
|
44
|
+
It defaults to the value of `Tree.root`.
|
|
45
|
+
"""
|
|
46
|
+
self._tree = tree
|
|
47
|
+
self._focus = focus or tree.root
|
|
48
|
+
self.root = tree.root
|
|
49
|
+
|
|
50
|
+
@lru_cache()
|
|
51
|
+
def __getitem__(self, pos):
|
|
52
|
+
"""gets (possibly decorated) line widget at given position"""
|
|
53
|
+
if implementsDecorateAPI(self._tree):
|
|
54
|
+
entry = self._tree.get_decorated(pos)
|
|
55
|
+
else:
|
|
56
|
+
entry = self._tree[pos]
|
|
57
|
+
return entry
|
|
58
|
+
|
|
59
|
+
def clear_cache(self):
|
|
60
|
+
"""removes all cached lines"""
|
|
61
|
+
self.__getitem__.cache_clear()
|
|
62
|
+
self._modified()
|
|
63
|
+
|
|
64
|
+
def _get(self, pos):
|
|
65
|
+
"""looks up widget for given position; handling invalid arguments"""
|
|
66
|
+
res = None, None
|
|
67
|
+
if pos is not None:
|
|
68
|
+
try:
|
|
69
|
+
res = self[pos], pos
|
|
70
|
+
except (IndexError, KeyError):
|
|
71
|
+
pass
|
|
72
|
+
return res
|
|
73
|
+
|
|
74
|
+
# List Walker API.
|
|
75
|
+
def get_focus(self):
|
|
76
|
+
return self._get(self._focus)
|
|
77
|
+
|
|
78
|
+
def set_focus(self, pos):
|
|
79
|
+
self._focus = pos
|
|
80
|
+
self._modified()
|
|
81
|
+
|
|
82
|
+
def get_next(self, pos):
|
|
83
|
+
return self._get(self._tree.next_position(pos))
|
|
84
|
+
|
|
85
|
+
def get_prev(self, pos):
|
|
86
|
+
return self._get(self._tree.prev_position(pos))
|
|
87
|
+
|
|
88
|
+
def positions(self, reverse=False):
|
|
89
|
+
"""returns a generator that walks the tree's positions"""
|
|
90
|
+
return self._tree.positions(reverse)
|
|
91
|
+
# end of List Walker API
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class TreeBox(WidgetWrap):
|
|
95
|
+
"""
|
|
96
|
+
A widget that displays a given :class:`Tree`.
|
|
97
|
+
This is essentially a :class:`ListBox` with the ability to move the focus
|
|
98
|
+
based on directions in the Tree and to collapse/expand subtrees if
|
|
99
|
+
possible.
|
|
100
|
+
|
|
101
|
+
TreeBox interprets `left/right` as well as `page up/`page down` to move the
|
|
102
|
+
focus to parent/first child and next/previous sibling respectively. All
|
|
103
|
+
other keys are passed to the underlying ListBox.
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
def __init__(self, tree, focus=None):
|
|
107
|
+
"""
|
|
108
|
+
:param tree: tree of widgets to be displayed.
|
|
109
|
+
:type tree: Tree
|
|
110
|
+
:param focus: initially focussed position
|
|
111
|
+
"""
|
|
112
|
+
self._tree = tree
|
|
113
|
+
self._walker = TreeListWalker(tree)
|
|
114
|
+
self._outer_list = ListBox(self._walker)
|
|
115
|
+
if focus is not None:
|
|
116
|
+
self.set_focus(focus)
|
|
117
|
+
super().__init__(self._outer_list)
|
|
118
|
+
|
|
119
|
+
# Widget API
|
|
120
|
+
def get_focus(self):
|
|
121
|
+
return self._outer_list.get_focus()
|
|
122
|
+
|
|
123
|
+
def set_focus(self, pos, valign=None):
|
|
124
|
+
position = self._outer_list.set_focus(pos)
|
|
125
|
+
if valign:
|
|
126
|
+
position = self._outer_list.set_focus_valign(valign)
|
|
127
|
+
return position
|
|
128
|
+
|
|
129
|
+
def refresh(self):
|
|
130
|
+
self._walker.clear_cache()
|
|
131
|
+
signals.emit_signal(self._walker, "modified")
|
|
132
|
+
|
|
133
|
+
def keypress(self, size, key):
|
|
134
|
+
key = self._outer_list.keypress(size, key)
|
|
135
|
+
if key in ['left', 'right', '[', ']', '-', '+', 'C', 'E', ]:
|
|
136
|
+
if key == 'left':
|
|
137
|
+
self.focus_parent()
|
|
138
|
+
elif key == 'right':
|
|
139
|
+
self.focus_first_child()
|
|
140
|
+
elif key == '[':
|
|
141
|
+
self.focus_prev_sibling()
|
|
142
|
+
elif key == ']':
|
|
143
|
+
self.focus_next_sibling()
|
|
144
|
+
elif key == '-':
|
|
145
|
+
self.collapse_focussed()
|
|
146
|
+
elif key == '+':
|
|
147
|
+
self.expand_focussed()
|
|
148
|
+
elif key == 'C':
|
|
149
|
+
self.collapse_all()
|
|
150
|
+
elif key == 'E':
|
|
151
|
+
self.expand_all()
|
|
152
|
+
# This is a hack around ListBox misbehaving:
|
|
153
|
+
# it seems impossible to set the focus without calling keypress as
|
|
154
|
+
# otherwise the change becomes visible only after the next render()
|
|
155
|
+
return self._outer_list.keypress(size, None)
|
|
156
|
+
else:
|
|
157
|
+
return self._outer_list.keypress(size, key)
|
|
158
|
+
|
|
159
|
+
# Collapse operations
|
|
160
|
+
def collapse_focussed(self):
|
|
161
|
+
"""
|
|
162
|
+
Collapse currently focussed position; works only if the underlying
|
|
163
|
+
tree allows it.
|
|
164
|
+
"""
|
|
165
|
+
if implementsCollapseAPI(self._tree):
|
|
166
|
+
w, focuspos = self.get_focus()
|
|
167
|
+
self._tree.collapse(focuspos)
|
|
168
|
+
self._walker.clear_cache()
|
|
169
|
+
self.refresh()
|
|
170
|
+
|
|
171
|
+
def expand_focussed(self):
|
|
172
|
+
"""
|
|
173
|
+
Expand currently focussed position; works only if the underlying
|
|
174
|
+
tree allows it.
|
|
175
|
+
"""
|
|
176
|
+
if implementsCollapseAPI(self._tree):
|
|
177
|
+
w, focuspos = self.get_focus()
|
|
178
|
+
self._tree.expand(focuspos)
|
|
179
|
+
self._walker.clear_cache()
|
|
180
|
+
self.refresh()
|
|
181
|
+
|
|
182
|
+
def collapse_all(self):
|
|
183
|
+
"""
|
|
184
|
+
Collapse all positions; works only if the underlying tree allows it.
|
|
185
|
+
"""
|
|
186
|
+
if implementsCollapseAPI(self._tree):
|
|
187
|
+
self._tree.collapse_all()
|
|
188
|
+
self.set_focus(self._tree.root)
|
|
189
|
+
self._walker.clear_cache()
|
|
190
|
+
self.refresh()
|
|
191
|
+
|
|
192
|
+
def expand_all(self):
|
|
193
|
+
"""
|
|
194
|
+
Expand all positions; works only if the underlying tree allows it.
|
|
195
|
+
"""
|
|
196
|
+
if implementsCollapseAPI(self._tree):
|
|
197
|
+
self._tree.expand_all()
|
|
198
|
+
self._walker.clear_cache()
|
|
199
|
+
self.refresh()
|
|
200
|
+
|
|
201
|
+
# Tree based focus movement
|
|
202
|
+
def focus_parent(self):
|
|
203
|
+
"""move focus to parent node of currently focussed one"""
|
|
204
|
+
w, focuspos = self.get_focus()
|
|
205
|
+
parent = self._tree.parent_position(focuspos)
|
|
206
|
+
if parent is not None:
|
|
207
|
+
self.set_focus(parent)
|
|
208
|
+
|
|
209
|
+
def focus_first_child(self):
|
|
210
|
+
"""move focus to first child of currently focussed one"""
|
|
211
|
+
w, focuspos = self.get_focus()
|
|
212
|
+
child = self._tree.first_child_position(focuspos)
|
|
213
|
+
if child is not None:
|
|
214
|
+
self.set_focus(child)
|
|
215
|
+
|
|
216
|
+
def focus_last_child(self):
|
|
217
|
+
"""move focus to last child of currently focussed one"""
|
|
218
|
+
w, focuspos = self.get_focus()
|
|
219
|
+
child = self._tree.last_child_position(focuspos)
|
|
220
|
+
if child is not None:
|
|
221
|
+
self.set_focus(child)
|
|
222
|
+
|
|
223
|
+
def focus_next_sibling(self):
|
|
224
|
+
"""move focus to next sibling of currently focussed one"""
|
|
225
|
+
w, focuspos = self.get_focus()
|
|
226
|
+
sib = self._tree.next_sibling_position(focuspos)
|
|
227
|
+
if sib is not None:
|
|
228
|
+
self.set_focus(sib)
|
|
229
|
+
|
|
230
|
+
def focus_prev_sibling(self):
|
|
231
|
+
"""move focus to previous sibling of currently focussed one"""
|
|
232
|
+
w, focuspos = self.get_focus()
|
|
233
|
+
sib = self._tree.prev_sibling_position(focuspos)
|
|
234
|
+
if sib is not None:
|
|
235
|
+
self.set_focus(sib)
|
|
236
|
+
|
|
237
|
+
def focus_next(self):
|
|
238
|
+
"""move focus to next position (DFO)"""
|
|
239
|
+
w, focuspos = self.get_focus()
|
|
240
|
+
next = self._tree.next_position(focuspos)
|
|
241
|
+
if next is not None:
|
|
242
|
+
self.set_focus(next)
|
|
243
|
+
|
|
244
|
+
def focus_prev(self):
|
|
245
|
+
"""move focus to previous position (DFO)"""
|
|
246
|
+
w, focuspos = self.get_focus()
|
|
247
|
+
prev = self._tree.prev_position(focuspos)
|
|
248
|
+
if prev is not None:
|
|
249
|
+
self.set_focus(prev)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: urwidtrees
|
|
3
|
+
Version: 1.0.3
|
|
4
|
+
Summary: Tree widgets for urwid
|
|
5
|
+
Home-page: https://github.com/pazz/urwidtrees
|
|
6
|
+
Author: Patrick Totzke
|
|
7
|
+
Author-email: patricktotzke@gmail.com
|
|
8
|
+
License: Licensed under the GNU GPL v3+.
|
|
9
|
+
License-File: LICENSE.md
|
|
10
|
+
Requires-Dist: urwid>=1.1.0
|
|
11
|
+
Provides-Extra: docs
|
|
12
|
+
Requires-Dist: mock; python_version < "3.3" and extra == "docs"
|
|
13
|
+
Dynamic: author
|
|
14
|
+
Dynamic: author-email
|
|
15
|
+
Dynamic: home-page
|
|
16
|
+
Dynamic: license
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
Dynamic: provides-extra
|
|
19
|
+
Dynamic: requires-dist
|
|
20
|
+
Dynamic: summary
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
urwidtrees/__init__.py,sha256=fyv2Vkr7vbl5oiBxJ6R5ycRBYMNdVwVbmy1eIDNspRA,302
|
|
2
|
+
urwidtrees/decoration.py,sha256=BchlTSm-z_0AXKYKyT9lZ3iMx_bC2w_ggUyO982MC50,19869
|
|
3
|
+
urwidtrees/lru_cache.py,sha256=3T93cna8Un00o6dtpByoh2xANiFKJ68e9tXBC9knKqo,6062
|
|
4
|
+
urwidtrees/nested.py,sha256=6vJbuyyE6rFWFzBYKOUAfpTtOE6ApdTBTW2uhIUhEdE,15887
|
|
5
|
+
urwidtrees/tree.py,sha256=Km9sLpUxVVxZLErCdVdhINUTg52Ak4NlfHXQ-vG9vOU,8761
|
|
6
|
+
urwidtrees/version.py,sha256=qKK4BMzdt84HiCorJPWqgEfa_DbJeuGx_bIPrDui2vc,22
|
|
7
|
+
urwidtrees/widgets.py,sha256=R72JGgfge8zSleKzx909Z46mmbi6S_IWQMNG0ENMxpI,8325
|
|
8
|
+
urwidtrees-1.0.3.dist-info/licenses/LICENSE.md,sha256=WJ7YI-moTFb-uVrFjnzzhGJrnL9P2iqQe8NuED3hutI,35141
|
|
9
|
+
urwidtrees-1.0.3.dist-info/METADATA,sha256=5mXBVGhvdM7Rv1f3hFiRxRj3rIlnHHLLwMnew1Gf_Xc,532
|
|
10
|
+
urwidtrees-1.0.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
11
|
+
urwidtrees-1.0.3.dist-info/top_level.txt,sha256=SmKwzwSHSgo_iALlq1-RjQBILDe-CT_AJrNkANfKIRI,11
|
|
12
|
+
urwidtrees-1.0.3.dist-info/RECORD,,
|